TALENTS FOR DEVELOPMENT - Petrovietnam UniversityFoundations of Engineering II- PVUTrần Ngọc Hiệp, MSc.Conditions StatementsSingle selection ifDouble Selection if-elseMultiple Selection
Trang 1TALENTS FOR DEVELOPMENT - Petrovietnam University
Chapter 6: Control Structures
Lecturer: Trần Ngọc Hiệp, MSc.
Email: hieptn@pvu.edu.vn Mobile: 0901 25 2468
Contents
Selection Statements For Loops
While Loops Do-While Loops Breaking out of Loops 1
Trang 2TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
Conditions Statements
3
Single selection (if) Double Selection (if-else) Multiple Selection (else-if) Multiple Selection (switch)
6.1 If statement
If statement is used in Python for selection
purposes
3
Trang 3TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
6.1 If statement - Example
5
6.1 If Statement - Problems
Problem 1: Write a
program which gets the
value of two integers from
user and display whether
first number is lower than,
equal to or greater than
the second number.
Problem 2: Prompt user to enter a value Check if the number is even then display a message that number is even If number
is odd then display number is odd.
5
Trang 4TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
6.1 If Statement - Solutions
7
6.1 If Statement - Task
Write a nested if statement to print the appropriate
activity depending on the value of a variable
temperature and humidity as in the table below:
7
Trang 5TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
6.2 Double selection statement (if…else)
9
6.2 Double selection statement - Problems
Problem 1: Write a program
which inputs the marks obtained
by student If the marks are
equal to or greater than 50 then
display the message that “You
are pass” else “Sorry you are
Trang 6TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
6.2 Double selection statement - Solutions
Trang 7TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
6.3 Multiple Selection (else if)
13
Condition1
If code Else if
codeCondition2
6.3 Multiple Selection - Problem
Problem 1: Write a program which prompt the user to enter a digit between 1 to 10 and display the digit entered by the user.
Problem 2: Write a program to calculate the monthly telephone bills as per by the following rules:
• Minimum 200.000 vnd for up to 100 calls
• Plus 600 vnd per call for next 50 calls.
• Plus 500 vnd per call for next 50 call.
• Plus 400 vnd per call for any call beyond 200 calls.
13
Trang 8TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
6.3 Multiple Selection - Problem
15
6.5 Repetition (Loops)
Repetition structures are used when a program needs
to repeatedly process one or more instructions until
some condition is met, at which time the loop ends.
Repetition allows the programmer to efficiently use
variables.
Parameters
of loops:
Starting Point
Ending Point
Sequence
of Moving
15
Trang 9TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
6.6 While loop
17
6.6 While loop Example:
17
Trang 10TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
Trang 11TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
6.6 While loop - Solutions
21
Trang 12TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
6.8 For loop
The structure of for loop is as
follows:
23
6.8 For loop - Problems
Problem 1: Write the table of 5 using for loop.
Problem 2: Write a program to calculate the factorial of a number using for loop.
23
Trang 13TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
6.8 For loop - Solution
25
6.8 For loop - Solution 25
Trang 14TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
Trang 15TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
Trang 16TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
Trang 17TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
6.9 Nested Loops
The placing of one
loop inside the body
of another loop is
called nesting
We can us loops inside loop body.
First inner loop complete its iteration and then control shift to outer one, this process continues till end.
33
6.9 Nested Loops
Nested while loop syntax:
33
Trang 18TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
Trang 19TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
6.10 Controlling Loops/ Jumping out of loop
37
We can use break and continue statement to control the
interaction of a loop:
Break statement: Stops the loops
and start executing program from
line after loop.
Continue statement: Breaks only the current integration.
Sometime, while executing a loop It becomes necessary to skip
a part of the loop or to leave the loop as soon as certain
condition becomes true, that is jump out of loop.
Break-continue statement example 37
Trang 20TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
Tasks
39
Task 1: Two numbers are
entered through the keyboard
Write a program to find the
value of one number raised to
the power of another.
Task 2: Write a program to sum
of digits of given integer number.
Tasks
Task 3: Write a program to check
given number is prime or not.
Task 4: Compute the natural logarithm of 2 by adding up to n terms in the series:
1-1/2+1/3-1/4+1/5-…1/n Where n is a positive integer and input by user
39
Trang 21TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
Solutions – Task 1
41
Solutions – Task 2 41
Trang 22TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
Solutions – Task 3
43
Solutions – Task 4 43
Trang 23TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
Trang 24TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc
Discussion
47
47