Skip to content
On this page

Control Flow

if-elif-else Statements:

The if-elif-else statement is used to implement conditional branching.

python
# if-elif-else statement
num = 10

if num > 0:
    print("Positive")
elif num == 0:
    print("Zero")
else:
    print("Negative")

while Loop:

The while loop is used to execute a block of code repeatedly until a condition becomes False.

python
# while loop
count = 1

while count <= 5:
    print("Iteration:", count)
    count += 1

for Loop:

The for loop is used to iterate over a sequence (e.g., list, tuple, string, etc.).

python
# for loop
numbers = [1, 2, 3, 4, 5]

for num in numbers:
    print(num)

break and continue Statements:

The break statement is used to exit a loop prematurely, and the continue statement is used to skip the current iteration and move to the next one.

python
# break and continue statements
numbers = [1, 2

, 3, 4, 5]

for num in numbers:
    if num == 3:
        break      # Exit the loop when num is 3
    elif num == 2:
        continue   # Skip the iteration when num is 2
    print(num)