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)




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:





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)




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.



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

for (initialization;condition;)


(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)


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

for (;;increment/decrement)


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

for (initialization;;)


(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;)


(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 (;;;)


Related Post

Java If-else Statement Java If-else Statement The Java if statement is used to test the condition. It checks boolean condition: true or false. For example, if a number i...
Variables Variables A variable is a named location in memory that is used to hold a value that can be modified by the program. All variable must be declared be...
Resignation Letter and Relieving Letter Resignation Letter Sample 1 Sender’s Name, Sender’s Address, Mobile Number, MailDate:ToThe Human Resource Department Company Name Company Addre...
How to install flutter | Flutter tutorial Flutter is the google’s mobile app SDK for develop the high-quality native app for iOS and android. We can develop the app by use of DART and javascri...
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...
Java Operators Java Operators An operator is used to perform an operation over one or more operands. It is a character that represents an action, for example * is a...

Leave a Reply

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