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:

[cc lang=”bash” escaped=”true” width=”auto” nowrap=”0″ theme=”blackboard” line_numbers=”off” ]

while (test condition)

{

body_of_the_loop;

}

[/cc]

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:

[cc lang=”bash” escaped=”true” width=”auto” nowrap=”0″ theme=”blackboard” line_numbers=”off” ]

do

{

statement(s);

}

while(test condition);

[/cc]

  • 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:

[cc lang=”bash” escaped=”true” width=”auto” nowrap=”0″ theme=”blackboard” line_numbers=”off” ]

for (initialization; test condition; increment or decrement)

{

Statement(s);

}

[/cc]

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.

[cc lang=”bash” escaped=”true” width=”75%” nowrap=”0″ theme=”blackboard” line_numbers=”off” ]

for(;condition;increment/decrement)

body;

[/cc]

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

[cc lang=”bash” escaped=”true” width=”75%” nowrap=”0″ theme=”blackboard” line_numbers=”off” ]

for (initialization;condition;)

body;

[/cc]

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

[cc lang=”bash” escaped=”true” width=”75%” nowrap=”0″ theme=”blackboard” line_numbers=”off” ]

for (initialization;;increment/decrement)

body;

[/cc]

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

[cc lang=”bash” escaped=”true” width=”75%” nowrap=”0″ theme=”blackboard” line_numbers=”off” ]

for (;;increment/decrement)

body;

[/cc]

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

[cc lang=”bash” escaped=”true” width=”75%” nowrap=”0″ theme=”blackboard” line_numbers=”off” ]

for (initialization;;)

body;

[/cc]

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

[cc lang=”bash” escaped=”true” width=”75%” nowrap=”0″ theme=”blackboard” line_numbers=”off” ]

for (;condition;)

body;

[/cc]

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

[cc lang=”bash” escaped=”true” width=”75%” nowrap=”0″ theme=”blackboard” line_numbers=”off” padding=”10px 10px 12px 12px”]

for (;;;)

body;

[/cc]

Related Post

Break Statement in JAVA Break Statement in JAVA The break statement is usually used in following two scenarios: The break statement terminates the loop immediately, and...
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...
Asset Color Catalog | Swift Tutorial Asset Color Catalog is the feature of XCode 9. BY the use of this feature we can maintain the colors easily and avoid the mistakes. We can create the ...
Create UITableView (Table View) By the use of stor... How to use UITableView in Above IOS 8 and Swift (UITableView) In this tutorial we are study how to use the TableView in Swift. One of the most common...
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...
Use of CollectionViewDataSource method moveItemAt ... Use of CollectionViewDataSource method moveItemAt for Reordering CollectionView cell In IOS (Swift 4) In this tutorial we are study about how we can ...
Author

Write A Comment