LOOPS IN C++ (Presentation)
LOOPS IN C++ (Presentation)
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
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.
Update statement
Increment/decrement T
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:
Loop condition
• 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:
Data validation.