1
A complete theoretical reference to loops in C
Programming Concepts

Loops defined: In any programming language, a loop is defined as a set of instructions that needs to be executed till a particular condition is satisfied. That is loops are the control statements of the program. And it is also worth remembering that the loops are subject to constraints (conditions). Types of loops: The loops are broadly classified into two types, namely

  1. The entry control loops or the pre-check loops.
  2. The exit control loops or the post-check loops.

As the name suggests, the entry control loops are those loops that verify the condition first and then decide to execute the set of instructions under that section and the exit control loops are those loops in which the code is executed first and then checked for the validity of the condition that acts as a constraint to the set of instruction that needs to be repeated.

The example of the entry control loops are the ‘while’ loop and the ‘for’ loop. The example of the exit control loop is the ‘do while’ loop.

Strictly speaking, there are three main parts in a loop, namely the initialization, the condition and the increment, decrements or the control operation on the conditional variable. As we know, the loop statement has to be executed from a certain point of initialization to a certain point of termination (i.e. end of condition). For this purpose, a conditional variable is taken and initialized to some required value and then the instruction is given to the computer with a condition telling, up to what value of this conditional variable, the set of codes must be repeatedly iterated and in what way the conditional variable is changing after every iteration.

Now the differences between the entry control loops and the exit control loops:

1.The entry control loops check the condition first and then executes the loop whereas the exit control loops execute the loop statements first and then check the trueness of the condition.

2. The entry control loops have zero or no iterations, if the first condition itself fails but on the contrary, the exit control loops have at least one iteration executed even though the first condition is wrong.

3.In an entry control loop, if there are ‘n’ true iterations possible, then the loop statements are executed ‘n’ times only but ironically if the exit control loop has ‘n’ true possible iterations, then the loop statements are executed ‘n+1’ times in the program.

Early exit from loops or jumps in loop execution: Before all the iterations are done, it is possible to come out of the loops and proceed with the next module by what is called as the early exit from the loops. The main tools that the compiler provides us are the ‘break’ statement, the ‘continue’ statement, the ‘exit’ statement and the ‘goto’ statement. Let us look into them in detail below.

The ‘break’ statement: This is the tool for termination of loop only after satisfying a sub-condition inside the loop. This just stops the loop iterations and continues with the codes that follow the loop in the program.

The ‘continue’ statement: This is the tool for termination of a particular iteration of the loop. This doesn’t allow a particular iteration of the loop to be executed and continues executing the loop with the next iteration till the main condition is satisfied.

The ‘exit’ statement: This is the tool that terminates the entire program including the loop before all the iterations are completed. In C, this is found in “stdlib” header file.

The ‘goto’ statement: This is the tool that helps to navigate from one part of the program to other part of the program where the goto labels are mentioned. This ‘goto’ statement has to be used very wisely or else there will be maximum chances that the program will undergo infinite iteration mode.

Thus the programming languages like C are having three main features; that is they are sequential, they are deciding and they are repetitive. This concept of loop enhances the beauty of their repetitive character and make programming much easier and enjoying.

Notifications

?