Unit 2 ControlFlowStatements
Unit 2 ControlFlowStatements
1. Conditional statements
2. Iterative statements
3. Transfer statements
Control flow statements
Control flow statements
● Conditional statement:-
● The conditional statement are depending on whether a given condition is true or
false.
● You are execute different blocks of codes depending on the outcome of a condition.
● Condition statements always evaluate to either true or false
● Types of conditional statements:-
1. if statements
2. If-else
3. If-elif-else
4. Nested if-else
Control flow statements
● Iterative statements:- iterative statement allow us to execute a block of
code repeatedly as long as the condition is true.
● We also call it a loop statements
● Python provides us to the following two loop statement to perform
Types of a iterative statements:-
1. For loop
2. While loop
Control flow statements
● Transfer statement:- in python transfer statement are used to
alter(stop) the program way of execution.
● The types of transfer statement:-
1. Break statement
2. Continue statement
3. Pass statement
Conditional statement
● If statement in python:-
In control statement the if statement is the simplest form.it takes a condition and evaluate
to either True or False
If the condition is true then the true block of code will be executed and if condition is false
then the block of code is skipped and controller moves the next block of code like else
block.
Syntax of if statement:-
If condition:
statement 1
statement 2
statement n
Conditional statement
● Flow chart of if else statement:-
If else statement
num=5
if num >=0:
print(“number is positive”)
else:
print(“number is negative”)
If else statement
password=input(“Enter password:-)
If password==“Hello@5656”:
print(“correct password”)
else:
print(“invalid password”)
If-elif-else
● Flowchart of if elif else:-
If-elif-else
If condition-1:
statement 1
elif condition-2:
Statement 2
elif condition-3:
statement 3
else:
statements
Syntax:
if condition 1:
# Code block to run if condition 1 is true.
elif condition 2:
# Code block to run if condition1 is false and condition 2 is true.
elif condition 3:
# Code block to run if condition1 and condition2 are both false, and condition 3 is true.
elif condition 4:
# Code block to run if condition1, condition2, and condition 3 are all false, and condition 4
is true.
# Add more elif statements as needed
else:
# Code block to run if all conditions (if and elif) are false.
If-elif-else
num=3
If num >0:
print(“positive”)
elif num==0:
print(“zero”)
else:
print(“negative number”)
If-elif-else
► for I in range(1,10):
print(I,end=“”)
► for I in range(1,10,2):
print(I,end=“”)
For loop with range function
for i in range(--):
if condition:
statements
else:
statements
If else in for loop
for i in range(1,11):
if i%2==0:
print(“even number:-”,i)
else:
print(“odd number:-”,i)
else in for loop
● Python allows us to use the else statement with the for loop .
● The for loop with an else block in Python will execute the else block when the loop completes all
iterations normally (i.e., it does not terminate due to a break statement).
● Example of else statement of for loop.
For loop
● Fibonacci sequence:-
a,b=0,1
print(“Fibonacci sequence:”)
for i in range(10):
print(a,end=“ ”)
c=a+b
a=b
b=c
While loop
i=1
while i<5:
print(i)
i=i+1
Using else with while loop
● Python allows us to use the else statement with the while loop also.
The else block is executed when the condition given in the while
statement becomes false.
● Like for loop if the while loop is broken using break statement then
the else block will not be executed and the statement present after
else block will be executed.
● The else statement is optional to use with the while loop .
While loop
While loop with else statement:
While loop
i=1
num=0
number=int(input(“Enter the number:-”))
while i<=10:
print(number,’x’,i,”=”,number*i)
i=i+1
If else in while loop
● In python condition statements act depending on whether a given condition is true or false.
● You can execute different block of codes depending on the outcome condition.
● Thee if else statements always evaluate the true or false.
● We use the if else statement in the loop when condition iteration is needed if the condition is
true then the statement inside the if block will execute otherwise the else block will execute.
● Syntax of if else statement:-
If condition:
statements
else:
statements
If else with while loop
● For loop :
● A for loop in Python iterates over a sequence (like lists, strings, or ranges). It's used when the
number of iterations is known or to iterate over each element in a collection.
● Example:
● while loop :
● A while loop runs as long as a specified condition is True. It’s used when the number of
iterations isn't predetermined, and depends on the condition.
Example:
LOOP CONTROL STATEMENT IN FOR
LOOP
● Loop control statement change the normal flow of execution.
● It is used when you want to exit a loop or skip a part of the loop based on
the given condition
● It is known as transfer statement
● Main three parts of transfer statement:-
● Break , continue and pass
Break statement for loop
Flow chart of break statement:-
Break statement for loop
● The break statement is used to terminate the loop. You can use the break
statement you want to stop the loop.
● You need to type the break inside the loop after the statement if you want to
break the loop.
● Break statement use to stop the loop execution
● This program for loop iterates over each number from a list.
● For example:-you are searching a specific email inside a file. You started
reading a file line by line using a loop. when you found an email you can
stop the loop using break statement.
Break statement for loop
● Example of break statement:-
● We will iterate numbers from a list using a for loop and if we found a number greater then 100
we will break the loop.
● Use the if condition to terminate the loop . If the condition evaluates to true then the loop will
terminate. Else loop will continue to work until the main loop condition is true.
Break statement for loop
number=[10,20,30,40,50]
for i in number:
if i>40:
break
print(“current number”,i) Output:
Break statement for loop
number=[1,4,7,8,15,20,35,45,55]
for i in number:
if i> 15:
break
else: Output:
print(i)
Note: break statement in Python is used to bring the control out of the loop when some
external condition is triggered. break statement is put inside the loop body . It terminates the
current loop, i.e., the loop in which it appears, and resumes execution at the next statement
immediately after the end of that loop. If the break statement is inside a nested loop, the break
will terminate the innermost loop.
break statement while loop
● We can use the break statements inside a while loop using the same approach.
Continue Statement
● Python Continue statement is a loop control statement that forces to execute the next iteration of
the loop while skipping the rest of the code inside the loop for the current iteration only, i.e. when
the continue statement is executed in the loop, the code inside the loop following the continue
statement will be skipped for the current iteration and the next iteration of the loop will begin.
Continue statement for loop
● The continue statement skips the current iteration of a loop and immediately jumps to the
next iteration.
● Use the continue statement when you want to jump to the next iteration of the loop
immediately.
● The interpreter found the continue statement inside the loop it skips the remaining code
and moves the next iteration.
● Example count the total number of ‘m’ in a string
● If statement checks the current character is m or not .if it is not m it continues to the next
iteration to check the following letter.
Continue statement for loop
Example1: Example : 2
One line if statement:
Example 1: Example 2:
One line if else statement:
a=2
b = 330
if a > b: print("a is greater than b")
else: print("b is greater than a")
can be substituted as in one line
print(“a is greater than b”) if a>b else print(“b is greater than a”)
Note:
● Python does not support Conditional(ternary) Operator ( _?_ :_ )
But Python support Conditional Expression.
You can also have multiple else statements on the same line:
Example
1. Some examples:
Some Examples:
2. What will be range of values for these calls if we pass to list function after this call.