Loop Statements in C++

do-while statement in C++

The do-while loop is another repetitive loop used in C++ programs. When a loop is constructed using the while statement, the test for continuation of the loop is carried out at the beginning of each pass. Sometimes, however, it is desirable to have a loop with the test for continuation at the end of each pass. This can be accomplished by means of do-while statement. The general form of the do-while statement is do{ statement 1; statement 2; …. …. } while(expression); The statement will be executed repeatedly, as long as the value of expression is true (i.e., is non-zero). Notice that statement will always be executed at least once, since the test for repetition does not occur until the end of the first pass through the loop. The statement can be either simple or compound. It must include some feature that eventually alters the value of expression so that...

while statement in C++

The second type of loop, the while loop, is used when we are not certain that the loop will be executed. After checking whether the initial condition is true or false and finding it to be true, then only while loop will enter into the loop operations. The general form of the while loop for a single statement is: while(expression) statement; The general form of the while loop for a block of statements is: while(expression) { statement 1; statement 2; …. …. } The expression can be any valid C++ language expression including the value of a variable, an unary or a binary expression, or the value returned by a function. The statement can be single or compount statement. The statement will be executed repeatedly, as long as the expression is true (i.e., as long as expression has a non zero value). statement must include some features that eventually alters...

for statement in C++

The for statement or for loop is useful while executing a statement multiple number of times. The for loop is the most commonly used statement in C++. This loop consists of three expression: The first expression is used to initialize the index value The second expression is used to check whether or not the loop is to be continued again The third expression is used to change the index value for further iteration The general form of for statement is: for(expression 1; expression 2; expression 3) statement; In other words, for(initial condition; test condition; incrementer or decrementer) { statement1; statement2; ….. ….. } Where, expression 1 is the initialization of the expression or the condition called an index expression 2 is the condition checked. As long as the given expression is true the loop statement will be repeated expression 3 is the incrementer or decrementer to change the index value...