Nearby lessons
42 of 108Python - Flow Control
Introduction
Flow Control decides the order in which Python statements are executed during program execution.
It helps the program make decisions, repeat tasks, and control the execution flow.
Types of Flow Control
| Category | Statements |
|---|---|
| Conditional Statements | if, if-elif, if-elif-else |
| Transfer Statements | break, continue, pass |
| Iterative Statements | for, while |
Conditional Statements
Conditional statements execute different blocks of code based on a condition.
Python provides the following conditional statements:
ifif-elifif-elif-else
1. if Statement
The if statement executes a block of code only when the condition is True.
Syntax:
if condition:
statement
Or
if condition:
statement1
statement2
statement3
Important Note
- If the condition is
True, all statements inside theifblock are executed. - If the condition is
False, the complete block is skipped.
Example 1 - if Statement
main.py
1 Enter Name: Durga Hello Durga Good Morning Output 2 Enter Name: Ravi (No output because the condition is False.)
Example 2 - Favourite Brand
main.py
1 Enter Your Favourite Brand: RC It is Childrens Brand Output 2 Enter Your Favourite Brand: KF (No output because the condition is False.)
if with Multiple Statements
An if block can contain multiple statements.
Example - Multiple Statements
main.py
Enter Name: Durga Hello Durga Good Morning How are you?
2. if-else Statement
The if-else statement is used when there are two possible actions.
- If the condition is
True, theifblock is executed. - If the condition is
False, theelseblock is executed.
Syntax:
if condition:
Action1
else:
Action2
Example 1 - if-else
main.py
1 Enter Name: Durga Hello Durga Good Morning Output 2 Enter Name: Ravi Hello Guest Good Morning
Example 2 - Biggest of Two Numbers
main.py
1 Enter First Number: 10 Enter Second Number: 20 Biggest Number is: 20 Output 2 Enter First Number: 30 Enter Second Number: 15 Biggest Number is: 30
Conditional Statements Summary
| Statement | Description |
|---|---|
if |
Executes a block only when the condition is True. |
if-else |
Executes one of two blocks. |
Important Points
- Flow Control decides the order of statement execution.
- Python has Conditional, Transfer, and Iterative statements.
- The
ifstatement executes only when the condition is True. - The
if-elsestatement chooses one of two blocks. - Indentation is compulsory in Python.
3. if-elif Statement
When multiple conditions need to be checked, use the if-elif statement.
Python checks conditions from top to bottom.
Once a condition becomes True, the corresponding block is executed and the remaining conditions are skipped.
Syntax
main.py
No output captured.
Example - if-elif
main.py
1 Enter Your Favourite Brand: KF It is not that much kick Output 2 Enter Your Favourite Brand: FO Buy One Get Two Free
Important Note
Only one matching block is executed. After that, all remaining conditions are skipped.
4. if-elif-else Statement
The else block executes when none of the above conditions are True.
Syntax
main.py
No output captured.
Example - if-elif-else
main.py
1 Enter Your Favourite Brand: RC It is Children's Brand Output 2 Enter Your Favourite Brand: ABC Other Brands are not Recommended
Program - Biggest of Three Numbers
main.py
1 Enter First Number: 10 Enter Second Number: 20 Enter Third Number: 30 Biggest Number is: 30 Output 2 Enter First Number: 100 Enter Second Number: 20 Enter Third Number: 30 Biggest Number is: 100
Program - Smallest of Three Numbers
main.py
Enter First Number: 10 Enter Second Number: 20 Enter Third Number: 5 Smallest Number is: 5
Program - Even or Odd Number
main.py
1 Enter Number: 10 Even Number Output 2 Enter Number: 15 Odd Number
Program - Check Number Between 1 and 100
main.py
1 Enter Number: 50 The Number is between 1 and 100 Output 2 Enter Number: 150 The Number is not between 1 and 100
Program - Print Digit in Words
main.py
1 Enter Digit From 0 to 9: 7 SEVEN Output 2 Enter Digit From 0 to 9: 15 Please Enter Digit From 0 to 9
Quick Summary
| Statement | Description |
|---|---|
if | Executes when the condition is True. |
if-else | Executes one of two blocks. |
if-elif | Executes one block from multiple conditions. |
if-elif-else | Executes one matching block or the else block. |
Important Notes
elifmeans else if.- Use
elifto check multiple conditions. - Only the first matching condition is executed.
- The
elseblock is optional. - If no condition is True, the
elseblock executes. - Indentation is compulsory in Python.