Upgrade to Pro

Python Control Flow

Control flow in Python refers to the order in which individual statements, instructions, or function calls are executed or evaluated. Python provides several constructs to control the flow of execution, including conditional statements, looping statements, and loop control statements. Below is an explanation of each with examples.


1. Conditional Statements (if, elif, else)

Conditional statements allow a program to execute certain pieces of code based on whether a given condition evaluates to True or False.

1.1 if Statement

The if statement evaluates a condition, and if it is True, executes the block of code inside it.

Example:

age = 18
if age >= 18:
    print("You are eligible to vote.")

Output:

You are eligible to vote.

Here, since age is 18, which is greater than or equal to 18, the statement inside if is executed.


1.2 if-else Statement

The else statement runs when the if condition evaluates to False.

Example:

age = 16
if age >= 18:
    print("You are eligible to vote.")
else:
    print("You are not eligible to vote.")

Output:

You are not eligible to vote.

Since age is 16 (less than 18), the else block is executed.


1.3 if-elif-else Statement

The elif statement allows for multiple conditions to be checked sequentially. It is useful when there are more than two possibilities.

Example:

marks = 85

if marks >= 90:
    print("Grade: A")
elif marks >= 80:
    print("Grade: B")
elif marks >= 70:
    print("Grade: C")
else:
    print("Grade: F")

Output:

Grade: B

Here, marks is 85, so the second condition (marks >= 80) is True, and the corresponding block is executed.


2. Looping Statements (for, while)

Loops allow us to execute a block of code multiple times.


2.1 for Loop

A for loop is used to iterate over sequences such as lists, tuples, dictionaries, and strings.

Example:

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

Output:

apple
banana
cherry

Here, the loop iterates over each item in the fruits list and prints it.

Using range() in a for loop:

for i in range(1, 6):  # Iterates from 1 to 5
    print(i)

Output:

1
2
3
4
5

The range(1, 6) generates numbers from 1 to 5.


2.2 while Loop

A while loop executes as long as the given condition is True.

Example:

count = 1
while count <= 5:
    print(count)
    count += 1

Output:

1
2
3
4
5

Here, the loop runs until count becomes greater than 5.


3. Loop Control Statements (break, continue, pass)

Loop control statements modify the execution of loops.


3.1 break Statement

The break statement exits the loop immediately.

Example:

for num in range(1, 10):
    if num == 5:
        break
    print(num)

Output:

1
2
3
4

The loop stops when num reaches 5.


3.2 continue Statement

The continue statement skips the current iteration and moves to the next.

Example:

for num in range(1, 6):
    if num == 3:
        continue
    print(num)

Output:

1
2
4
5

The number 3 is skipped.


3.3 pass Statement

The pass statement is a placeholder and does nothing. It is useful for writing code that will be implemented later.

Example:

for num in range(1, 6):
    if num == 3:
        pass  # Placeholder for future code
    print(num)

Output:

1
2
3
4
5

The pass statement allows the loop to continue normally.


Conclusion

  • Conditional statements (if, elif, else) help in decision-making.
  • Loops (for, while) are used for iteration.
  • Loop control statements (break, continue, pass) modify loop execution.

These concepts are fundamental to controlling the flow of a Python program efficiently! 

Flowise Tech https://flowisetech.com