Coupon Accepted Successfully!


What is a Loop?

A loop is defined as a block of statements that are repeatedly executed for certain number of times even though those block statements appear only once in the program.

Loops are of two types and they are as follows:

(1) Counter-controlled repetition

(2) Sentinel-controlled repetition

  1. Counter-controlled repetition: This is also called definite repetitive actions, because in advance the number of iterations to be performed is defined in the program itself. The steps for performing counter-controlled repetitions are given below.
    Steps in loop
    Loop Variable: It is a variable used in the loop.
    Initialization: It is the first step in which starting and final value is assigned to the loop variable. Each time the updated value is checked by the loop itself.
    Incrimination/Decrimination: It is the numerical value added or subtracted to the variable in each round of the loop. The updated value is compared with the final value and if it is found to be less than final value the steps in the loop are executed.
    The above steps are implemented in numerous programs in this chapter.
  2. Sentinel-controlled repetition: This is also called indefinite repetitive actions. One cannot estimate the number of iterations to be performed. In this type loop, termination happens based on certain conditions using decision-making statement.

The ‘C++’ language supports three types of loop control statements. Their syntaxes are described as given in Figure.

Syntaxes of various loops.

The for loop comprises three actions. The three actions are placed in the for statement itself. The three actions initialize counter, test condition and re-evaluation parameters are included in one statement. The expressions are separated by semi-colons (;). This leads to the programmer to visualize the parameters easily. The for statement is equivalent to the while and do-while statements. The only difference between for and while is later checks the logical condition and then executes the body of the loop, whereas in the for statement test is always performed at the beginning of the loop. The body of the loop may not be executed at all times if the condition fails at the beginning. The do-while loop executes the body of the loop at least once regardless of the logical condition.

Test Your Skills Now!
Take a Quiz now
Reviewer Name