Python (Control Statements)
Python (Control Statements)
1. if statements
2. if-else statements
3. Nested if-else statement
if statement in Python
Syntax:
if(condition):
statement [statements]
e.g. n = 2
if (n== 3): Output:
print(‘Hello ') Hello
print(‘students’) Students
print(‘Outside of if statement’)
Outside of if statement
Note:
To indicate a block of code in Python, you must indent each line of the block by the
same amount. In above e.g. both print statements are part of if condition because of
both are at same level indented but not the third print statement.
Types of if statements
1. if statements
Using logical operator in if statement
x,y=1,2
if(x==1 and y==2):
print(‘condition is true')
.
2 if-else Statements If-else statement executes some code if
the test expression is true (nonzero) and some other code if the
test expression is false.
Types of if statements
.
2 if-else Statements If-else statement executes some code
if the test expression is true (nonzero) and some other code
if the test expression is false.
Syntax:
e.g. a=10
if(condition): if(a < 100):
statements print(‘less than 100')
else: else:
statements print(‘more than equal 100')
OUTPUT
less than 100
Types of if statements
3. Nested if-else statement The nested if...else statement
allows you to check for multiple test expressions and
execute different codes for more than two conditions.
Syntax e.g.
if (condition): num=float(input("Enter a number: "))
statements if (num == 0):
elif (condition): print("Zero")
statements elif (num>0):
else: print("Positive number")
else:
statements print("Negative number")
Types of if statements pro2025,if(storing Condition)
1. While Loop
2. For Loop
3. Nested For /While Loops
range() Function Parameters :
start: Starting number of the sequence.
stop: Generate numbers up to, but not including this number.
step(Optional): Determines the increment between each numbers in
the sequence.
e.g.
for n in range(0,10,2):
print(n,end=" ")
Output
02468
For Loop :
It is used to iterate over items of any sequence, such as a
list or a string.
Syntax:
for val in sequence:
statements
e.g. for i in range(5,3,-1):
for n in range(3,6): print(i)
print(n, end=“ ”) Output
Output 5
345 4
For Loop :
For Loop continue Example programs with range() and
len() function
sub = [‘Eng', ‘Comp', ‘Math']
for index in range(len(sub)):
print (‘Subject name:', sub[index])
Nested for Loop :
A loop contains another loop(s) in its body.
In this case, the inner loop must terminate before the outer loop
e.g. for i in range(1,3):
for j in range(1,11):
print (i*j, end=' ')
print()
Output:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
While Loop :
It is used to execute a block of statement as long as a given
condition is true. And when the condition become false, the
control will come out of the loop.
The condition is checked every time at the beginning of the
loop.
x=1 1
Syntax: while (x <= 4): 2
while (condition): print(x) 3
statement [statements] x=x+1
4
pro2024(while)
Note:
The while loop is used when it is not possible to know in advance how
many times the loop will be executed, but the termination condition is
known.
The while loop is also known as top tested /entry-controlled / pre tested
loop.
d=5 d=50 d=5
while(d<=15): while(d<=15): while(d>=15):
print(d,end=" ") print(d,end=" ") print(d,end=" ")
d+=2 d+=2 d+=2
print(d) print(d) print(d)
5 7 9 11 13 15 17 50 No output
Example of Nested While Loop : Example of Nested While Loop :
d=1 d=1
while(d<=4): while(d<=5): 54321
c=1 1 c=5
12 5432
while(c<=d): while(c>=d):
print (c, end=' ') 123 print (c, end=' ') 543
c=c+1 1234 c=c-1 54
print() print() 5
d=d+1 d=d+1
Jump Statements:
Jump statements are used to transfer the program's control
from one location to another.
Basically these are used to alter the flow of a loop like :
to skip a part of a loop
terminate a loop
There are three types of jump statements used in python.
1.break
2.continue
3.pass
break statement:
A break statement terminates the execution of the loop
(in which it is written) and the control is transferred to the
statement immediately following the loop.
e.g. hello
for n in range(1,5):
bye bye
print("hello")
if(n%3==0):
hello
break bye bye
print("bye bye") hello
print("Thank U") Thank U
break (Dry Run)
Loop(5 rounds): Loop(5 rounds):
S1 S1 S1 S2 S3 S4
S2 S2
if(condition(s)): if(condition(s)): R1 1
break break x x
S3 S3 R2 2
S4 S4 Suppose
S5 condition is R3
S5 true only in
round 2 & 4
R4
R5
S5
x=1 1
1
PYTHON
while(x<=5): X=3
X=1
X=2 2
print(x) PYTHON
2
if x%3==0: 3 3
break END OF PROG.
print("PYTHON")
x=x+1
print("END OF PROG.")
continue statement:
It is used to skip all the remaining statements in the loop and
move controls back to the top of the loop.
e.g.
for n in range(1,5): hello
print("hello") bye bye
if(n%2==0):
hello
continue
print("bye bye") hello
print("Thank U") bye bye
hello
Thank U
continue (Dry Run)
Loop(5 rounds): Loop(5 rounds):
S1 S1 S1 S2 S3 S4
S2 S2
if(condition(s)): if(condition(s)): R1 1
continue continue x x
S3 S3 R2 2
S4 S4 Suppose
S5 S5 condition is R3 3
true only in
x x
round 2 & 4
R4 4
R5 5
S5
x=0 1
1
PYTHON
while(x<=4):
x=1
x=2
x=3 2 2
x=x+1 x=5
x=4
3
print(x) PYTHON
3
if x%2==0: 4 4
continue 5
print("PYTHON") PYTHON 5
print("END OF PROG.") END OF PROG.
pass statement:
This statement does nothing. It can be used when a statement
is required syntactically but the program requires no action
e.g.
for n in range(1,5): hello
print("hello") bye bye
if(n%2==0):
hello
pass # no work is assign/given
else: hello
print("bye bye") bye bye
print("Thank U") hello
Thank U
pass statement:
Use in loop :
while True:
pass # Busy-wait for keyboard interrupt (Ctrl+C) .
In function:
It makes a controller to pass by without executing any code.
e.g.
def myfun():
pass #if we don’t use pass here then error message will be shown
print('my program')
OUTPUT
My program
NOTE :
continue forces the loop to start at the next iteration while pass
means "there is no code to execute here" and will continue
through the remainder or the loop body.
e.g.
s='application'
OUTPUT
for n in s:
if(n=='a'): ppliction
pass
else:
print(n,end='')
Difference between continue and pass
for n in range(1,5): Welcome for n in range(1,5): Welcome
print(“Welcome") Good bye print(“Welcome") Good bye
if(n%2==0): if(n%2==0): Welcome
Welcome
continue pass
print("Good bye") Welcome Good bye
print("Good bye")
print("Thank U") Good bye print("Thank U") Welcome
Welcome Good bye
Thank U Welcome
Good bye
Thank U
Get outputs
for n in range(1,5): for n in range(1,5):
print(n,end=" ") print(n,end=" ")
if(n%2==0): if(n%2==0):
continue pass
print(n*n,end=" ") print(n*n,end=" ")
print("END") print("END")
OUTPUT:
OUTPUT:
Get outputs
for n in range(1,5): for n in range(1,5):
print(n,end=" ") print(n,end=" ")
if(n%2==0): if(n%2==0):
continue pass
print(n*n,end=" ") print(n*n,end=" ")
print("END") print("END")
OUTPUT:
OUTPUT: 1 1 2 4 3 9 4 16 END
1 1 2 3 9 4 END
Jump Statements (break, continue, pass)
Syntax Program
for i in range(1,11):
for val in seq:
if(i==3):
if (val== i): print("hello", end=' ')
break continue
if (val== j): if(i==8):
continue break
if(i==5): Output:
if (val== k): pass 1 2 hello 4 6 7
pass else:
print(i, end=' ');
while and for with else statement
Python loops have an optional else clause
m=1 m=1
while(m<=5): while(m<=5):
print(m) print(m)
m=m+1 1 if(m%3==0):
2 break
else: 3
print(m*m) 4 m=m+1
5 else: 1
36 print(m*m)
print(m) 6
2
print("END") END 3
print(m) 3
print("END") END