Download presentation
Presentation is loading. Please wait.
Published byMarlene Stevenson Modified over 9 years ago
1
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Loops: Repeating One or More Statements for loop while loop do-while loop break statement in a loop continue statement in a loop Nested loops
2
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Loops Iteration statements –while (conditions) { statements; } –do {statements;} while (conditions); –for ( initializing_expression; conditions; iteration_expression) { statements; }
3
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. while loopdo-while loop
4
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. The while Loop
5
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. while Loop while (condition) { statements; } Program 5.1
6
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. The do-while Loop do { statements; } while (conditions); Program 5.2
7
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. The for Loop
8
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. for Flowchart
9
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. for Loop for (initialization; conditions; iteration) { statements; } Program 5.3
10
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Loops and Variable Scope int i = 1; for (; i<=count; i++) sum +=1; for (int i=1; i<=count; i++) sum +=1;
11
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Controlling a for Loop with Floating-points Values const double pi = 3.14159265; for (double radius=2.5; radius<=20.0; radius+=2.5) cout << “radius = “ << setw(12) << radius << “ area =“ << setw(12) << pi*radius *radius << endl; Program 5.4, 5.5
12
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Multiple Initializations in A Loop Expression for (initialized expressions; conditions; expressions) E.g. –for (int j=0, k=2, product=1; k<count; j++, k++) Program 5.6, 5.7
13
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. The comma operator
14
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Nested Loop Using a nested loop to generate multiplication talbes Program 5.8
15
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. continue Statement while (cin.get(ch)) { statement1; if (ch == ‘\n’) continue; statement2; } Program 5.9
16
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Indefinite Loops for (;;) { statements; } while (true) { statements; } Using the break statement to terminate the indefinite loop
17
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. break Statement while (cin.get(ch)) { statement1; if (ch == ‘\n’) break; statement2; } statement3; Program 5.10, 5.11
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.