Difference between for and while loop in C, C++, Java
Last Updated :
04 Sep, 2023
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
// C Program to illustrate the use of for loop
#include <stdio.h>
int main()
{
// loop variable
int i = 0;
// for loop that prints "GFG" 5 times
for (i = 5; i < 10; i++) {
printf("GFG\n");
}
return 0;
}
C++
// CPP program to demonstrate
// for loop
#include <iostream>
using namespace std;
int main()
{
// Initialize variable 'i' with a value of 0
int i = 0;
// Iterate using a for loop from 5 to 9 (inclusive)
for (i = 5; i < 10; i++) {
// Print "GFG" on each iteration
cout << "GFG\n";
}
return 0;
}
Java
// Java program to illustrate the use of for loop
import java.io.*;
class GFG {
public static void main(String[] args)
{
// loop variable
int i = 0;
// for loop
for (i = 5; i < 10; i++) {
System.out.println("GfG");
}
}
}
OutputGFG
GFG
GFG
GFG
GFG
Looping Infinite Times
C
// C program to demonstrate
// infinite for loop
#include <stdio.h>
int main()
{
// Infinite loop using a for loop with no condition
// specified The absence of a condition means the loop
// will continue indefinitely until it is explicitly
// interrupted or terminated.
for (;;) {
printf("GFG\n");
}
return 0;
}
C++
// C++ program to demonstrate
// infinite for loop
#include <iostream>
using namespace std;
int main()
{
// Infinite loop using a for loop with no condition
// specified The absence of a condition means the loop
// will continue indefinitely until it is explicitly
// interrupted or terminated.
for (;;) {
// Print "GFG" on each iteration
cout << "GFG\n";
}
return 0;
}
Java
// Java program to illustrate the use of for loop
import java.io.*;
class GFG {
public static void main(String[] args)
{
// infinite for loop
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
// C program to demonstrate while loop
#include <stdio.h>
int main()
{
// loop variable definition
int i = 5;
// while loop that prints "GFG" 5 times
while (i < 10) {
printf("GFG\n");
i++;
}
return 0;
}
C++
// C++ program to demonstrate
// while loop
#include <iostream>
using namespace std;
int main()
{
// Initialize variable 'i' with a value of 5
int i = 5;
// Execute the loop as long as 'i' is less than 10
while (i < 10) {
// Increment 'i' by 1 on each iteration
i++;
// Print "GFG" on each iteration
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
// C program to domonstrate
// infinite while loop
#include <stdio.h>
int main()
{
// Condition is always true which results in infinite
// loop
while (1)
printf("GFG\n");
return 0;
}
C++
// C++ program to domonstrate
// infinite while loop
#include <iostream>
using namespace std;
int main()
{
// Condition is always true which results in infinite
// loop
while (1)
cout << "GFG\n";
return 0;
}
Java
// C++ program to domonstrate
// infinite while loop
import java.io.*;
class GFG {
public static void main(String[] args)
{
// loop variable
int i = 5;
// while loop
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. |
Similar Reads
Difference between for and do-while loop in C, C++, Java 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 con
2 min read
Difference between while and do-while loop in C, C++, Java 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:Example:C++ #include <iostream> usin
2 min read
Difference between while(1) and while(0) in C language Prerequisite: while loop in C/C++ In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The boolean condition is either true or false. while(1) It is an infinite loop which will run till a break
3 min read
What is the difference between for and Foreach loop in PHP ? Loops can be used to iterate over collection objects in PHP. The for and foreach loop can be used to iterate over the elements. for loopThe for loop works at the end of the given condition. It is used for the implementation of variables and works in a single way. The for loop does not work in the ca
3 min read
Difference between Sentinel and Counter Controlled Loop in C Sentinel Controlled Loop A sentinel controlled loop is also called an indefinite repetition loop because the number of iterations is not known before the loop starts executing. In a sentinel controlled loop, a special value called sentinel value is used to change the loop control expression from tru
3 min read
Difference between Recursion and Iteration A program is called recursive when an entity calls itself. A program is called iterative when there is a loop (or repetition).Example: Program to find the factorial of a number C++ // C++ program to find factorial of given number #include<bits/stdc++.h> using namespace std; // ----- Recursion
6 min read
Java for loop vs Enhanced for loop In Java, loops are fundamental constructs for iterating over data structures or repeating blocks of code. Two commonly used loops are the for loop and the enhanced for loop. While they serve similar purposes, their applications and usage vary based on the scenario.for loop vs Enhanced for loopBelow
4 min read
JavaScript do...while Loop A do...while loop in JavaScript is a control structure where the code executes repeatedly based on a given boolean condition. It's similar to a repeating if statement. One key difference is that a do...while loop guarantees that the code block will execute at least once, regardless of whether the co
4 min read
Solidity - While, Do-While, and For Loop Loops are used when we have to perform an action over and over again. While writing a contract there may be a situation when we have to do some action repeatedly, In this situation, loops are implemented to reduce the number of lines of the statements. Solidity supports following loops too ease down
3 min read
Understanding for loops in Java Suppose it is required to print numbers from 1 to 5. One possible way to do this is with the help of below code: Java class GFG { public static void main(String args[]) { int a; a = 1; System.out.println(a); a=2; System.out.println(a); a=3; System.out.println(a); a=4; System.out.println(a); a=5; Sys
4 min read