Loop Control Statement

Loop Control Statement

Loop control statements are used when a section of code may either be executed a fixed number of times. C gives you a choice of three types of loop statements, while, do- while and for.

  • The while loop keeps repeating an action until an associated condition returns false. This is useful where the programmer does not know in advance how many times the loop will be traversed.
  • The do while loop is similar, but the condition is checked after the loop body is executed. This ensures that the loop body is run at least once.
  • The for loop is frequently used, usually where the loop will be traversed a fixed number of times.

The While Loop

When in a program a single statement or a certain group of statements are to be executed repeatedly depending upon certain test condition, then while statement is used. The syntax is as follows:

while (test condition)

{

body_of_the_loop;

}

Here, test condition is an expression that controls how long the loop keeps running.

  • Body of the loop is a statement or group of statements enclosed in braces and are repeatedly executed till the value of test condition evaluates to true.
  • As soon as the condition evaluates to false, the control jumps to the first statement following the while statement.
  • If condition initially itself is false, the body of the loop will never be executed.
  • While loop is sometimes called as entry-control loop, as it controls the execution of the body of the loop depending upon the value of the test condition.

The do…while Loop

There is another loop control structure which is very similar to the while statement – called as the do.. while statement. The only difference is that the expression which determines whether to carry on looping is evaluated at the end of each loop. The syntax is as follows:

do

{

statement(s);

}

while(test condition);
  • In do-while loop, the body of loop is executed at least once before the condition is evaluated.
  • Then the loop repeats body as long as condition is true.
  • However, in while loop, the statement doesn’t execute the body of the loop even once, if condition is false. That is why do-while loop is also called exit-control loop.

The for Loop

for statement makes it more convenient to count iterations of a loop and works well where the number of iterations of the loop is known before the loop is entered. The syntax is as follows:

for (initialization; test condition; increment or decrement)

{

Statement(s);

}

The main purpose is to repeat statement while condition remains true, like the while loop. But in addition, for provides places to specify an initialization instruction and an increment or decrement of the control variable instruction. So this loop is specially designed to perform a repetitive action with a counter.

  • Initialization is executed. Generally it is an initial value setting for a counter variable. This is executed only once.
  • Condition is checked, if it is true the loop continues, otherwise the loop finishes and statement is skipped.
  • Statement is/are executed. As usual, it can be either a single instruction or a block of instructions enclosed within curly brackets { }.
  • Finally, whatever is specified in the increment or decrement of the control variable field is executed and the loop gets back to step 2.

Various forms of loop statements can be:

(a) A blank first statement will mean no initialization.

for(;condition;increment/decrement)

body;

(b) A blank last statement will mean no running increment/decrement.

for (initialization;condition;)

body;

(c) A blank second conditional statement means no test condition to control the exit from the loop. So, in the absence of second statement, it is required to test the condition inside the loop otherwise it results in an infinite loop where the control never exits from the loop.

for (initialization;;increment/decrement)

body;

(d) Initialization is required to be done before the loop and test condition is checked inside the loop.

for (;;increment/decrement)

body;

(e) Test condition and control variable increment/decrement is to be done inside the body of the loop.

for (initialization;;)

body;

(f) Initialization is required to be done before the loop and control variable increment/decrement is to be done inside the body of the loop.

for (;condition;)

body;

(g) Initialization is required to be done before the loop, test condition and control variable increment/decrement is to be done inside the body of the loop.

for (;;;)

body;

Related Post

C Language – Introduction Why C? There are three important aspects of any language so as C has : The way it stores data, The way it operates on this data and How th...
Array An array is a collection of similar kind of data elements stored in adjacent memory locations and are referred to by a single array-name. The first el...
Create Firebase single Page Website Create firebase single page Website tutorial In this tutorial we are study how to create single page website by the use of firebase hosting its very ...
First Java Program First Java Program NOTE: You need to have java installed on your system. Step 1: Open a text editor, like Notepad on windows and TextEdit on Mac. W...
Comments in C Comments in C Comments in C language are used to provide information about lines of code. It is widely used for documenting code. There are 2 types o...
Switch Case Statement Switch Case Statement The switch statement is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of c...

Leave a Reply

Your email address will not be published. Required fields are marked *

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.