0% found this document useful (0 votes)
24 views

Repetition Structures "Loops"

The document discusses repetition structures called loops, specifically the while loop. It provides examples of how the while loop works, including: 1) The general format of the while loop evaluates an expression and repeats a statement or block of statements if the expression is true. 2) Loops must contain logic to eventually make the expression false to avoid infinite loops. 3) The while loop can be used for input validation by repeating a prompt and read until valid input is entered. 4) A counter variable is often used to control how many times a loop repeats. The counter must be initialized and typically incremented or decremented each iteration.

Uploaded by

Hani Tamimi
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Repetition Structures "Loops"

The document discusses repetition structures called loops, specifically the while loop. It provides examples of how the while loop works, including: 1) The general format of the while loop evaluates an expression and repeats a statement or block of statements if the expression is true. 2) Loops must contain logic to eventually make the expression false to avoid infinite loops. 3) The while loop can be used for input validation by repeating a prompt and read until valid input is entered. 4) A counter variable is often used to control how many times a loop repeats. The counter must be initialized and typically incremented or decremented each iteration.

Uploaded by

Hani Tamimi
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Repetition Structures

“Loops”
Introduction to Loops: The while
Loop
Introduction to Loops:
The while Loop
• Loop: a control structure that causes a
statement or statements to repeat
• General format of the while loop:
while (expression)
statement;
• statement; can also be a block of
statements enclosed in { }
The while Loop – How It Works
while (expression)
statement;
• expression is evaluated
– if true, then statement is executed, and
expression is evaluated again
– if false, then the loop is finished and
program statements following statement
execute
The Logic of a while Loop
The while loop in Program 5-3
How the while Loop in Program 5-
3 Lines 9 through 13 Works
Flowchart of the while Loop in
Program 5-3
The while Loop is a Pretest Loop
expression is evaluated before the
loop executes. The following loop will
never execute:

int number = 6;
while (number <= 5)
{
cout << "Hello\n";
number++;
}
Watch Out for Infinite Loops
• The loop must contain code to make
expression become false
• Otherwise, the loop will have no way of
stopping
• Such a loop is called an infinite loop,
because it will repeat an infinite number of
times
Example of an Infinite Loop

int number = 1;
while (number <= 5)
{
cout << "Hello\n";
}
Using the while Loop for Input
Validation
Using the while Loop for
Input Validation
• Input validation is the process of
inspecting data that is given to the
program as input and determining whether
it is valid.

• The while loop can be used to create input


routines that reject invalid data, and repeat
until valid data is entered.
Using the while Loop for
Input Validation
• Here's the general approach, in
pseudocode:

Read an item of input.


While the input is invalid
Display an error message.
Read the input again.
End While
Input Validation Example

cout << "Enter a number less than 10: ";


cin >> number;
while (number >= 10)
{
cout << "Invalid Entry!"
<< "Enter a number less than 10: ";
cin >> number;
}
Flowchart for Input Validation
Input Validation in Program 5-5
Counters
Counters
• Counter: a variable that is incremented or
decremented each time a loop repeats
• Can be used to control execution of the
loop (also known as the loop control
variable)
• Must be initialized before entering loop
A Counter Variable Controls the
Loop in Program 5-6

Continued…
A Counter Variable Controls the
Loop in Program 5-6

You might also like