Control Flow Practice Problems

12th February 2026 by Tanishq Chavan
Python C C++ Javascript React JS
Python Programming Language

Python Practice Set: Control Flow (Related Questions)

Test your understanding of if-else statements, for loops, while loops, and control statements like break, continue, and pass.


1. Conditional Logic (If-Else)

Q1: What is the difference between multiple if statements and an if-elif-else chain?

Answer: In multiple if statements, every condition is checked independently. In an if-elif-else chain, Python stops checking once it finds a True condition, which is more efficient and ensures only one block of code runs.

Q2: Predict the output of the following code:

x = 10
if x > 5:
    print("Greater than 5")
elif x > 8:
    print("Greater than 8")
else:
    print("Small")

Output: Greater than 5 (Note: Even though 10 > 8 is true, the elif is skipped because the first if was satisfied)

Q3: How do you write a "Shorthand If-Else" (Ternary Operator) in Python?

Answer: result = "Yes" if condition else "No"


2. Loops (For & While)

Q4: When should you prefer a for loop over a while loop?

Answer: Use a for loop when you know the number of iterations in advance (e.g., iterating over a list or a range). Use a while loop when the number of iterations depends on a condition being met (e.g., waiting for user input).

Q5: What does range(10, 0, -2) produce?

Answer: 10, 8, 6, 4, 2 (Starts at 10, ends before 0, decreasing by 2 each time).

Q6: What happens if a while loop condition never becomes False?

Answer: It creates an Infinite Loop, which can crash your program. You can usually stop it by pressing Ctrl + C in the terminal.


3. Control Statements (Break, Continue, Pass)

Q7: What is the specific use of the pass statement?

Answer: pass is a null operation. It does nothing. It is used as a placeholder when a statement is syntactically required but you don't want to execute any code (e.g., in an empty function or loop).

Q8: Explain the else clause used with a for loop.

Answer: The else block in a loop runs only if the loop completes naturally (i.e., it was not terminated by a break statement).


4. Coding Challenges

Task 1: Check if a number is Prime

Write a program that takes a number and checks if it is prime using a for loop and break.

num = int(input("Enter a number: "))
is_prime = True

if num < 2:
    is_prime = False
else:
    for i in range(2, int(num**0.5) + 1):
        if num % i == 0:
            is_prime = False
            break

if is_prime:
    print(f"{num} is a Prime Number")
else:
    print(f"{num} is not a Prime Number")

Task 2: Print a Triangle Pattern

Use nested loops to print the following pattern for n = 5:

*
**
***
****
*****

Solution:

n = 5
for i in range(1, n + 1):
    print("*" * i)

Task 3: The Guessing Game

Use a while loop to let a user guess a secret number (e.g., 7) until they get it right.

secret = 7
guess = -1

while guess != secret:
    guess = int(input("Guess the number: "))
    if guess < secret:
        print("Too low!")
    elif guess > secret:
        print("Too high!")

print("Congratulations! You guessed it.")

How did you feel about this post?

๐Ÿ˜ ๐Ÿ™‚ ๐Ÿ˜ ๐Ÿ˜• ๐Ÿ˜ก

Was this helpful?

๐Ÿ‘ ๐Ÿ‘Ž