Flow Control in Python

 Flow control in Python refers to the order in which statements are executed in a program. By default, Python executes code line by line from top to bottom.

But sometimes we need:

  • To make decisions

  • To repeat tasks

  • To skip certain statements

  • To stop execution

For this purpose, Python provides Flow Control Statements.

Flow control is mainly divided into:

  1. Conditional Statements

  2. Looping Statements

  3. Jumping Statements

Conditional Statements

Conditional statements help the program make decisions.

Python supports:

  • if

  • if-else

  • if-elif-else

  • Nested if

These statements check a condition and execute code based on whether the condition is True or False.

Example:

age = 18
if age >= 18:
print("Eligible to vote")

if Statement

The if statement executes a block of code only if the condition is True.

Syntax:

if condition:
statement

Example:

num = 10
if num > 0:
print("Positive number")

If the condition is false, nothing happens.

if-else Statement

The if-else statement executes one block if condition is True and another block if False.

Syntax:

if condition:
statement1
else:
statement2

Example:

num = -5
if num >= 0:
print("Positive")
else:
print("Negative")

if-elif-else Statement

Used when multiple conditions need to be checked.

Syntax:

if condition1:
statement1
elif condition2:
statement2
else:
statement3

Example:

marks = 75

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

Nested if Statement

An if inside another if is called nested if.

Example:

num = 10

if num > 0:
if num % 2 == 0:
print("Positive Even")

Nested if is useful when multiple related conditions must be checked.

Looping Statements

Loops are used to execute a block of code multiple times.

Python supports:

  1. for loop

  2. while loop

Loops reduce code repetition.

for Loop

The for loop is used to iterate over a sequence like list, tuple, string, or range.

Syntax:

for variable in sequence:
statement

Example:

for i in range(1, 6):
print(i)

Output:
1 2 3 4 5

while Loop

The while loop executes as long as the condition is True.

Syntax:

while condition:
statement

Example:

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

break Statement

The break statement stops the loop immediately.

Example:

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

Output:
1 2 3 4

continue Statement

The continue statement skips the current iteration and continues with the next iteration.

Example:

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

Output:
1 2 4 5

pass Statement

The pass statement does nothing. It is used as a placeholder.

Example:

for i in range(5):
pass

Conclusion

Flow control statements are essential in Python programming. They help in:

✔ Making decisions
✔ Repeating tasks
✔ Controlling execution flow
✔ Writing efficient programs

Without flow control, programs cannot make logical decisions or perform repeated tasks.

Comments

Popular posts from this blog

Functions in Python

Tkinter -Python

Python – Database Connection