Iteration Statement in C++
-
Upload
kendriya-vidyalaya-no1-sec-14gurgaon -
Category
Software
-
view
381 -
download
0
Transcript of Iteration Statement in C++
![Page 1: Iteration Statement in C++](https://reader036.fdocuments.us/reader036/viewer/2022062412/58f0aae01a28abeb188b457b/html5/thumbnails/1.jpg)
Iteration Statement Made byGaurav YadavXI-ARoll No- 10
![Page 2: Iteration Statement in C++](https://reader036.fdocuments.us/reader036/viewer/2022062412/58f0aae01a28abeb188b457b/html5/thumbnails/2.jpg)
What is Iteration?
Iteration construct means repetition of set of statements depending upon a condition test. Till the time of condition is true. ( or false depending upon the loop). A set of statements are repeated again and again. As soon as the condition become false (or true), the repetition stops. The iteration condition is also called ”Looping Statement ”.
Idli
![Page 3: Iteration Statement in C++](https://reader036.fdocuments.us/reader036/viewer/2022062412/58f0aae01a28abeb188b457b/html5/thumbnails/3.jpg)
ELEMNTS THAT CONTROL
A LOOP
InitializationExpression
Test Expression
UpdateExpression
The Body Of The Loop
![Page 4: Iteration Statement in C++](https://reader036.fdocuments.us/reader036/viewer/2022062412/58f0aae01a28abeb188b457b/html5/thumbnails/4.jpg)
Click icon to add picture
Click icon to add picture
Type offor
Loop
while Loop
Do while Loop
![Page 5: Iteration Statement in C++](https://reader036.fdocuments.us/reader036/viewer/2022062412/58f0aae01a28abeb188b457b/html5/thumbnails/5.jpg)
The for Loop
![Page 6: Iteration Statement in C++](https://reader036.fdocuments.us/reader036/viewer/2022062412/58f0aae01a28abeb188b457b/html5/thumbnails/6.jpg)
The for Loop
• In computer science a for-loop (or simply for loop) is a programming language control statement for specifying iteration, which allows code to be executed repeatedly.
• Syntax
for (ForInit ; ForExpression; PostExpression)
Action
• Example
for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl;
}
![Page 7: Iteration Statement in C++](https://reader036.fdocuments.us/reader036/viewer/2022062412/58f0aae01a28abeb188b457b/html5/thumbnails/7.jpg)
FlowChartForA
for-Loop
ForExpr
Action
true false
ForInit
PostExpr
Evaluated onceat the beginning
of the forstatements's
execution The ForExpr isevaluated at thestart of each
iteration of theloop
If ForExpr istrue, Action is
executed
After the Actionhas completed,
thePostExpression
is evaluated
If ForExpr isfalse, program
executioncontinues withnext statement
After evaluating thePostExpression, the next
iteration of the loop starts
![Page 8: Iteration Statement in C++](https://reader036.fdocuments.us/reader036/viewer/2022062412/58f0aae01a28abeb188b457b/html5/thumbnails/8.jpg)
Example
• Output• Input
![Page 9: Iteration Statement in C++](https://reader036.fdocuments.us/reader036/viewer/2022062412/58f0aae01a28abeb188b457b/html5/thumbnails/9.jpg)
The while Loop
![Page 10: Iteration Statement in C++](https://reader036.fdocuments.us/reader036/viewer/2022062412/58f0aae01a28abeb188b457b/html5/thumbnails/10.jpg)
The while Loop
• In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The while loop can be thought of as a repeating if statement.
• Syntax
Logical expression that determineswhether the action is to be executed
while ( Expression ) Action
Action to be iterativelyperformed until logical
expression is false
![Page 11: Iteration Statement in C++](https://reader036.fdocuments.us/reader036/viewer/2022062412/58f0aae01a28abeb188b457b/html5/thumbnails/11.jpg)
FlowChartOfAwhileLoop
Expression
Action
true false
Expression isevaluated at the
start of eachiteration of the
loop
If Expression istrue, Action is
executed If Expression isfalse, program
executioncontinues withnext statement
![Page 12: Iteration Statement in C++](https://reader036.fdocuments.us/reader036/viewer/2022062412/58f0aae01a28abeb188b457b/html5/thumbnails/12.jpg)
Example
Input Output
![Page 13: Iteration Statement in C++](https://reader036.fdocuments.us/reader036/viewer/2022062412/58f0aae01a28abeb188b457b/html5/thumbnails/13.jpg)
The do-while Loop
![Page 14: Iteration Statement in C++](https://reader036.fdocuments.us/reader036/viewer/2022062412/58f0aae01a28abeb188b457b/html5/thumbnails/14.jpg)
The do-while Loop
• a do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given boolean condition at the end of the block.
• Syntax
do Action
while (Expression)
![Page 15: Iteration Statement in C++](https://reader036.fdocuments.us/reader036/viewer/2022062412/58f0aae01a28abeb188b457b/html5/thumbnails/15.jpg)
Flow Chart
![Page 16: Iteration Statement in C++](https://reader036.fdocuments.us/reader036/viewer/2022062412/58f0aae01a28abeb188b457b/html5/thumbnails/16.jpg)
Example
Input Output
![Page 17: Iteration Statement in C++](https://reader036.fdocuments.us/reader036/viewer/2022062412/58f0aae01a28abeb188b457b/html5/thumbnails/17.jpg)
Iteration Do’s
![Page 18: Iteration Statement in C++](https://reader036.fdocuments.us/reader036/viewer/2022062412/58f0aae01a28abeb188b457b/html5/thumbnails/18.jpg)
![Page 19: Iteration Statement in C++](https://reader036.fdocuments.us/reader036/viewer/2022062412/58f0aae01a28abeb188b457b/html5/thumbnails/19.jpg)