Nearby lessons

37 of 159

Python - Flow Control

📌 What You Will Learn
  • Define flow control and its role in program execution
  • Identify the three types of flow control statements
  • Distinguish if, if-else, if-elif and if-elif-else conditional statements
  • Write programs that find the biggest and smallest of three numbers
  • Convert a digit to its word form using an if-elif-else ladder

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:

  • if
  • if-elif
  • if-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 the if block are executed.
  • If the condition is False, the complete block is skipped.

Example 1 - if Statement

Loading editor…

Example 2 - Favourite Brand

Loading editor…

if with Multiple Statements

An if block can contain multiple statements.

Example - Multiple Statements

Loading editor…

2. if-else Statement

The if-else statement is used when there are two possible actions.

  • If the condition is True, the if block is executed.
  • If the condition is False, the else block is executed.

Syntax:

if condition:
    Action1
else:
    Action2
        

Example 1 - if-else

Loading editor…

Example 2 - Biggest of Two Numbers

Loading editor…

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

Loading editor…

Example - if-elif

Loading editor…

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

Loading editor…

Example - if-elif-else

Loading editor…

Program - Biggest of Three Numbers

Loading editor…

Program - Smallest of Three Numbers

Loading editor…

Program - Even or Odd Number

Loading editor…

Program - Check Number Between 1 and 100

Loading editor…

Program - Print Digit in Words

Loading editor…
📝 Key Takeaways
  • Flow control decides the order in which Python statements are executed
  • Conditional statements execute different blocks based on a condition
  • Only one matching block runs and the remaining conditions are skipped
  • The else block executes when none of the conditions are True
  • Python provides if, if-elif and if-elif-else conditional statements

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.