While

Comprehensive study notes, diagrams, and exam preparation for While.

While

Definition

A while statement or while loop is a control structure that repeatedly executes a block of instructions as long as a given condition is true. The repetition stops when the condition becomes false.

In simple terms:

  • If the condition is true, the loop continues.
  • If the condition is false, the loop ends.

Example in pseudocode:

while condition is true
    execute statements

Example in Python:

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

This prints numbers from 1 to 5 because the loop keeps running while count <= 5 remains true.


Main Content

1. First Concept: While Loop Structure

  • The while loop has three essential parts:
  • a condition
  • a loop body
  • an update/control statement
  • The condition is checked before each repetition, so the loop may run many times or not at all.

A general structure looks like this:

initialize variable
while condition:
    statements
    update variable

Example:

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

Explanation:

  • i = 1 initializes the counter.
  • i <= 3 is the condition.
  • print("Hello") is the repeated action.
  • i += 1 changes the value so the condition eventually becomes false.

If the update is forgotten, the loop may continue forever. This is why understanding the structure of a while loop is essential.

2. Second Concept: Condition Checking and Loop Control

  • A while loop is called a pre-test loop because the condition is evaluated before executing the loop body.
  • The loop runs only if the condition evaluates to true at the start.
  • The condition must be carefully designed so the loop ends at the correct time.

Example:

number = 10
while number > 0:
    print(number)
    number -= 1

Here:

  • The loop starts with number = 10.
  • Each time, it prints the current value.
  • The value decreases by 1.
  • When number becomes 0, the condition number > 0 becomes false and the loop stops.

This concept is important because it shows how a program can make decisions repeatedly. The condition acts like a gate that decides whether the loop continues or not.

3. Third Concept: Infinite and Sentinel-Controlled While Loops

  • A while loop can become infinite if the condition never becomes false.
  • Infinite loops are usually a mistake, but they can also be intentional in programs that must keep running, such as servers or menus.
  • A sentinel-controlled loop uses a special value called a sentinel to stop the loop.

Example of a sentinel-controlled loop:

entry = input("Enter a name (type 'exit' to stop): ")
while entry != "exit":
    print("You entered:", entry)
    entry = input("Enter a name (type 'exit' to stop): ")

In this example:

  • The loop continues until the user types "exit".
  • "exit" is the sentinel value.

Diagram for how a while loop works:

Start
  |
  v
Check condition
  |
  +-- false --> Stop
  |
 true
  |
  v
Execute loop body
  |
  v
Update value
  |
  v
Go back to condition

This concept is especially useful when the exact number of repetitions is not known in advance.


Working / Process

1. Initialize the control variable

  • Before the loop begins, set a starting value.
  • This variable is often used in the condition.
  • Example: count = 1

2. Check the condition

  • The program checks whether the condition is true.
  • If true, the loop begins.
  • If false, the loop is skipped or ends.

3. Execute the loop body and update

  • Run the statements inside the loop.
  • Change the control variable so the condition can eventually become false.
  • Example: python count = 1 while count <= 5: print(count) count += 1

In this process, the key idea is that the loop repeats only while the condition remains satisfied. The update step is necessary to avoid endless repetition.


Advantages / Applications

Useful when the number of repetitions is unknown

  • A while loop is ideal when you do not know in advance how many times something should repeat.
  • Example: keep asking for input until it is valid.

Helps automate repetitive tasks

  • It reduces the need to write repeated code manually.
  • Example: printing numbers, processing data, or reading entries until a stop value is entered.

Widely used in real programs

  • While loops are used in games, menus, validation systems, searching processes, and event-driven applications.
  • They are also helpful in algorithmic problem solving where repetition depends on changing conditions.

Summary

  • while is used to repeat instructions as long as a condition stays true.
  • It is checked before each repetition, so it may run zero or more times.
  • Proper initialization and updating are necessary to prevent infinite loops.
  • Important terms to remember: condition, loop body, control variable, infinite loop, sentinel value