Why Repetition is Needed: Show
Types of Repetition Structures Two types of repetition structures: pretest and
posttest loops
while LoopUsing while Loop:
while (expression) statement;
int counter=0; //initialize loop control variable while (counter < n) //test loop control variable { . . . counter++; //update loop control variable . . . } Example 1: int counter=1; //initialize loop control variable while (counter < 11) //test loop control variable { cout << i << endl; //print value of i counter++; //update loop control variable } Example 2: //printing 1 - 10 using while loop cout << "while loop prints 1 - 10" << endl; int num=1; while(num < 11) { cout << num << endl; ++num; }Sentinel-Controlled while Loops:
cin >> variable; //initialize loop control variable while (variable != sentinel) //test loop control variable { . . . cin >> variable; //update loop control variable . . . }Flag-Controlled while Loops:
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:
cin >> variable; //initialize loop control variable while (cin) //test loop control variable { . . . cin >> variable; //update loop control variable . . . } istreamVar.eof() } 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 hereThis works neatly, because operator >> returns zero when an error occurs, which would happen if a non-int or end-of-file is encountered. for LoopUsing for Loop:
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; }
Example: for(int i = 1; i < 11; i++); //semantic error--action of for loop is empty cout << i << endl; Example: for (;;) cout << "Hello" << endl; do...while LoopUsing do...while Loop:
do statement while (expression); Example: //printing 1 - 10 using do while loop cout << "do while loop prints 1 - 10" << endl; int num1=1; do { cout << num1 << endl; num1=num1 + 1; } while(num1 < 11);
break and continue Statements (alter flow of control)Using break Statements:
FYI... statement cannot be used in a switch statement. In a while loop, when the continue statement is executed, if the update statement appears after the continue statement, the update statement is not executed. In a for loop, the update statement always executes. In general, avoid using break and continue to escape loop and branch code (one exception would be in switch statements). Instead consider adding/changing the exit conditions of the control statement. Likewise, even though permitted (due to backward-compatibility) refrain from using goto statements. With that said, also consider how sometimes generalized rules may be reconsidered under specific circumstances. Which type of control structure allows the repetition of instructions or statements?LOOP statements let you execute a sequence of statements multiple times. There are three forms of LOOP statements: LOOP , WHILE-LOOP , and FOR-LOOP .
Is a structure used to execute a set of instructions repeatedly until the specified condition is met?Repetition structure is used when a program needs to repeatedly process on one or more instructions until some conditions are met.
What is repetition structure in C programming?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.
Which type of structure makes a decision based on one or more conditions?Selection Structure. Use to make a decision or comparison and then, based on the result of that decision or comparison, to select one of two paths. The condition must result in either a true (yes) or false (no) answer. If the condition is true, the program performs one set of tasks.
|