CBSETest.comby Bimal Publications

Need help with Flow of Control?

Practice Tests
Class 11 · Computer Science NCERT Class 11 Computer Science · Ch. 65 min read · 15 questions

Flow of Control

Computer Science

Flow of Control

Flow of Control in Python

By default, Python executes statements sequentially from top to bottom. However, real programs need to make decisions and repeat tasks. Flow of control statements let us control the order in which statements execute, using conditional statements and loops.

Conditional Statements

Conditional statements execute a block of code only when a specified condition is True.

if statement:
if condition:
statement(s)

if-else statement:
if condition:
statement(s) # runs if condition is True
else:
statement(s) # runs if condition is False

if-elif-else (ladder):
if condition1:
block1
elif condition2:
block2
elif condition3:
block3
else:
defaultblock

Python uses indentation (spaces/tabs) to define code blocks — there are no curly braces {}. Consistent indentation is mandatory; incorrect indentation causes an IndentationError.

Nested if Statements

An if statement placed inside another if block is called a nested if. Used when a secondary condition needs to be checked only after the primary condition is True.

Loops

Loops repeat a block of code multiple times, avoiding repetition.

while loop: Repeats as long as a condition remains True.
while condition:
body
Caution: If the condition never becomes False, the result is an infinite loop.

for loop: Iterates over a sequence (list, string, range).
for variable in sequence:
body

  • range() function: Generates a sequence of numbers.
  • range(n) → 0, 1, 2, ..., n-1
  • range(start, stop) → start to stop-1
  • range(start, stop, step) → with step increment

Loop Control Statements

  • break: Immediately exits the loop.
  • continue: Skips the rest of the current iteration and moves to the next.
  • pass: A placeholder; does nothing (used when a block is syntactically required but no code is needed).

else with loops: Python's loop can have an optional else block that executes when the loop completes normally (i.e., not terminated by break).

Worked Examples

Example 1

Write Python code to check if a number is positive, negative, or zero.
num = int(input("Enter a number: "))
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")

Example 2

Print the multiplication table of 5 using a for loop.
for i in range(1, 11):
print(5, "x", i, "=", 5 · i)
Produces: 5 x 1 = 5, 5 x 2 = 10, ... 5 x 10 = 50

Example 3

Use a while loop to calculate the sum of digits of 1534.
n = 1534
total = 0
while n > 0:
total += n % 10 # extract last digit
n = n // 10 # remove last digit
print("Sum of digits:", total) # Output: 13

Example 4

Demonstrate break — find the first multiple of 7 between 1 and 50.
for i in range(1, 51):
if i % 7 == 0:
print("First multiple of 7:", i)
break

Example 5

Demonstrate continue — print all numbers 1 to 10 except 5.
for i in range(1, 11):
if i == 5:
continue
print(i)

Example 6

Nested if — classify a student's grade based on marks.
marks = int(input("Enter marks: "))
if marks ≥ 33:
if marks ≥ 90:
print("Grade: A1")
elif marks ≥ 75:
print("Grade: A2")
else:
print("Grade: Pass")
else:
print("Fail")

Example 7

Use the for-else construct to check if a number is prime.
n = int(input("Enter number: "))
for i in range(2, n):
if n % i == 0:
print(n, "is not prime")
break
else:
print(n, "is prime")
The else block executes only if no break was triggered.

Common mistakes

> A very common error is using = instead of == inside an if condition. x = 5 assigns 5 to x; x == 5 compares x to 5. Another pitfall is the off-by-one error with range(): range(1, 11) gives 1 through 10, NOT 11. Also, remember that a missing or wrong indentation level causes an IndentationError and must be fixed before the program can run.

Summary

Python's flow of control uses conditional statements (if, if-else, if-elif-else) and loops (while, for) to alter sequential execution. Indentation defines code blocks. range() generates number sequences for for loops. break exits a loop early; continue skips the current iteration; pass is a no-op placeholder. The optional else block on a loop runs only when the loop ends without a break.

Practice Problems

15 questions with instant feedback.

Question 1 of 15Score 0

What will range(1, 5) generate in Python?