Computer Science

Python while Loop | Notes, Syntax, Flowchart, Examples & Important Programs | CBSE Class 11 Computer Science

Class 11 · Computer Science

Python while Loop

The while loop is an iterative statement used to execute a block of code repeatedly as long as a given condition remains True. Unlike the for loop, which is generally used when the number of iterations is known, the while loop is preferred when the number of iterations is not known in advance.


Learning Objectives

  • Understand the concept of the while loop.
  • Learn the syntax and working of the while loop.
  • Write programs using the while loop.
  • Understand infinite loops and nested while loops.
  • Differentiate between for and while loops.

What is a while Loop?

A while loop repeatedly executes a block of statements as long as the specified condition evaluates to True. Before every iteration, Python checks the condition. If the condition becomes False, the loop terminates.

The while loop is also known as an entry-controlled loop because the condition is tested before entering the loop body.


Syntax

while condition:
    statements

Working of the while Loop

  1. The condition is evaluated.
  2. If the condition is True, the loop body executes.
  3. The control returns to the condition.
  4. The process continues until the condition becomes False.
  5. When the condition is False, the loop terminates.

Flowchart

        Start
          |
     Initialize Variable
          |
     Check Condition?
       /          \
    True          False
      |              |
Execute Statements   End
      |
Update Variable
      |
      +-------------+

Example 1: Print Numbers from 1 to 5

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

Output

1
2
3
4
5

Example 2: Print Even Numbers

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

Example 3: Print Odd Numbers

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

Example 4: Reverse Counting

i=10
while i>=1:
    print(i)
    i=i-1

Example 5: Multiplication Table

n=int(input("Enter a number: "))
i=1
while i<=10:
    print(n,"x",i,"=",n*i)
    i=i+1

Example 6: Sum of First 10 Natural Numbers

sum=0
i=1
while i<=10:
    sum=sum+i
    i=i+1
print("Sum =",sum)

Dry Run

Program

i=1
while i<=3:
    print(i)
    i=i+1
Iteration Value of i Condition Output
1 1 True 1
2 2 True 2
3 3 True 3
4 4 False Loop Stops

Infinite while Loop

If the condition never becomes False, the loop continues forever. Such a loop is called an Infinite Loop.

Example

while True:
    print("Welcome")

Explanation: Since the condition is always True, the loop never terminates unless interrupted manually.


Nested while Loop

A Nested while Loop means placing one while loop inside another while loop.

Example

i=1
while i<=3:
    j=1
    while j<=2:
        print(i,j)
        j=j+1
    i=i+1

Output

1 1
1 2
2 1
2 2
3 1
3 2

Real-Life Applications

  • ATM transactions until the user exits.
  • Menu-driven programs.
  • Password verification.
  • Reading sensor values continuously.
  • Online gaming loops.
  • Processing user input until a valid value is entered.
  • Inventory management systems.

Difference between for Loop and while Loop

Feature for Loop while Loop
Iterations Usually known Usually unknown
Condition Based on a sequence Based on a Boolean condition
Initialization Handled automatically Done manually
Updation Automatic Programmer must update

Common Programming Mistakes

1. Forgetting to Update the Variable

Wrong

i=1
while i<=5:
    print(i)

Explanation: This creates an infinite loop because i never changes.

2. Incorrect Indentation

Wrong

while i<=5:
print(i)

Correct

while i<=5:
    print(i)

3. Wrong Condition

Example

i=10
while i<=1:
    print(i)
    i=i-1

Explanation: The loop will never execute because the condition is initially False.


CBSE Important Programs

  1. Print numbers from 1 to N.
  2. Print numbers from N to 1.
  3. Print even numbers.
  4. Print odd numbers.
  5. Find the sum of first N natural numbers.
  6. Display multiplication table.
  7. Reverse counting.
  8. Print squares of numbers.
  9. Print cubes of numbers.
  10. Accept input until the user enters 0.

Viva Questions

  1. What is a while loop?
  2. Why is it called an entry-controlled loop?
  3. When should a while loop be preferred over a for loop?
  4. What is an infinite loop?
  5. How can an infinite loop be avoided?
  6. What is a nested while loop?
  7. Why is variable updation important?
  8. Can a while loop execute zero times?
  9. Can a while loop contain another loop?
  10. Give any two real-life applications of the while loop.

Exam Tips

  • Use a while loop when the number of iterations is not known.
  • Always update the loop variable inside the loop.
  • Maintain proper indentation.
  • Be careful with the loop condition to avoid infinite loops.
  • Trace the program manually to understand loop execution.

Quick Revision

  • The while loop repeats statements while the condition is True.
  • It is an entry-controlled loop.
  • The condition is checked before every iteration.
  • The loop variable must be updated manually.
  • If the condition never becomes False, an infinite loop occurs.