Page 501 - ComputerScience_Class_11
P. 501
Syntax:
for variable in sequence:
pass # Do nothing
Program 10: Write a program to print the numbers using pass statement.
Program 10.py
File Edit Format Run Options Window Help
for i in range(1, 6):
if i == 3:
pass
else:
print(i)
Output
1
2
4
5
Explanation: The loop iterates over numbers from 1 to 5. When i is equal to 3, the pass statement is executed, which
means no action is taken for that iteration. For other values of i, the number is printed.
The while Loop
The while loop in Python is a type of iterative statement that is used to repeat a block of code as long as a specified
condition is true. Unlike the for loop, which iterates over a predefined sequence, the while loop continues to run as
long as the condition remains true, making it useful when you don't know the number of iterations in advance.
Syntax:
while condition:
# Loop body
Where:
• condition: The expression that is checked before each iteration. If the condition evaluates to True, the loop
continues; if it evaluates to False, the loop stops.
• Loop body: The indented block of code that is executed as long as the condition is True.
Working of while Loop
The working of while loop is mentioned below:
1. The condition is checked.
2. If the condition is True, the body of the loop is executed.
3. After the body of the loop finishes, the condition is checked again.
4. If the condition is still True, the loop repeats.
5. If the condition becomes False, the loop stops and the program continues with the next statement after the loop.
Flow of Control 499

