Difference Between do, do-while and for loop | Syntax and Coding Example

Difference Between do, do-while and for loop | Syntax and Coding Example

What is LOOPING STRUCTURE in C/C++?

Loops is a technique to repeatedly perform the same set of instructions until a specific condition is satisfied. It makes the code less complex and more efficient.

What are the Different Types of Loop Statement in C/C++?

C language has three looping statements, namely…

  • while
  • do while
  • for loop

1. while loop in C

In the while statement, firs,t a counter is initialized which undergoes a test condition. If the condition evaluates to true, then the body of the loop gets executed. Hence, while loop runs until the condition becomes false.

Syntax:

Its syntax is-

initialize counter;
while (test condition)
{
    //Body of the loop
}

Example:

Write a C Prgram to print all the first 10 multiples of three using while Loop.

#include<stdio.h>
int main()
{
int i,m;
i=1;
printf("The first ten multiples of 3 are\n");
while(i<=10)
{
m=3*i;
printf("%d\n",m)
i++;
}
return 0;
}

Output:

The first ten multiples of 3 are
3 
6 
9 
12 
15 
18 
21 
24 
27 
30

2. do-while loop in C

In the do-while statement, first the instructions in the do block are executed and then the condition in the while block is tested. So, the do while statement will at least execute the code once, even if the condition is false at the very first time.

Syntax:

Its syntax is-

do
{
    //body of the loop
} while (test condition);

Example:

Write a C program to print the cube of the number first five natural numbers using while loop.

#include<stdio.h>
int main()
{
printf("The cube of first 10 number are\n");
int i;

do
{
printf("%d\n",i*i*i);

i++;
} while(i<=5);
return 0;
}

Output:

The cube of first 10 number are
1
8 
27 
64 
125

3. for loop in C

The for loop is the most used repetition statement in almost all programming languages. The reason for its popularity is that it does three jobs at once.

It initializes the counter, tests the condition and increments/decrements the counter at the same time.

This is also the key difference which makes for loop more acceptable over the while and do while loop for most programmers.

Syntax:

Its syntax is-

for (initialize counter; test condition; increment/decrement counter;)
{
    //body of for loop
}

Example:

Write a C program to print the square of the first 10 natural number using for loop.

#include<stdio.h>
int main()
{
int i;
printf("The square of first 10 natural numbers are\n");
for(i=10; i>0; i--)
{
printf("%d\n",i*i);
}
return 0;
}

Output:

The square of first 10 natural numbers are
1 
4
9 
16
25 
36 
49 
64 
81 
100

What is the difference between while, do-while and for loop in C/C++?

  • The while and do-while loop are almost similar in C. Except, for the fact that while tests the condition first and then executes, whereas do-while loop first executes then tests the condition.
  • Block of code inside the while statement may or may not be executed depending on the condition. The block of the code inside do-while always executes the first time.
  • The for loop is very much easy as compared to do and do-while in C/C++ programming. The for loop initialize, test the condition and increment/decreament the counter at the same time. If you look at the most of the code, you will see, for loop has been used over other loops in programming.

What’s next?

Practice solving while, do-while, for loop coding questions

Hope this tutorial has helped you to understand the main difference between while, do-while and for loop in C/C++ along with syntax and C programming example.

Now practise solving coding questions using different loops.

  1. Write a program to display the list of first 20 odd numbers using while, do-while and for loop.
  2. Print the Pyramid of stars using nested for loops.

You can find more programming questions here for practice.

Happy Programming!

Leave a Reply

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