0% found this document useful (0 votes)
19 views12 pages

Python Control Structures Explained

This document provides an overview of control structures in Python, focusing on conditional statements and loops. It covers various types of conditional statements such as if, elif, and else, as well as logical operators and indentation. Additionally, it explains iteration using while and for loops, including the use of break, continue, and pass statements.

Uploaded by

RamaNand Singh
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)
19 views12 pages

Python Control Structures Explained

This document provides an overview of control structures in Python, focusing on conditional statements and loops. It covers various types of conditional statements such as if, elif, and else, as well as logical operators and indentation. Additionally, it explains iteration using while and for loops, including the use of break, continue, and pass statements.

Uploaded by

RamaNand Singh
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

13-Nov-25

CONTROL By Gaurav Omee, MCA, DCL


STRUCTURES IN IDI’s Blended Learning
Specialist
PYTHON Core Faculty (IS), SAI India.

FLOW OF
CONTROL
IN
PROGRAMS

Image source: [Link]

1
13-Nov-25

If

Types of If else
conditional
statement If elif else

Nested if else
3

Before
• We need to know the
getting into • Logical Operators and
conditional • Indentation
statements

2
13-Nov-25

Equals: a == b

Different Not Equals: a != b

Logical Less than: a < b


operators Less than or equal to: a <= b
used in Greater than: a > b
Python
Greater than or equal to: a >= b

Indentation
• Python relies on indentation (whitespace at the beginning of a line)
to define scope in the code. Other programming languages often use
curly-brackets for this purpose.
• Eg
• a = 33
b = 200
if b > a:
print("b is greater than a") # you will get an error

• #because print has not been given indentation. The correct


#format should be like below
• a = 33
b = 200
if b > a:
print("b is greater than a")

3
13-Nov-25

if:
• If is used when a simple code of block is to be performed.
• Here the condition mentioned holds true then the code of
the block runs otherwise not.
• Syntax :
if condition:
# Statements to execute if condition is true
• Example :
if 15 > 7:
print("15 greater than 7")
Output:
print("Program ended") 15 greater than 7
Program ended

Notice the indentation used in above example python code


7

elif:
• The elif keyword is Python's way of saying "if the
previous conditions were not true, then try this
condition“
• E.g.
• a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")

4
13-Nov-25

else:
• The else keyword catches anything which isn't caught
by the preceding conditions.
• Eg:
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")

And
• The and keyword is a logical operator, and is used to
combine conditional statements:
• Eg:
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")

10

5
13-Nov-25

Or
• The or keyword is a logical operator, and is used to
combine conditional statements.
• Eg
a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")

11

Not
• The not keyword is a logical operator, and is used to
reverse the result of the conditional statement:
• E.g.
a = 33
b = 200
if not a > b:
print("a is NOT greater than b")

12

6
13-Nov-25

Nested if
• You can have if statements inside if statements, this is
called nested if statements.
• E.g.
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")

13

The Pass statement


• if statements cannot be empty, but if for some reason you
have an if statement with no content, put in the pass
statement to avoid getting an error.
• Eg
a = 33
b = 200

if b > a:
pass

14

7
13-Nov-25

ITERATION
(LOOP)

15

Loops in Python Programming


Language

WHILE LOOP FOR LOOP

16

8
13-Nov-25

While Loop
• LOOP statements let you execute a sequence of statements multiple times.

• Until a specified criterion is true, the block of statements will be


continuously executed in while loop.

• As soon as the condition gives “False” output, the control will pass to the
next line that is out of the loop boundary.

• It’s very important for programmer to write a Loop in way that it must be
false some time otherwise your programme will go in infinite loop (never
ending stage).

17

Syntax for While Loop


while expression:
statement(s)
Example:
# Print iCISA
# Guess, How many times iCISA will get printed
count = 0
while (count < 3):
count = count+1
Output:
print(“iCISA") iCISA
iCISA
iCISA

18

9
13-Nov-25

For Loop
• Mainly used when a set of codes needs to be repeated for certain
number of times.
• The for-loop is usually used with an iterable object such as a list or a
range.

19

Syntax for For Loop


for iterator_var in sequence:
statements(s)

In place of iterator variable, generally we give small i


In place of sequence, you may give any list or range. (Note: in
Python any word is also like a list, so you may give any word also in
place of sequence. Example on next slide.

20

10
13-Nov-25

The Pass statement


• Whenever you are writing a Loop, there must be some statements
inside the loop.
• You cannot keep the statement portion empty. but if for some reason
you don’t have any statement then you can use pass to avoid getting
an error.
• Remember, we used pass in if statement also, for the same reason.
• Example of pass in For loop:

# An empty loop
# Guess, what will be output ?
for letter in “gauravkumaromee”:
Output
pass
e
print(letter)
(The last letter will be stored in letter after running the
loop till whole word gaurakumaromee. Bcoz pass
statement will allow to execute the loop till end.)

21

The break statement


• This is also a loop control statement. As soon as Python compiler
encounters break inside any loop, the programme controls comes out
of the loop.
• Even if the loop condition is true, break will forcefully move the
programme control out of the loop.
• Example:

#Guess the output #Output will be


for i in range(5): 0
if i == 3: 1
break 2
print(i)

22

11
13-Nov-25

The continue statement


• The continue statement skips the current iteration of the loop and
jump to the next iteration.
• It means it returns the control to the beginning of the loop.
• Example:

#Guess the output


#Output will be
for i in range(5):
0
if i == 3:
1
continue
2
print(i)
4

23

Thank you !

24

12

You might also like