2. Python Flow Of Control
A programâs control flow is the order in which the programâs code executes. A Flow
Control Statement defines the flow of the execution of the Program. Control flow
statements are required to alter the flow of program execution based on the
conditions.
SEQUENTIAL :
SELECTION CONDITIONAL:
ITERATIVE / LOOPING:
It is classified into three categories:
3. Python Flow Of Control
SEQUENTIAL :
It to provide the order in which we execute our instructions.
Example: Program to display sum of two numbers.
A=10
B=12
sum=A+B
print(sum)
All the lines are in a
sequence
4. Python Flow Of Control
Decision-making statements : if, if..else, if..elif..else
SELECTION CONDITIONAL: Selection to allow decisions to be made.
5. Python Flow Of Control
Looping statements : for, while
Branching statements : break, continue
ITERATIVE / LOOPING:
Iteration to loop or repeat our instructions as many times as we
need.
6. Decision-making Statements or Conditional Statements
if Statement
if - else Statement
elif statements
The if statement is used to test a specific condition. If the condition is true, a block of
code or block of statements will be executed.
The if-else statement is similar to if statement except that, it also provides the block of
the code for the false case of the condition to be checked. If the condition provided in
the if statement is false, then the else statement will be executed.
In python, we have one more conditional statement called elif statements. elif
statement is used to check multiple conditions only if the given if condition false. Itâs
similar to an if-else statement and the only difference is that in else we will not check
the condition but in elif we will do check the condition.
7. Nested if-else statements
Nested if-else statements mean that an if statement or if-else statement is present inside
another if or if-else block. Python provides this feature as well, this in turn will help us to
check multiple conditions in a given program.
elif Ladder
It means a program which contains ladder of elif statements or elif statements which are
structured in the form of a ladder.
This statement is used to test multiple expressions
8. What is for loop in Python?
Python Loops or Iteration
for loops
There are two types of loops in Python
Loops can execute a block of code number of times until a certain condition is met.
while loops While Loop is used to repeat a block of code. Instead of running the
code block once, It executes the code block multiple times until a
certain condition is met
In Python, "for loops" are called iterators.ust like while loop, "For Loop"
is also used to repeat the program. But unlike while loop which
depends on condition true or false. "For Loop" depends on the
elements it has to iterate
9. So first we discuss Decision-making Statements
if Statement
if - else Statement
elif statements
Nested if-else statements
elif Ladder
10. if Statement
The if statement is used to test a specific condition. If the condition is true only, a block
of code or block of statements will be executed.
Syntax:
if condition or expression:
-----Statements-------
-----Statements-------
Example: Program to check number is less than 10 , if yes
then display message "Number is smaller than 10"
no=5
If no<10:
print(âNumber is smaller than 10â)
---Output----
Number is smaller than 10
This is indentation. Python relies on indentation (whitespace
at the beginning of a line) to define scope in the code
11. Let us understand the if condition indentation and scope with the help of another example:
Num = 5
if(Num < 10):
print(âNum is smaller than 10â)
print(âThis statements will always be executedâ)
---Output----
Number is smaller than 10
This statements will always be executed
Num = 15
if(Num < 10):
print(âNum is smaller than 10â)
print(âThis statements will always be executedâ)
---Output----
This statements will always be executed
Now in this case if we change the value of Num to 15, then if() condition not satisfied and
it not run the statement inside its scope , but last message will display it is because it not a
part of if condition , its out of its scope.
12. So in python indentation is very important to get a correct result.
If statement, without indentation (will raise an error)
a = 33
b = 200
if b > a:
print("b is greater than a")
For example , if we need to check that variable A is greater than B.
It display the error message , that
indentation missing.
a = 33
b = 200
if b > a:
print("b is greater than a")
Correct code is when we use proper indentation:
---Output----
B is greater than a
13. So now we know python indentation
Let take one more example: Write a program to accept the age of a person and check the
age is more than equal to 18, if yes than display âYou are valid to voteâ
age=int(input(âEnter the age:â))
if age>=18:
print(âYou are valid to voteâ)
---Output----
Enter the age: 19
You are valid to vote
14. Now we use the logical operator AND , OR with if condition
OR with if condition:
Now if you want to display message âYou select write numberâ if a user enter any one
number from the given list and the numbers are 1, 3, 5
no=int(input(âEnter the value[1 , 3 , 5]:â))
if (no==1 or no==3 or no == 5):
print(âYou selected write numberâ)
---Output----
Enter the value[1,3,5]: 5
You selected write number
15. Now we use the logical operator AND , OR with if condition
AND with if condition:
Write a program to check the number is between 10 to 40 , if yes then print message that
âYour value in range of 10-40â
no=int(input(âEnter the value:â))
if (no>=10 and no<=40):
print(âYou value in range of 10-40â)
---Output----
Enter the value: 15
Your value in range of 10-40
One important point the And is used for checking the range values. For Example:
16. How to use ifâs without else part in a program
Ifâs condition:
Write a program to accept a number and print the following message:
If no is 1 âyou selected 1â , if no is 2 âyou selected 2â , if no is 3 âyou selected 3â
no=int(input(âEnter the value:â))
if (no == 1):
print(âYou selected 1â) ---Output----
Enter the value: 2
Your selected 2
if (no == 2):
print(âYou selected 2â)
if (no == 3):
print(âYou selected 3â)