0% found this document useful (0 votes)
16 views31 pages

L8 - Iteration

Uploaded by

lkixome
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)
16 views31 pages

L8 - Iteration

Uploaded by

lkixome
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/ 31

LOOPS

(Iteration control
structure)
By Sir. Joshua
Introduction
 Suppose you want to find the average marks
of each student for a class of 100 students.
 Toavoid repetition of performing the same
task, C++ programming languages uses
looping or iteration control structure.
 Loopingcontrol manages repeated
executions in a program.
 Theloop contains two parts: the body of the
loop and the control statement.

 Thecontrol statement tests the condition
provided, and it allows the repeated
execution of the statements within the loop
body.
 Thereare three types of looping or iteration
used in the C++ programming language;
these are: While loop, Do while loop and
For loop.
While loop

 The while loop allows a program to


test the conditions first before
performing the execution.
 Ifthe condition is true, the loop is
executed according to the loop body.
 Otherwise, the loop terminates.
Syntax and Flow Diagram
Example
Do-while loop
 The do-while loop allows execution of the statement at least
once, and then,
 it evaluates the loop.
 If the loop condition is true, the execution continues until the
condition becomes false.
 The do-while loop terminates when the loop condition is not met.
 Do-while loop is useful where the loop body needs to be executed
before the loop condition is tested.
 For example, when you look for your favorite food at
 the restaurant.
 First, you will go, and if the food is attractive, you will continue to
go. Otherwise, you will quit to another restaurant.
Syntax and Flow Diagram
Example
Difference between while...loop
statements and do-while…..loop
statements
For loop
 Unlikeother control structures, for...loop
contains three body features: initialisation,
condition, and increment/decrement
counter
 Functions
Feature /of body features
Part Function
Initialisation It initialises a variable
counter
Condition It tests the loop condition
Increment/ It provides an increment or
decrement decrement variable counter.
Syntax and Flow Diagram
Flow of control in a for loop
 Theinit step is executed first, and only once.
This step allows you to declare and initialize
any loop control variables. You are not required
to put a statement here, as long as a semicolon
appears.
 Next, the condition is evaluated. If it is true,
the body of the loop is executed. If it is false,
the body of the loop does not execute and flow
of control jumps to the next statement just
after the for loop.

 Afterthe body of the for loop executes, the flow
of control jumps back up to the
incrementstatement. This statement allows you
to update any loop control variables. This
statement can be left blank, as long as a
semicolon appears after the condition.
 The condition is now evaluated again. If it is
true, the loop executes and the process repeats
itself (body of loop, then increment step, and
then again condition). After the condition
becomes false, the for loop terminates.
Example
nested loops
 Syntax:
 Thesyntax for a nested for loop statement
in C++ is as follows:

 Thesyntax for a nested while loop
statement in C++ is as follows:

 Thesyntax for a nested do...while loop
statement in C++ is as follows:
Loop Control Statements
 Loop control statements change execution from
its normal sequence.
 When execution leaves a scope, all automatic
objects that were created in that scope are
destroyed.
 C++ supports the following control statements.

Control Description
Statement
Terminates the loop or switch statement
break
and transfers execution to the statement
statement
immediately following the loop or switch.
Causes the loop to skip the remainder of
continue
its body and immediately retest its
statement
condition prior to reiterating.
Transfers control to the labeled statement.
goto
Though it is not advised to use goto
statement
statement in your program.
break statement
 The break statement has the following two usages in
C++:
 When the break statement is encountered inside a loop,
the loop is immediately terminated and program control
resumes at the next statement following the loop.
 It can be used to terminate a case in the switch
statement.
Ifyou are using nested loops (i.e., one loop inside
another loop), the break statement will stop the
execution of the innermost loop and start executing the
next line of code after the block.
Syntax and Flow Diagram
Example
continue statement
 The continue statement works somewhat
like the break statement. Instead of forcing
termination, however, continue forces the
next iteration of the loop to take place,
skipping any code in between.
 Forthe for loop, continue causes the
conditional test and increment portions of
the loop to execute. For the while and
do...while loops, program control passes to
the conditional tests.
Syntax and Flow Diagram
Example
goto statement
A goto statement provides an unconditional
jump from the goto to a labeled statement in
the same function.
 NOTE: Use of goto statement is highly
discouraged because it makes difficult to
trace the control flow of a program, making
the program hard to understand and hard to
modify.
 Anyprogram that uses a goto can be
rewritten so that it doesn't need the goto.
Syntax and Flow Diagram

Where label is an identifier that


identifies a labeled statement. A
labeled statement is any
statement that is preceded by an
identifier followed by a colon (:).
Example
The Infinite Loop

 A loop becomes infinite loop if a condition never


becomes false.
 The for loop is traditionally used for this purpose.
 Since none of the three expressions that form the
for loop are required, you can make an endless loop
by leaving the conditional expression empty.
Syntax

When the conditional expression is absent, it is assumed to


be true. You may have an initialization and increment
expression, but C++ programmers more commonly use the
for(;;) construct to signify an infinite loop.

NOTE: You can terminate an infinite loop by pressing Ctrl + C


keys.

You might also like