Chapter 9 Flow of Control
1. Types of statements:............................................................................................................................2
2. Flow control statements......................................................................................................................2
3. If statement formats............................................................................................................................3
4. The range function...............................................................................................................................4
5. Operators in and not in........................................................................................................................5
6. For loop...............................................................................................................................................6
7. While loop...........................................................................................................................................8
8. Jump Statements - Break and continue.............................................................................................10
1. Break with for loop........................................................................................................................10
2. Break with While loop...................................................................................................................11
3. Continue statement.......................................................................................................................12
9. Loop else statement..........................................................................................................................13
10. Nested loop...................................................................................................................................14
1 Swati Jain
Chapter 9 Flow of Control
1.Types of statements
- Empty Statement – pass is the statement does not do anything. Useful in instances where
syntax of the language requires presence of a statement.
- Simple Statement – Any statement of python is a simple statement.
- Compound statement – Group of statements inside a condition or loop.
2.Flow control statements
- Sequence – The regular flow of statements in python
- Selection – Use of if- else , if -elif – else statement in python
- Repetition Iteration (looping) - Use of for or while statement in python
2 Swati Jain
Chapter 9 Flow of Control
3.If statement formats
- If statement
- If – else statement
- If – elif – else statement
3 Swati Jain
Chapter 9 Flow of Control
4.The range function
Syntax:
1. Range (lowerlimit , upperlimit)
2. Range (lowerlimit , upperlimit, stepvalue)
1. range (lowerlimit , upperlimit)
This is an arithmetic progression with a=lowerlimit , d=1 and last element is upperlimit-1
Example range (10,19) -> [10,11,12,13,14,15,16,17,18]
2. range (lowerlimit , upperlimit, stepvalue)
This is an arithmetic progression with a=lowerlimit , d=stepvalue and last element is upperlimit-1
Example range (10,19,2) -> [10,12,14,16,18]
Some more examples:
1. range(10) – [0,1,2,3,4,5,6,7,8,9]
2. range (5,10) – [5,6,7,8,9]
3. range (3,7) – [3,4,5,6]
4. range (9,3,-1) – [9,8,7,6,5,4]
5. range (10,1,-2) – [10,8,6,4,2]
4 Swati Jain
Chapter 9 Flow of Control
5.Operators in and not in
Used to check the presence of an element inside strings , list, tuples and range function.
- 3 in [1,2,3,4] will return True
- 5 in [1,2,3,4] will return False
- ‘a’ in “Great” will return True
- “eat” in “Great” will return True
- a in range (1,11) will return True when 1<=a<11. a is in the range 1 – 11.
- b in range (1,8) will return True when 1<= b <8. b is in the range 1 – 8.
5 Swati Jain
Chapter 9 Flow of Control
6.For loop
for <variable> in <Sequence>
statement_to_repeat
Examples
1. for a in [1,4,7] :
print (a)
print (a*a)
a–1
output
1
1
a–4
output
4
16
a–7
output
7
49
2. for ch in “Book”:
print (ch, end=””)
output
Book
3. for val in range (3,8)
print (“The value is :“, val)
output
The value is :3
The value is :4
The value is :5
The value is :6
The value is :7
6 Swati Jain
Chapter 9 Flow of Control
7.While loop
1. It needs an assignment statement
2. It needs an iteration statement
3. Doesn’t work for tuples, list, strings.
4. Works for integers which can be iterated.
5. Works for condition statements also.
Syntax :-
While <logicalexpression>
Loop-body
While loop to print first n natural numbers:
N = int (input (“Enter the number:”))
I=1
While (I<=N)
Print (I)
I += 1
#end of while loop
For loop to print first n natural numbers:
N = int (input (“Enter the number:”))
For I in range (1,N)
Print (I)
#End of for loop
To replace a for loop with a while loop.
- Add an assignment statement.
- Add an increment statement.
7 Swati Jain
Chapter 9 Flow of Control
8 Swati Jain
Chapter 9 Flow of Control
Cases where while loop is necessary – perform a task till the user says “yes”
user_input=’y’
While (user_input == ‘y’) or (user_input == ‘Y’)
Print (“Work has been done”)
user_input = input(“Type Y/y to continue”)
#end of while loop
9 Swati Jain
Chapter 9 Flow of Control
8.Jump Statements - Break and continue
Break statement – The break statement terminates the loop it lies within. It enables to skip a part of the
code.
1. Break with for loop.
for i in range(5):
if i == 3:
break
print(i)
Here i goes from 0 to 4. In the if statement if breaks at 3.
Output
10 Swati Jain
Chapter 9 Flow of Control
2. Break with While loop
# program to find first 5 multiples of 6
i = 1
while i <= 10:
print('6 * ',(i), '=',6 * i)
if i >= 5:
break
i = i + 1
at i=5 the loop breaks with the break statement.
output
6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
11 Swati Jain
Chapter 9 Flow of Control
3. Continue statement
The continue statement forces the next iteration of the loop to continue, skipping any code in between.
Continue with for and while loop
12 Swati Jain
Chapter 9 Flow of Control
9.Loop else statement
Syntax : For – Else statement
For <Variable> in <Sequence> :
Statement 1
Statement 2
Else:
Statement 3
Syntax : While – else statement
While <test condition> :
Statement 1
Statement 2
Else:
Statement 3
1. In the above statement the for loop or while loop gets executed regularly and the else statement
is executed.
2. In case there is a break in the for or while loop else will not get executed.
13 Swati Jain
Chapter 9 Flow of Control
10. Nested loop
Cases where a loop contains another loop inside is called a nested loop.
Example:
x = [1, 2] Output:
y = [4, 5] 1 4
1 5
for i in x:
for j in y: 2 4
print(i, j)
2 5
11. Printing patterns using nested loop.
Printing special characters.
Example: 1. Print: Output:
For i in range (1,6) ----- This for loop decides the number of rows. *
**
For j in range (1,i+1) ----- This for loop decides the no. of items in a row
***
Print (‘*’,”end=””) ---- This is for printing the items. ****
*****
Print () – This print is for going on the new line after a row.
14 Swati Jain
Chapter 9 Flow of Control
15 Swati Jain
Chapter 9 Flow of Control
For i in range (1,6) ----- This for loop decides the number of rows. Output:
For j in range (1,i+1) ----- This for loop decides the no. of items in a row (1,2,3,4,5) 1
12
Print (j ,”end=””) ---- This is for printing the items.
123
Print () – This print is for going on the new line after a row. 1234
12345
For i in range (1,6) ----- This for loop decides the number of rows. - 5
For j in range (1, 7-i) ----- This for loop decides the no. of items in a row (5,4,3,2,1)
Print (j ,”end=””) ---- This is for printing the items. End=”” makes the numbers on the same line.
Print () – This print is for going on the new line after a row.
16 Swati Jain