Class 11 control statement While Loop notes

 The while loop is a control structure used in programming to repeat a block of code as long as a specified condition remains true. It is particularly useful when you don’t know beforehand how many times you need to repeat a block of code.



Syntax

while condition:
# Code to execute repeatedly
  • condition: This is an expression that is evaluated before each iteration of the loop. If the condition is true, the code block inside the loop is executed. If the condition is false, the loop stops.

How It Works

  1. Condition Evaluation: Before each iteration, the condition is checked.
  2. Code Execution: If the condition is true, the code inside the loop is executed.
  3. Repeat: After executing the code block, the condition is evaluated again. If it is still true, the loop continues; otherwise, the loop terminates.

Example 1: Basic While Loop

count = 0
while count < 5:
print("Count is:", count)
count += 1

Output:

Count is: 0
Count is: 1
Count is: 2
Count is: 3
Count is: 4

Explanation:

  • The loop starts with count = 0.
  • The condition count < 5 is true, so the loop prints the current value of count and then increments it by 1.
  • This process repeats until count reaches 5, at which point the condition count < 5 becomes false, and the loop terminates.

Example 2: Infinite Loop

If the condition never becomes false, the loop will run indefinitely. This is called an infinite loop.

while True:
print("This will run forever!")

Explanation:

  • The condition True is always true, so this loop never ends. You typically use break to exit such loops manually.

Example 3: Using break to Exit a Loop

The break statement can be used to exit a loop prematurely, even if the condition is still true.

count = 0
while count < 10:
print("Count is:", count)
if count == 5:
break
count += 1

Output:

Count is: 0
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5

Explanation:

  • The loop is designed to run until count reaches 10.
  • However, when count equals 5, the break statement is executed, causing the loop to terminate early.

Example 4: Using continue to Skip an Iteration

The continue statement skips the rest of the code inside the loop for the current iteration and moves on to the next iteration.

count = 0
while count < 5:
count += 1
if count == 3:
continue
print("Count is:", count)

Output:

Count is: 1
Count is: 2
Count is: 4
Count is: 5

Explanation:

  • When count equals 3, the continue statement is executed, causing the loop to skip the print statement for that iteration.

Common Pitfalls

  • Infinite Loops: Make sure the condition will eventually become false, or include a break statement to prevent the loop from running forever.

Key Points

  • A while loop is used when you want to repeat an action as long as a condition is true.
  • Ensure that the loop condition will eventually become false to avoid infinite loops.
  • Use break to exit a loop prematurely and continue to skip an iteration.

Understanding the while loop is essential for writing programs that need to repeat actions based on dynamic conditions, making it a fundamental concept in programming.

Post a Comment

0 Comments