Lecture 12 Loops
Lecture 12 Loops
Introduction to
Loops
MUHAMMAD AHMAD
LECTURER
CS DEPARTMENT
Today we will cover 2
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 6
The while loop-Example 7
How the while Loop in previous 8
int n = 6;
while (n <= 5)
{
cout << "Hello! ICP\n";
number++;
}
Watch Out for Infinite Loops 11
int n = 1;
while (n <= 5)
{
cout << "Hello ICP\n";
}
Using the while Loop for Input Validation
Using the while Loop for 14
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 15
Input Validation
Here's the general approach, in pseudocode:
Continued…
A Counter Variable Controls 24
The do-while Loop
The do-while Loop 26
int x = 1;
do
{
cout << x << endl;
} while(x < 0);
Continued…
A do-while Loop 30
do-while Loop Notes 31