Python Control Statements
In any programming language a program may execute sequentially, selectively or
iteratively. Every programming language provides constructs to support
Sequence, Selection and Iteration. In Python all these construct can broadly
categorized in 2 categories.
A.Conditional Control Construct
(Selection, Iteration)
B. Un- Conditional Control Construct
(pass, break, continue, exit(), quit())
Python have following types of control statements
1. Selection ( branching) Statement
Conditional Control
2. Iteration ( looping) Statement Statements
3. Jumping (break / continue)Statement
Un Conditional Control
Python Selection Statements Statements
Python have following types of selection statements
1. if statement
2. if else statement
3. Ladder if else statement (if-elif-else)
4. Nested if statement
Python If statements
This construct of python program consist of one if condition with one block
of statements. When condition becomes true then executes the block given
below it.
Syntax:
if ( condition):
…………………..
…………………..
…………………..
Flow Chart: it is a graphical
representation of steps an
Flowchart algorithm to solve a problem.
Example:
Age=int(input(“Enter Age: “)) If
( age>=18):
Print(“You are eligible for vote”)
If(age<0):
Print(“You entered Negative Number”)
Python if - else statements
This construct of python program consist of one if condition with two blocks.
When condition becomes true then executes the block given below it. If
condition evaluates result as false, it will executes the block given below else.
Syntax:
if ( condition):
…………………..
else:
…………………..
Flowchart
Example-1:
Age=int(input(“Enter Age: “))
if ( age>=18):
print(“You are eligible for vote”) else:
print(“You are not eligible for vote”)
Example-2:
N=int(input(“Enter Number: “)) if(n%2==0):
print(N,“ is Even Number”) Else:
print(N,“ is Odd Number”)
Python Ladder if else statements (if-elif-else)
This construct of python program consist of more than one if condition. When
first condition evaluates result as true then executes the block given below it.
If condition evaluates result as false, it transfer the control at else part to test
another condition. So, it is multi-decision making construct.
Syntax:
if ( condition-1):
…………………..
………………….. elif
(condition-2):
…………………..
…………………..
elif (condition-3):
…………………..
…………………..
else:
…………………..
…………………..
Example:
num=int(input(“Enter
Number: “)) If
( num>=0):
Print(“You entered positive number”) elif
( num<0):
Print(“You entered Negative number”) else:
Print(“You entered Zero ”)
Python Nested if statements
It is the construct where one if condition take part inside of other if condition.
This construct consist of more than one if condition. Block executes when
condition becomes false and next condition evaluates when first condition
became true.
So, it is also multi-decision making construct.
Syntax: FlowChart
if ( condition-1):
if (condition-2):
……………
……………
else:
……………
……………
else:
…………………..
…………………..
Example:
num=int(input(“Enter Number:
“)) If ( num<=0): if ( num<0):
Print(“You entered Negative number”)
else:
Print(“You entered Zero ”)
else:
Print(“You entered Positive number”)
Python Iteration Statements
The iteration (Looping) constructs mean to execute the block of statements
again and again depending upon the result of condition. This repetition of
statements continues till condition meets True result. As soon as condition
meets false result, the iteration stops.
Python supports following types of iteration statements
1. while
2. for
WHILE LOOP in Python
Definition:
A while loop in Python executes a block of code as long as a specified condition is
True. It checks the condition before each iteration.
Syntax:
while condition:
# code block to execute
Use indentation (typically 4 spaces) instead of braces {} like in JavaScript.
Flow:
1. Evaluate condition
2. If True, run the indented block
3. Repeat step 1
4. If False, exit the loop
Example:
i=0
while i < 5:
print("Number is:", i)
i += 1
Explanation:
Starts with i = 0
Prints numbers 0 through 4
Stops when i becomes 5
Common mistake:
If you forget i += 1, it causes an infinite loop.
Use Cases:
Reading user input until valid
Waiting for a specific event
Repeating tasks until a condition is met
2. FOR LOOP in Python
Definition:
In Python, a for loop iterates over a sequence (like a list, string, or range). It’s
commonly used with the range() function to loop a specific number of times.
Syntax:
for variable in sequence:
# code block
Using range():
for i in range(5):
print("i is:", i)
Explanation:
range(5) produces: 0, 1, 2, 3, 4
Loop runs 5 times with values from 0 to 4
Reverse loop:
for i in range(5, 0, -1):
print(i)
Looping over a list:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Use Cases:
Looping through items in a list or string
Repeating a task a known number of times
Useful when working with data structures