0% found this document useful (0 votes)
21 views39 pages

Python (Control Statements)

The document explains control statements in Python, which are categorized into decision making statements, iteration statements (loops), and jump statements. It details various types of decision making statements like if, if-else, and nested if-else, as well as iteration statements such as while and for loops. Additionally, it covers jump statements including break, continue, and pass, along with examples and syntax for each type.

Uploaded by

Khushi Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views39 pages

Python (Control Statements)

The document explains control statements in Python, which are categorized into decision making statements, iteration statements (loops), and jump statements. It details various types of decision making statements like if, if-else, and nested if-else, as well as iteration statements such as while and for loops. Additionally, it covers jump statements including break, continue, and pass, along with examples and syntax for each type.

Uploaded by

Khushi Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Control statements are used to control the flow of

execution depending upon the specified condition/logic.

There are three types of control statements.

1. Decision Making Statements


2. Iteration Statements (Loops)
3. Jump Statements
(break, continue, pass)
Decision making statement used to control the flow of
execution of program depending upon condition.

There are three types of decision making statement.

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)

4. Storing Conditions: To reduce complex and repetitive code of conditions of a


program, we can use named conditions (i.e., you can store conditions in a name
and then use that named conditional in the if statements.
 Named or stored conditional reduces repetition. It may also improve
processing speed.
e.g.
p,q,r=20,30,10
fval=p==20 and q==30 and r==10 # The conditions is given a name fval
if fval:
print("Condition is true") Ans)
else: Condition is true
print("Condition is false")
Iteration Statements (Loops)
Iteration statements(loop) are used to execute a block of
statements as long as the condition is true.
Loops statements are used when we need to run same code
again and again.
Python Iteration (Loops) statements are of three types :-

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

for <variable> in <sequence>: while <test condition(s)>:


statement(s) statement(s)
else: else:
statement(s) statement(s)
The else clause of a Python loop executes when the loop
terminates normally, i.e., when test condition results into
false for a while loop or for loop has executed for the last
value in the sequence; not when the break statement
terminates the loop.
e.g. for with else statement
for m in range(1,6): for m in range(1,6):
print(m) print(m)
else: 1 if(m%3==0):
2 break
print(m*m) 3
1
print(m) 4 else: 2
5
print("STOP") 25 print(m*m) 3
5 print(m) 3
STOP
print("STOP") STOP
e.g. while with else statement

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

You might also like