10/12/2024
LECTURE-10: LOOPS
Computer Programming
1
TODAY WE WILL COVER
The while loop
The while Loop for Input Validation
Counters
The do-while loop
CONDITIONAL OPERATOR (? :)
Syntax
(Boolean expression) ? Statement1 :
Statement2;
THE WHILE LOOP
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-EXAMPLE
HOW THE WHILE LOOP IN PREVIOUS
SLIDE LINES 9 THROUGH 13 WORKS
FLOWCHART OF THE WHILE LOOP
THE WHILE LOOP IS A PRETEST LOOP
expression is evaluated before the
loop executes. The following loop will
never execute:
int n = 6;
while (n <= 5)
{
cout << "Hello! ICP\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 n = 1;
while (n <= 5)
{
cout << "Hello ICP\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
Continued…
A COUNTER VARIABLE CONTROLS
THE DO-WHILE
LOOP
THE DO-WHILE LOOP
do-while: a posttest loop – execute the loop, then
test the expression
General Format:
do
statement; // or block in { }
while (expression);
Note that a semicolon is required after
(expression)
THE LOGIC OF A DO-WHILE LOOP
AN EXAMPLE DO-WHILE LOOP
int x = 1;
do
{
cout << x << endl;
} while(x < 0);
Although the test expression is false, this loop will
execute one time because do-while is a
posttest loop.
A DO-WHILE LOOP
Continued…
A DO-WHILE LOOP
31
DO-WHILE LOOP NOTES
Loop always executes at least once
Execution continues as long as expression is
true, stops repetition when expression
becomes false
10/12/2024
Questions
33