In C, C++, and Java, both for loop and while loop is used to repetitively execute a set of statements a specific number of times. However, there are differences in their declaration and control flow. Let’s understand the basic differences between a for loop and a while loop.
for Loop
A for loop provides a concise way of writing the loop structure. Unlike a while loop, a for loop declaration 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 of for Loop

Example: Program to Demonstrate How to Use for Loop
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" );
}
}
}
|
OutputGFG
GFG
GFG
GFG
GFG
Looping Infinite Times
C
#include <stdio.h>
int main()
{
for (;;) {
printf ( "GFG\n" );
}
return 0;
}
|
C++
#include <iostream>
using namespace std;
int main()
{
for (;;) {
cout << "GFG\n" ;
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
for (;;) {
System.out.println( "GFG!" );
}
}
}
|
Output
GFG
GFG
GFG
...
...
...
{truncated}
while Loop
A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.
Syntax
while (boolean condition)
{
// loop statements...
}
Flowchart of while Loop

Example: CPP Program to Demonstrate while Loop
C
#include <stdio.h>
int main()
{
int i = 5;
while (i < 10) {
printf ( "GFG\n" );
i++;
}
return 0;
}
|
C++
#include <iostream>
using namespace std;
int main()
{
int i = 5;
while (i < 10) {
i++;
cout << "GFG\n" ;
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int i = 5 ;
while (i < 10 ) {
i++;
System.out.println( "GfG" );
}
}
}
|
OutputGFG
GFG
GFG
GFG
GFG
Looping Infinite Times
C
#include <stdio.h>
int main()
{
while (1)
printf ( "GFG\n" );
return 0;
}
|
C++
#include <iostream>
using namespace std;
int main()
{
while (1)
cout << "GFG\n" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int i = 5 ;
while (i < 10 ) {
System.out.println( "GFG\n" );
}
}
}
|
Output
GFG
GFG
GFG
...
...
...
{truncated}
Difference Between for Loop and while Loop
The major differences between for loop and while loop in C, C++ and Java are as follows:
for Loop | while Loop |
---|
Initialization may be either in the loop statement or outside the loop. | Initialization is always outside the loop. |
Once the statement(s) is executed then increment is done. | The increment can be done before or after the execution of the statement(s). |
It is normally used when the number of iterations is known. | It is normally used when the number of iterations is unknown. |
Condition is a relational expression. | The condition may be an expression or non-zero value. |
It is used when initialization and updation of conditions are simple. | It is used for complex initialization. |
For loop is entry controlled loop. | While loop is also entry controlled loop. |
Syntax: for ( init ; condition ; iteration ) { statement(s); } | Syntax: while ( condition ) { statement(s); } |
The for loop is used when the number of iterations is known. | The while loop is used when the number of iterations is unknown. |