DEV Community

Vasanthakumari N
Vasanthakumari N

Posted on

Python Day 10: Introduction to Looping and While Loop

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
Output:

1
1
1
1
1
Enter fullscreen mode Exit fullscreen mode

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:

  1. Never say "I don't know". Say "Let me try".
  2. Don't think about the entire output at once.
  3. Think about only the very next step.
  4. Introduce a variable whenever necessary.
  5. Move from micro to macro (small steps to bigger logic).
  6. Python supports while and for loops. It does not have a built-in do...while loop.
  7. Dry-run the program on paper before executing it.
  8. Every program has three parts: Input, Process and Output.
  9. 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
Enter fullscreen mode Exit fullscreen mode

Output:

1 1 1 1 1
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
Output:

1 1 1 1
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
Output:

1 1 1 1 1 1
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
Output:


Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
Output:


Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Output:

1 1 1
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode
Output:

This creates an infinite loop.

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
Output:

0
1
2
3
Enter fullscreen mode Exit fullscreen mode

Top comments (0)