0% found this document useful (0 votes)
39 views32 pages

Lecture 12 Loops

The document discusses different types of loops in C++. It covers the while loop, using while loops for input validation, counters to control loop execution, and the do-while loop. Key points include how while loops check the loop condition before each iteration, how counters can increment each loop repetition, and that do-while loops always execute the loop body at least once before checking the condition.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views32 pages

Lecture 12 Loops

The document discusses different types of loops in C++. It covers the while loop, using while loops for input validation, counters to control loop execution, and the do-while loop. Key points include how while loops check the loop condition before each iteration, how counters can increment each loop repetition, and that do-while loops always execute the loop body at least once before checking the condition.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Lecture # 12

Introduction to
Loops
MUHAMMAD AHMAD
LECTURER
CS DEPARTMENT
Today we will cover 2

 The while loop


 The while Loop for Input Validation
 Counters
 The do-while loop
The while Loop
The while Loop 4

 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 5

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

slide Lines 9 through 13 Works


Flowchart of the while Loop 9
The while Loop is a Pretest Loop 10

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 11

 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 12

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:

Read an item of input.


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

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


cin >> number;
while (number >= 10)
{
cout << "Invalid Entry!"
<< "Enter a number less than 10: ";
cin >> number;
}
17
18
Flowchart for Input Validation 19
Input Validation in Program 5-5 20
Counters
Counters 22

 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 23

Continued…
A Counter Variable Controls 24
The do-while Loop
The do-while Loop 26

 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 27
An Example do-while Loop 28

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 29

Continued…
A do-while Loop 30
do-while Loop Notes 31

 Loop always executes at least once


 Execution continues as long as expression is true, stops
repetition when expression becomes false
32

You might also like