for loop:
for loop provides a concise way of writing the loop structure. Unlike a while loop, a for statement consumes the initialization, condition and increment/decrement in one line thereby providing a shorter, easy to debug structure of looping.
Syntax:
for (initialization condition; testing condition;
increment/decrement)
{
statement(s)
}
Flowchart:

Example:
C
#include <stdio.h>
int main()
{
int i = 0;
for (i = 5; i < 10; i++) {
printf("GFG\n");
}
return 0;
}
|
C++
#include <iostream>
using namespace std;
int main()
{
int i = 0;
for (i = 5; i < 10; i++) {
cout << "GFG\n";
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int i = 0;
for (i = 5; i < 10; i++) {
System.out.println("GfG");
}
}
}
|
Output:
GFG
GFG
GFG
GFG
GFG
do-while loop:
do while loop is similar to while loop with the only difference that it checks for the condition after executing the statements, and therefore is an example of Exit Control Loop.
Syntax:
do
{
statements..
}
while (condition);
Flowchart:

Example:
C
#include <stdio.h>
int main()
{
int i = 5;
do {
printf("GFG\n");
i++;
} while (i < 10);
return 0;
}
|
C++
#include <iostream>
using namespace std;
int main()
{
int i = 5;
do {
i++;
cout << "GFG\n";
} while (i < 10);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int i = 5;
do {
i++;
System.out.println("GfG");
} while (i < 10);
}
}
|
Output:
GFG
GFG
GFG
GFG
GFG
Here is the difference table:
| For loop |
Do-While loop |
| Statement(s) is executed once the condition is checked. |
Condition is checked after the statement(s) is executed. |
| It might be that statement(s) gets executed zero times. |
Statement(s) is executed at least once. |
| For the single statement, bracket is not compulsory. |
Brackets are always compulsory. |
| Initialization may be outside or in condition box. |
Initialization may be outside or within the loop. |
| for loop is entry controlled loop. |
do-while is exit controlled loop. |
for ( init ; condition ; iteration )
{ statement (s); } |
do { statement(s); }
while (condition); |
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
27 Jun, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...