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

Firebase Push notification | Swift tutorial Push Notification by firebase in IOS.For Firebase Push notification we need to have paid apple developer account. You need to Enroll into the Apple D...
Setting Class Path for Java Setting Class Path for Java In this tutorial we are study how we can set the java JDK class path. Java is an open source software, it is is freely...
Dart Data type | Dart Tutorial Dart Data type Variables can store data of different types, and different data types can do different things. Dart supports the following data typ...
Data Type And Storage And Keywords To store data inside the computer we need to first identify the type of data elements we need in our program. There are several different types of dat...
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...
Create Gradient layer by the use of CAGradientLaye... Create Gradient layer by the use of CAGradientLayer Mostly developers are use the gradient (Combination of colors) colors images for make app more ef...

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.