while loop

A while loop is a looping statement used to repeatedly execute a block of code as long as a specified condition remains True.

Unlike the for loop, the while loop is mainly used when we do not know exactly how many times the loop should repeat.

Explanation

  • A while loop repeats a block of code continuously until the condition becomes False.

    The loop first checks the condition.

    • If the condition is True → loop executes
    • If the condition is False → loop stops

Step-by-Step Explanation of the Flowchart

1. Loop Start

The program starts the loop.

Initialization usually happens before the loop begins.

Example:

count = 1

2. Condition Check

The diamond shape checks the condition.

Example:

count <= 5

Two possibilities occur:

3. Condition is True

If the condition is True:

  • The body of the while loop executes.
  • The statements inside the loop run.

Example:

print(count)

4. Return Back to Condition

After executing the loop body:

  • Control goes back to the condition.
  • The condition is checked again.

Usually the variable is updated.

Example:

count = count + 1

This process keeps repeating.

5. Condition is False

When the condition becomes False:

  • The loop stops.
  • Control moves outside the loop.

6. Loop End

The loop terminates completely.

The next statements after the loop will execute.


Real-Life Scenario

  • Enter password until it is correct
  • Playing a game until lives become 0
  • Filling water until the tank is full

 Loop runs until the condition becomes False

Syntax

initialization

while condition:
    statements
    increment/decrement

Explanation of Syntax

Part Meaning
Initialization Starting value
while Starts the loop
condition Checks True or False
statements Code repeated inside loop
increment/decrement Updates loop variable


Example 1

i = 1

while i <= 3:
    print("Hi")
    i += 1


Output

Hi
Hi
Hi


Example 2

num = 1
sum = 0

while num <= 5:
    sum += num
    num += 1

print(sum)


Output

15

 

Explanation of Example

  • Loop runs from 1 to 5
  • Adds values → 1+2+3+4+5 = 15

How While Loop Works

Step 1

Python first checks the condition.

Step 2

If the condition is True:

  • the loop block executes

Step 3

The variable is updated using increment or decrement.

Step 4

Python again checks the condition.

The process repeats until the condition becomes False.

Why while loop is Important

While loops are important because they help programs:

  • repeat tasks automatically
  • handle unknown repetitions
  • create interactive systems
  • process real-time conditions
  • build dynamic applications


Difference Between for Loop and while Loop

Feature for Loop while Loop
Use Fixed repetitions Condition-based
Easy Easy Medium
Risk Less Can cause infinite loop

Common Beginner Mistakes 

Mistake Problem
Forgetting increment/decrement Infinite loop
Wrong condition Incorrect output
Forgetting indentation Indentation error
Missing colon : Syntax error

➤ Summary

  • The while loop in Python is used to repeat a block of code as long as a condition remains True.

    Important concepts include:

    • initialization
    • condition checking
    • increment/decrement
    • infinite loops

Check your knowledge

Quickly verify what you've learned from this tutorial.

Question 1

What does a while loop do?

A while loop repeats code based on a condition.

Question 2

When does a while loop stop?

Loop stops when condition becomes False.

Question 3

What is the output?

i = 1

while i <= 2:
    print(i)
    i += 1

Loop runs for 1 and 2.

Question 4

What happens if variable is not updated?

Without update, condition stays True → infinite loop.

Question 5

What is the output?

x = 2

while x < 6:
    print(x)
    x += 2

Loop runs for 2 and 4; stops before 6.

Congratulations!

You've successfully mastered the knowledge check for "while loop."

For more questions and practice, click the link below:

Practice More Questions
Previous Topic for Loop Next Topic Control Flow & Data Structures