Nearby lessons

48 of 108

Python - While Loop

What is a while Loop?

The while loop executes a group of statements as long as the given condition is True.

When the condition becomes False, the loop stops.

Syntax of while Loop

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

Flow of while Loop

  1. Check the condition.
  2. If the condition is True, execute the loop body.
  3. Again check the condition.
  4. Repeat until the condition becomes False.
  5. When the condition becomes False, control comes outside the loop.

Example 1 - Print "Hello" 10 Times

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello

Explanation

The variable i starts from 1.

After every iteration, its value increases by 1.

The loop stops when i becomes greater than 10.

Example 2 - Print Numbers from 1 to 10

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
1
2
3
4
5
6
7
8
9
10

Example 3 - Print Numbers from 1 to 20

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Example 4 - Print Even Numbers from 1 to 20

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
2
4
6
8
10
12
14
16
18
20

Explanation

The loop starts from 2.

Each time, the value increases by 2, so only even numbers are printed.

Example 5 - Print Odd Numbers from 1 to 20

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
1
3
5
7
9
11
13
15
17
19

Explanation

The loop starts from 1.

Each time, the value increases by 2, so only odd numbers are printed.

Example 6 - Print Numbers from 10 to 1

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10
9
8
7
6
5
4
3
2
1

Explanation

The loop starts from 10.

After every iteration, the value decreases by 1.

The loop stops when the value becomes less than 1.

Infinite while Loop

If the condition is always True, the loop runs forever.

Such a loop is called an Infinite Loop.

Example - Infinite while Loop

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Hello
Hello
Hello
Hello
...

(The loop continues until it is stopped manually.)

Why Does an Infinite Loop Occur?

An infinite loop happens when the loop condition never becomes False.

This usually happens when the loop variable is not updated correctly.

Important Points

  • Use the while loop when the number of iterations is not known in advance.
  • The condition is checked before executing the loop body.
  • If the condition is False initially, the loop body does not execute even once.
  • Always update the loop variable inside the loop.
  • Otherwise, the loop may become an infinite loop.

Quick Summary

Topic Description
while Loop Executes statements while the condition is True.
Loop Variable Controls the number of iterations.
Condition Checked before every iteration.
Infinite Loop Occurs when the condition never becomes False.

🧠 Test Your Knowledge

3 Questions

Progress: 0 / 3
Keep Going!Python - For Loop