0% found this document useful (0 votes)
7 views

Unit 2 ControlFlowStatements

Control flow statements in Python are categorized into conditional, iterative, and transfer statements. Conditional statements execute code blocks based on true or false evaluations, while iterative statements allow code execution to repeat under certain conditions. Transfer statements alter the flow of execution, including break, continue, and pass statements.

Uploaded by

shantanu270805
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Unit 2 ControlFlowStatements

Control flow statements in Python are categorized into conditional, iterative, and transfer statements. Conditional statements execute code blocks based on true or false evaluations, while iterative statements allow code execution to repeat under certain conditions. Transfer statements alter the flow of execution, including break, continue, and pass statements.

Uploaded by

shantanu270805
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 74

Control flow statements

● The control flow statements are divided into three parts:-

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

● Example of if else statement:-

num=5
if num >=0:
print(“number is positive”)
else:
print(“number is negative”)
If else statement

● Example of 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-elif-else condition statement has an elif blocks to chain multiple


conditions one after another.
● This is useful when you need to check multiple conditions.
● The if –elif-else we can make a tricky decision.
● The elif statement checks multiple conditions one by one and if the
condition fulfilled then execute the code.
If-elif-else

● Syntax of if elif else statement:-

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

Example of if elif else statement:-

num=3
If num >0:
print(“positive”)
elif num==0:
print(“zero”)
else:
print(“negative number”)
If-elif-else

● Example of if elif else statement:-


a=40
b=30
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”)
If –elif-else

● Example of if elif and else statement:-


a=50
If(a==20):
print(“value of a is 20”)
elif(a==30):
print(value of a is 30”)
elif (a==40)
print(“value of a is 40”)
else:
print(“value of a is greater than 40”)
Nested if else statement

● Nested if else statement:-in python nested if else statement is an if


statement inside the if else statement
● It is allowed in python to put any number of if statement in another if
statement.
● Indentation is the only way to different level nesting.
● We can have a if …elif …else statement inside another if elif ..else
statement this is called nesting in computer.
● Any number of these statement can be nested inside one another
Nested if else statement
Nested if else statement

● Syntax of the nested-if else:


If condition outside:
if condition inside:
statement of inside if
else:
statement of inside else:
statement of outside if
else:
outside else
Statement outside if block
Nested if else statement

● Example of nested if else statement:-


a=int(input(“Enter 1st value:-”)
b=int(input(“Enter 2st value:-”)
If a>=b:
if a == b:
print(a ‘and’, b, ‘are equal’)
else:
print(a, ‘is greater than’, b)
else:
Print(a, ‘is smaller then’,b)
For loops

● Syntax of for loop:-


for I in range:
statement 1
statement 2
statement n
● Syntax of for loop I in the iterating variable and the range specific how
many times the loop should run.
● For example if a list contains 10 number the for loop will execute 10 times
to print each number.
● Iteration of the loop the variable I get the current value.
For loops
● Flow chart of for loop:-
For loop with range()
For loop with range function
● The range function is used to generate the sequence of the numbers. If we pass
the range like (10) it will generate the sequence of the numbers from 0 to 9
● The syntax of the range function:-
range(start , stop , step size)
range(1,10,2)
● The start represents the beginning of the iteration.
● The stop represents that the loop will iterate till stop n-1 the range (1,10) will
generate numbers 1 to 9 iteration
● The step size is used to skip the specific numbers from the iteration it is optional
to use.by default the step size is 1 .
For loop with range function

► Example of range function of for loop:-


► for I in range(10):
print(I,end=“”)

► for I in range(1,10):
print(I,end=“”)

► for I in range(1,10,2):
print(I,end=“”)
For loop with range function

● Example of range function in for loop:-

a=int(input(“Enter any number:-”)


for i in range(1,11)
c=a*i
print(a,”x”,i”=“c)
Nested for loop in python
● Python allows us to nest any number of for loops inside a for loop .
the inner loop is executed n number of times for every iteration of
the outer loop
● Syntax of nested for loop
for I in range: #outer loop
for j in range : # inner loop
#statements
statements
Nested for loop in python

● Example of nested for loop:-

for i in range(0,6): Output:


for j in range(i):
print(“*”,end=“ “)
print() # This prints a new line
Nested for loop in python

● Example of nested for loop:-

for in range(1,6): Output:


for j in range(i):
print(i,end=” ”)
print()
If else in for loop

● If else statement use in for loop.


● If else is a conditional statement for example print student names
who got more than 80 percent.
● If else statement check the condition and if the condition is true it
execute the block of code present inside the if block and if the
condition is false it will execute the block of code present inside the
else block.
● If else condition is used inside the loop the interpreter check the if
condition in each iteration and the true condition is run and false
condition is skipped.
If else in for loop

● Syntax of if else statement in for loop:-

for i in range(--):
if condition:
statements
else:
statements
If else in for loop

● Example of if else in for loop:-

● for loop statement first iteration all the elements from 0 to 20


● Next the if statements checks the current number is even or
not
● Check the condition is true the number is even run the if block
and otherwise number is odd run the else block .
If else in for loop

● Example of 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

● In python the while loop statement repeatedly executes a code block


while a particular condition is true.
● The python while loop allows a part of the code to be executed until
the condition return false.
● While loop is known as pre-tested loop.
● It can be viewed as a repeating if statement.
● Syntax of while loop
while expression:
statements
While loop
While loop

● The while statement checks the condition. The condition must


return a Boolean value like True and False.

● Next if the condition evaluates to true the while statement executes


the statements present inside its block.

● The while statement continues checking the condition in each


iteration and keeps executing its block until the condition becomes
false.
While loop

● Example of 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

● Example of 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

● Example of while loop in if else:-

n=int(input(“enter the number:-”)


while n>0:
if n % 2==0:
print(n, “is a even number”)
else:
print(n, “is a odd number”)
n=n-1
Nested While Loop :
NOTE: For Loop Using Range ,String, List, Tuple, Set,Dictionary

● 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

● Example of break statement:-

number=[10,20,30,40,50]
for i in number:
if i>40:
break
print(“current number”,i) Output:
Break statement for loop

● Example of break statement:-

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

► Example of continue statement:-


continue loop in while loop

● Example of continue statements:-


Pass statement for loop

● The pass statement is null statement . nothing happens when the


statements
Is executed .it is used in empty functions or classes . when the interpreter
finds a pass statements in the program it returns no operation.

Syntax of pass statements:

for elements in sequence:


if condition:
pass
Pass statement for loop
Example of pass statement:-
Shorthand Control Flow Statement
● ShortHand if
If you have only one statement to execute, you can put it on the same line as the
if statement.
Syntax:
if condition : statement

Example1: Example : 2
One line if statement:

if a > b: print("a is greater than b")


ShortHand If ... Else
If you have only one statement to execute, one for if, and one for else, you can put it all on the same line:
Syntax :

statement_when_True if condition else statement_when_False

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.

Q1. >>> range(5)

Q2. >>> range(1,5)

Q3. >>> range(1,10,2)

Q4. >>> range(5,0,-1)

Q5. >>> range(5,0,-2)

Q6. >>> range(-4,4)

Q7. >>> range(-4,4,2)

Q8. >>> range(0,1)


Answers:
Q1. [0,1,2,3,4]
Q2. [1,2,3,4]
Q3. [1,3,5,7,9]
Q4. [5,4,3,2,1]
Q5. [5,3,1]
Q6. [-4,-3,-2,-1,0,1,2,3]
Q7. [-4,-2,0,2]
Q8. [0]
3.Write a program to test whether a number is divisible by 5 and 10 or by 5 or 10.
Ask user to enter the number.
4. Write a program to prompt a user to enter a day of the week. If the entered day of
the week is between 1 and 7 then display the respective name of the day.

day = int(input(“Enter the day of the week”))


if day == 1:
print(“Its Monday”)
if day == 2:
print(“Its Tuesday”)
if day == 3:
print(“Its Wednesday”)
if day == 4:
print(“Its Thursday”)
if day == 5:
print(“Its Friday”)
if day == 6:
print(“Its Saturday”)
if day == 7:
print(“Its Sunday”)
else:
print(“Sorry!!! Week contains only 7 days”)
5.

You might also like