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.vnMobile: 0901 25 2468
Selection StatementsFor Loops
While LoopsDo-While LoopsBreaking out of Loops1
Trang 2TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc.
Conditions Statements
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
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.
Trang 5TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc.
6.2 Double selection statement (if…else)
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 fail”.
Problem 2: Ask use to enter two numbers Display which number is greater between two.
9
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)
If codeElse 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
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
6.6 While loopExample:
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
6.6 While loop - Your task
Task 1: Write a program to sum of digits of given integer number.
Task 2: Write a
program to calculate the sum of first 10 natural number.21
Trang 12TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc.
6.8 For loopThe structure of for loop is as
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
6.8 For loop - Solution25
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.
6.9 Nested LoopsNested while loopsyntax:
33
Trang 18TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc.
6.9 Nested LoopsNested for loopsyntax:
6.9 Nested LoopsNested while, for loopsyntax:
35
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
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 example37
Trang 20TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc.
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.
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
Solutions – Task 241
Trang 22TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc.
Solutions – Task 3
Solutions – Task 443
Trang 23TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc.
Summary
1• If else statement2• While loop
4• For loop
5• Break and continue
45
Trang 24TALENTS FOR DEVELOPMENT - Petrovietnam University
Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc.
47