




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The need for repetition structures or loops in programming tasks that require the same task to be performed multiple times. It describes the two types of repetition structures, pretest and posttest loops, and the three looping structures in C++. The document also explains counter-controlled repetition and the use of while, for, and do...while loops. It also covers the use of break and continue statements to alter the flow of control. examples and syntax for each concept.
Typology: Exercises
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Why Repetition is Needed: Repetition structures, or loops, are used when a program needs to repeatedly process one or more instructions until some condition is met, at which time the loop ends. Many programming tasks are repetitive, having little variation from one item to the next. The process of performing the same task over and over again is called iteration, and C++ provides built-in iteration functionality. A loop executes the same section of program code over and over again, as long as a loop condition of some sort is met with each iteration. This section of code can be a single statement or a block of statements (a compound statement). Loops and Using Loops Repetition allows the programmer to efficiently use variables Can structure programming statements to be repeated as long as specific conditions are met For example: can input, add, and average multiple numbers using limited number of variables There are three looping structures in C++: 1. while loop, 2. for loop, and 3. do...while Loop Mnemonic: "ALL loops must have ITU"--Initialize, Test, Update
Two types of repetition structures: pretest and posttest loops Pretest:
Using while Loop: Executes from zero to many times, depending on expression while is reserved word Syntax of while statement: while (expression) statement; Expression provides entry condition Expression (parentheses must be included as part of syntax) must evaluate to true to invoke loop statement(s) Statement(s) can be simple or compound (block) Statement(s) is/are body of loop BE SURE to include an exit condition that will eventually evaluate expression to be false Infinite loop: statement(s) continue(s) to execute endlessly Loop invocation:
flag-controlled while loop uses bool variable to control loop Syntax of flag-controlled while statement: bool found = false; //initialize loop control variable while (!found) //test loop control variable { . . . if (expression) found = true; //update loop control variable . . . } EOF (end of file)-Controlled while Loops: bool value returned by cin determines if program has input Syntax of EOF-controlled while statement: cin >> variable; //initialize loop control variable while (cin) //test loop control variable { . . . cin >> variable; //update loop control variable . . . } eof() function can determine end of file status eof() function is member of data type istream--like other I/O functions (get, ignore, peek) Syntax of eof() function: istreamVar.eof() } istreamVar is input stream variable (e.g., cin) Code that reads integers until EOF or a non-int is encountered: int n; while (cin >> n) { // do whatever appropriate with n, which is ensured to be of type int } // after all ints are read, execution picks up here
This works neatly, because operator >> returns zero when an error occurs, which would happen if a non-int or end-of-file is encountered.
Using for Loop: Executes a set number of times determined by the counter Statement can be a single statement or a compound (block) statement. for is reserved word Simplifies writing of count-controlled while loop Syntax of for statement (for loop control statements): for (initial statement; test statement (loop condition); update statement) statement; Example: //printing 1 - 10 using for loop cout << "for loop prints 1 - 10" << endl; for(int num=1; num < 11; ++num) { cout << num << endl; } for loop executes as follows:
Using break Statements: When break statement executes in repetition structure, it immediately exits Likewise, break statement in switch structure provides an immediate exit break statement can be used in while, for, and do...while loops Generally, break statement used for two purposes:
With that said, also consider how sometimes generalized rules may be reconsidered under specific circumstances.