Control Flow: Conditional Blocks 1. Flow of Control
Control Flow: Conditional Blocks 1. Flow of Control
1. Flow of control
1. The order in which statements are executed is called the flow of control/execution
2. Execution always begins at the first statement of theprogram.
3. Statements are executed one at a time, in order, from top tobottom.
4. A program's control flow is the order in which the program's code executes. The
control flow of a Python program is regulated by conditional statements, loops,
and function calls.
5. When the flow of control of a program is changed based on some condition using
control statements, it is termed conditional flow of control.
>>> 5 == 5
True
>>> 5 == 6
False
True and False are special values that belong to the type bool; they are not strings:
>>>type(True)
<class 'bool'>
>>>type(False)
<class 'bool'>
x != y # x is not equal to y
Note:All expressions involving relational and logical operators will evaluate to either
true or false
3. Conditional statements
In Python, condition statements act depending on whether a given condition is true or false.
We can execute different blocks of codes depending on the outcome of a condition.
Condition statements always evaluate to either True or False.
1. if statement
2. if-else
3. if-elif-else
4. nested if-else
4. If statement in Python
If the condition is True, then the True block of code will be executed, and if the condition is
False, then the block of code is skipped, and The controller moves to the next line
Syntax of the if statement
if condition:
statement1
statement2
statement n
Let’s see the example of the if statement. In this example, we will calculate the square of a
number if it greater than 5
Example
number=6
if number >5:
# Calculate square
print(number * number)
print('Next lines of code')
Output
36
The if-else statement checks the condition and executes the if block of code when the
condition is True, and if the condition is False, it will execute the else block of code.
Syntax of the if-else statement
if condition:
statement1
else:
statement2
If the condition is True, then statement 1 will be executed If the condition is False, statement
2 will be executed. See the following flowchart for more detail.
Example
if password =="PYnative@#29":
print("Correct password")
else:
print("Incorrect Password")
Output 1:
Correct password
Output 2:
Enter password PYnative
Incorrect Password
With the help of if-elif-else we can make a tricky decision. The elif statement checks
multiple conditions one by one and if the condition fulfills, then executes that code.
Syntax of the if-elif-else statement:
if condition-1:
statement1
elif condition-2:
stetement2
elif condition-3:
stetement3
...
else:
statement
defuser_check(choice):
if choice ==1:
print("Admin")
elif choice ==2:
print("Editor")
elif choice ==3:
print("Guest")
else:
print("Wrong entry")
user_check(1)
user_check(2)
user_check(3)
user_check(4)
Output:
Admin
Editor
Guest
Wrong entry
Indentation is the only way to differentiate the level of nesting. The nested if-else is useful
when we want to make a series of decisions.
ifconditon_outer:
ifcondition_inner:
statement of inner if
else:
statement of inner else:
statementot outer if
else:
Outer else
statement outside if block
Output 1:
56 is greater than 15
Output 2:
29 is smaller than 78
8. Simple programs using Conditional statements