Where to Use Different Types of Loops (for, while, do-while) in C

Loops are very fundamental programming language constructs that allow us to execute a block of code multiple times. In C programming language, there are three types of loops: for, while and do-while. Here we will discuss on the syntax of every loop and where to use them. Though we have three types of loops, any one is sufficient to solve any requirement. I can not imagine a situation where you can do something using one loop and that can not be done using other type of loop. But it is also true that one type of loop is more  convenient in some situation than others.

for loop

Syntax:

for (<init_statement>; <condition>; <end_statement>)
{
 <code_block>
}

The <init_statement> will be executed only once at the beginning of the first iteration of the loop. This is used primarily to initialize or reset a variable. The <condition> will be evaluated at the beginning of every iteration of the loop. If the <condition> satisfies, the <code_block> will be executed. And finally, after completion of <code_block> execution, the <end_statement> is executed. The <end_statement> is generally used to increment a variable.

Example:

for(i = 0; i < 5; i++)
{
    printf("Value of i is %d.n", i);
}

Output:

Value of i is 0.
Value of i is 1.
Value of i is 2.
Value of i is 3.
Value of i is 4.

In this example we initialized i with 0 in the <init_statement>. It will be executed once at the beginning of the first iteration. In our <condition> statement, we are checking if the value of i is less than 5. So, in the first iteration this conditional statement will be true and the <code_block> (a printf statement here) will be executed. The value of the variable i will be printed. After completion of <code_block> execution, the <final_statement> will be executed where we are incrementing the variable i. After completion of first iteration i will become 2. In the second iteration the <init_statement> will not be executed but the <condition> will be re-evaluated. As the value of i is 2 in the second iteration, the <condition> will again be true and the <code_block> will be re-executed. This will continue until the value of i becomes 5. That will happen after end of 5th iteration. When value of i will be 5, the condition will not be satisfied and the loop will be terminated.

Where to use for loop:
for loop is used primarily when we know how many times we want to iterate or we want to traverse a list or array. The variable, that are used as index of list or array, will be used int the and for initialize and increment. We will access an element of the list or array using that variable.
For example, if we want to access or print all elements of an array, better to use the for loop. As I mentioned earlier you can achieve the same thing using other type of loops also.

int i;
int arr[] = {2, 6, 34, 6, 1, 44};
for (i = 0; i < sizeof (arr); i++)
{
printf("Value of arr[%d] is %d. n", i, arr[i]);
}

Output:

Value of arr[0] is 2.
Value of arr[1] is 6.
Value of arr[2] is 34.
Value of arr[3] is 6.
Value of arr[4] is 1.
Value of arr[5] is 44.

while Loop

Syntax:

while( <condition> )
{
 <code-block>
}

Here we have only <condition> and <code_block>. Here first the <condition> will be checked and if the is true, the <code_block> will be executed. This process will continue until the <condition> becomes false.

Example:

int x = 1000;
while (x > 0)
{
    x = x - (rand() % 5);
}

In this example the loop will continue to run until x becomes 0 or negative. In every iteration we are subtracting x by a number ranging from 0 to 4. As a result we won’t know how many times the loop will iterate.

Where to use while loop:
Typically we use this loop when we don’t traverse a list or an array and we don’t know how many times the loop should iterate. I want to remind you again that this is just a guide line. “while” loop is widely used when the number of iterations is known beforehand.

do-while loop

Sytanx:

do {
 <code-block>
} while(<condition>);

This loop is very similar to the while loop but the difference is that the <code_block> will be executed first then the <condition> will be verified. If the <condition> returns true the <code_block> will be re-executed. This process will continue until the <condition> becomes false. One thing you should note here that the do-while loop will iterate at least once whereas the while or for loop might not iterate at all.

Where to use do-while loop:
This loop is used if you want to execute something execute at least once and then re-execute based on some condition. Consider this example where you want to continue a loop until user gives 0 as input. The example can be written as:

int x;
do{
scanf("Enter a number: %d", &x);
} while(x != 0);

This can also be achieved using while loop like this.

int x;
scanf("Enter a number: %d", &x);
while(x != 0)
{
    scanf("Enter a number: %d", &x);
}

But here we had to use scanf statement twice. There is no other way to achieve this.

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

2 thoughts on “Where to Use Different Types of Loops (for, while, do-while) in C”

  1. Very funny typo above 🙂
    Though we have three types of loos, any one loop is sufficient to solve any requirement.

Leave a Reply

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

0
0
0
0
0
0