Sajjad Rasul Chaudhry
M08-PG12
Moghees Arshad
M08-PG13
Introduction
Loops are basically meant to do a task multiple times,
without actually coding all statements over and over
again.
They are also called "repetition structures” or “iterations”.
Types of Loops
Loops in C++ are mainly of three types :
1. 'for' loop ( good for numeric simulations or when we are
dealing with numbers only)
2. 'while' loop (good for symbolic conditions or characters.)
3. 'do while' loop (used when program has to be executed
at least once.)
“for” Loop:
for loop is basically a pretest loop. The syntax for a for loop is
for ( variable initialization; condition; variable update ) {
Code to execute while the condition is true
}
The variable initialization allows us to either declare a variable and
give it a value or give a value to an already existing variable.
The condition tells the program that while the conditional
expression is true the loop should continue to repeat itself.
The variable update section is the easiest way for a for loop to
handle changing of the variable.
“for” Loop (cont.)
a semicolon separates each of these sections, that is important.
Also every single one of the sections may be empty, though the
semicolons still have to be there.
If the condition is empty, it is evaluated as true and the loop
will repeat infinite times until something else stops it.
“for” Loop (cont.)
Initial statement
Loop condition Loop termination
Condition F
Update statement
Increment/decrement T
Statements in body of loop
Statement 1:
Statement n:
“while” Loop Loop starts
The while loop, executes a block of
statements as long as a specified
condition is true. It is a pretest loop. F
The syntax for a while loop is: condition
Increment/
decrement T Loop termination
while (<stopping condition>) {
<one or more statements> Statement 1:
increment/decrement
} Statement n:
The while loop checks to see if the
<stopping condition> is true or not. If true, the statements inside the while
loop are executed, then the <stopping condition> is checked again, and so on.
When the <stopping condition> is found to be false, execution continues with
whatever statements follow at the end of the while loop.
“do while” Loop
It is similar to while loop but
contrarily checks the condition at
the end of the loop rather than at
the beginning so, it is a posttest loop.
Syntax is: Statement 1:
T
Statement n:
do { T
<one or more statements>
increment/decrement
F
} while (<stopping condition>) condition
Loop condition
Statements inside a do loop are always executed at least once,
whereas the statements inside a while loop may never be executed.
Control of “while” loops
Two major types of statements used for repetition of a loop are as
below:
• Sentinel-Control Repetition
Sentinel value is a special value of a variable in the loop condition
that makes the loop condition false. The sentinel value of a sentinel
variable must be a value of the variable that is not encountered in
normal operation of the loop but is taken as input from the user
during execution of loop.
• Counter-controlled Repetition
Uses a counter variable to count the number of times a loop is
iterated. Thus in this statement it is decided before the execution of
a loop that how many times it will run.
Applications of Loops
loops are used to solve a multitude of programming
problems. Three common uses include:
Sentinel controlled loops to input data.
Data validation.
Computing sums and averages.