Log In

Don't have an account? Sign up now

Lost Password?

Sign Up

Prev Next

MODULE 4: Control Flow Statements

Control flow statements decide how a program executes, which statements run, and how many times they run based on conditions.

1. Conditional Statements (if, elif, else)

Brief

Conditional statements are used to make decisions in a program based on conditions.

Detailed

  • if → Executes when condition is true
  • elif → Checks multiple conditions
  • else → Executes when no condition is true

Syntax

if condition:
    statements
elif condition:
    statements
else:
    statements

Example

age = 20

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

Multiple Conditions

marks = 75

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

2. Nested Conditions

Brief

Nested conditions are if statements inside another if statement.

Detailed

Used when a decision depends on multiple dependent conditions.

Example

username = "admin"
password = "1234"

if username == "admin":
    if password == "1234":
        print("Login successful")
    else:
        print("Wrong password")
else:
    print("Invalid username")

Real Use Case

  • Login systems
  • Eligibility checks
  • Permission-based access

3. Looping (for, while)

Brief

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


3.1 for Loop

Detailed

Used when the number of iterations is known.

Syntax

for variable in sequence:
    statements

Example

for i in range(5):
    print(i)

Iterating a List

names = ["Amit", "Rahul", "Sneha"]

for name in names:
    print(name)

3.2 while Loop

Detailed

Used when the number of iterations is unknown and depends on a condition.

Syntax

while condition:
    statements

Example

count = 1

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

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

4.1 break

Brief

Stops the loop immediately.

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

4.2 continue

Brief

Skips the current iteration and continues with the next.

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

4.3 pass

Brief

Acts as a placeholder where a statement is required syntactically.

for i in range(5):
    if i == 2:
        pass
    else:
        print(i)

5. Range Function

Brief

range() generates a sequence of numbers.

Syntax

range(start, stop, step)

Examples

range(5)        # 0 to 4
range(1, 6)     # 1 to 5
range(1, 10, 2) # Odd numbers

Using with Loop

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

6. Pattern Programs

Pattern programs improve logic building and loop mastery.


Star Pattern 1

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

Star Pattern 2

*****
****
***
**
*
for i in range(5, 0, -1):
    print("*" * i)

Number Pattern

1
12
123
1234
for i in range(1, 5):
    for j in range(1, i + 1):
        print(j, end="")
    print()

7. Real-World Logic Problems

1. Even or Odd

num = int(input("Enter number: "))

if num % 2 == 0:
    print("Even")
else:
    print("Odd")

2. ATM Withdrawal Logic

balance = 10000
withdraw = int(input("Enter amount: "))

if withdraw <= balance:
    balance -= withdraw
    print("Transaction successful")
else:
    print("Insufficient balance")

3. Login Attempts

attempts = 3

while attempts > 0:
    password = input("Enter password: ")
    if password == "admin":
        print("Login success")
        break
    attempts -= 1
else:
    print("Account locked")

4. Find Largest Number

a, b, c = 10, 25, 15

if a > b and a > c:
    print("A is largest")
elif b > c:
    print("B is largest")
else:
    print("C is largest")

Leave a Comment

    🚀 Join Common Jobs Pro — Referrals & Profile Visibility Join Now ×
    🔥