CBSE Class 11 CS (083) - Flow of Control

Flow of Control

Flow of control refers to the order in which statements are executed in a program. Python provides selection statements (if, if-else, if-elif-else) and iteration statements (for loop, while loop) to control program flow. This chapter covers all control structures with examples.

1. Introduction to Control Flow

The default flow of execution in a program is sequential, where statements are executed one after another. Control flow statements alter this default order.

Python has three types of control structures:

  • Sequential: Statements execute in order.
  • Selection (Decision Making): if, if-else, if-elif-else.
  • Iteration (Looping): for loop, while loop.
  • Jump Statements: break, continue, pass.

2. The if Statement

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

# Syntax
if condition:
statement(s)

# Example
age = 18
if age >= 18:
print("You are eligible to vote.")

3. The if-else Statement

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

number = int(input("Enter a number: "))
if number % 2 == 0:
print(number, "is even.")
else:
print(number, "is odd.")

4. The if-elif-else Ladder

Used when there are multiple conditions to check. Only the first True condition's block executes.

marks = int(input("Enter marks: "))
if marks >= 90:
grade = "A+"
elif marks >= 75:
grade = "A"
elif marks >= 60:
grade = "B"
elif marks >= 45:
grade = "C"
elif marks >= 33:
grade = "D"
else:
grade = "F"
print("Grade:", grade)

5. Nested if Statements

An if statement inside another if statement is called nesting.

num = int(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Number is zero.")
else:
print("Number is positive.")
else:
print("Number is negative.")

6. The for Loop

The for loop iterates over a sequence (list, tuple, string, range) or any iterable object.

# Iterating over a string
for char in "Python":
print(char)

# Iterating over a list
for fruit in ["apple", "banana", "cherry"]:
print(fruit)

# Iterating with index using enumerate
for index, fruit in enumerate(["apple", "banana", "cherry"]):
print(index, fruit)

7. The range() Function

The range() function generates a sequence of numbers. It is commonly used with for loops.

# range(stop) - from 0 to stop-1
for i in range(5):
print(i) # 0, 1, 2, 3, 4

# range(start, stop)
for i in range(2, 7):
print(i) # 2, 3, 4, 5, 6

# range(start, stop, step)
for i in range(1, 10, 2):
print(i) # 1, 3, 5, 7, 9

# Reverse order
for i in range(10, 0, -1):
print(i) # 10, 9, 8, ..., 1

8. The while Loop

The while loop executes a block of code as long as a condition is True.

# Syntax
while condition:
statement(s)

# Example: Print 1 to 5
count = 1
while count <= 5:
print(count)
count += 1

Important: Ensure the loop condition eventually becomes False to avoid infinite loops.

9. The break Statement

The break statement terminates the loop immediately and transfers control to the statement after the loop.

for i in range(1, 10):
if i == 5:
break
print(i) # Prints: 1, 2, 3, 4

10. The continue Statement

The continue statement skips the current iteration and moves to the next iteration of the loop.

for i in range(1, 6):
if i == 3:
continue
print(i) # Prints: 1, 2, 4, 5

11. The pass Statement

The pass statement is a null operation (placeholder) that does nothing. It is used when a statement is syntactically required but no action is needed.

for i in range(5):
if i == 2:
pass # Will implement later
else:
print(i)

12. Nested Loops

A loop inside another loop is called a nested loop. The inner loop completes all iterations for each iteration of the outer loop.

# Multiplication table
for i in range(1, 4):
for j in range(1, 4):
print(f"{i} x {j} = {i*j}")
print() # Blank line after each table

13. Loop with else Clause

In Python, loops can have an else clause that executes when the loop completes normally (without break).

for i in range(5):
print(i)
else:
print("Loop completed normally.")

# With break, else does NOT execute
for i in range(5):
if i == 3:
break
print(i)
else:
print("This will not print.")

14. Pattern Printing using Nested Loops

# Right-angled triangle
for i in range(1, 6):
print("* " * i)

# Output:
# *
# * *
# * * *
# * * * *
# * * * * *

# Number pattern
for i in range(1, 6):
for j in range(1, i + 1):
print(j, end=" ")
print()

15. Revision Questions and Answers

Very Short Answer Questions

1. Name the two types of loops in Python.
for loop and while loop.

2. What does the break statement do?
It terminates the loop immediately.

3. What does the continue statement do?
It skips the current iteration and moves to the next iteration.

4. Which function generates a sequence of numbers?
range().

5. What is the purpose of the pass statement?
It is a placeholder that does nothing.

Short Answer Questions

1. Differentiate between for loop and while loop.
A for loop iterates over a sequence (definite iteration) and is used when the number of iterations is known. A while loop executes as long as a condition is True (indefinite iteration) and is used when the number of iterations depends on a condition.

2. Explain the if-elif-else ladder with an example.
It checks multiple conditions sequentially. The first True condition's block executes and the rest are skipped. Example: if marks>=90: grade="A+"; elif marks>=75: grade="A"; else: grade="B".

Long Answer Questions

1. Write a Python program to check if a number is prime or not using a loop.
num = int(input("Enter a number: ")); if num > 1: for i in range(2, int(num**0.5)+1): if num % i == 0: print("Not prime"); break else: print("Prime") else: print("Not prime")

2. Write a program to print the Fibonacci series up to n terms using a while loop.
n = int(input("Enter terms: ")); a, b = 0, 1; count = 0; while count < n: print(a, end=" "); a, b = b, a + b; count += 1