What is Looping?
Looping means repeating a particular activity multiple times.
For example, if we want to print the number 1 three times, we can write:
print(1)
print(1)
print(1)
But what if we want to print it 300 times?
Writing the same statement 300 times is not convenient.
This is where looping becomes useful.
Example:
count = 0
while count < 5:
print(1)
count = count + 1
Output:
1
1
1
1
1
Remember: Looping is a choice. We should use a loop when it reduces our effort and makes the program easier to write.
Ground Rules for Learning Looping
These are some important guidelines to follow when solving looping problems:
- Never say "I don't know". Say "Let me try".
- Don't think about the entire output at once.
- Think about only the very next step.
- Introduce a variable whenever necessary.
- Move from micro to macro (small steps to bigger logic).
- Python supports while and for loops. It does not have a built-in do...while loop.
- Dry-run the program on paper before executing it.
- Every program has three parts: Input, Process and Output.
- Logic first, syntax next.
While Loop in Python
A while loop executes a block of code repeatedly as long as its condition is True.
Syntax:
while condition:
# Statements
# Update the variable
Example:
count = 0
while count < 5:
print(1, end=' ')
count = count + 1
Output:
1 1 1 1 1
How does it work?
- Initially, count = 0.
- Python checks whether count < 5.
- If the condition is True, it prints 1.
- The count increases by 1.
- Python checks the condition again.
- This continues until the condition becomes False.
Understanding Changes in a While Loop
Let's understand how changing the initial value, condition and increment affects the loop.
Example 1: Change the initial value to 1
count = 1
while count < 5:
print(1, end=' ')
count = count + 1
Output:
1 1 1 1
The loop executes 4 times because the count starts at 1 and stops before reaching 5.
Example 2: Change < to <=
count = 0
while count <= 5:
print(1, end=' ')
count = count + 1
Output:
1 1 1 1 1 1
The loop executes 6 times because the condition includes 5.
Example 3: Change the initial value to 5
count = 5
while count < 5:
print(1, end=' ')
count = count + 1
Output:
No output is displayed because the initial condition 5 < 5 is False.
Example 4: Change < to >
count = 0
while count > 5:
print(1, end=' ')
count = count + 1
Output:
No output is displayed because 0 > 5 is False.
Example 5: Change increment from 1 to 2
count = 0
while count < 5:
print(1, end=' ')
count = count + 2
Output:
1 1 1
The count values are:
0 → 2 → 4 → 6
When count becomes 6, the condition becomes False.
Example 6: Change increment to decrement
count = 0
while count < 5:
print(1, end=' ')
count = count - 1
Output:
This creates an infinite loop.
Why?
- Initially, count is 0.
- Then it becomes -1, -2, -3, and so on.
- Every negative number is less than 5.
- Therefore, the condition always remains True.
Important: Be careful when updating loop variables. An incorrect update can create an infinite loop.
Understanding the Three Parts of a Loop
A while loop generally has three important parts:
| Part | Purpose |
|---|---|
| Initialization | Assign the starting value |
| Condition | Decide whether the loop should continue |
| Update | Change the variable after each iteration |
Example:
count = 0 # Initialization
while count < 5: # Condition
print(count)
count = count + 1 # Update
Output:
0
1
2
3
Top comments (0)