Last Updated : 23 Jul, 2025
For Loop, While Loop, and Do-While Loop are different loops in programming. A For loop is used when the number of iterations is known. A While loop runs as long as a condition is true. A Do-While loop runs at least once and then continues if a condition is true.
For Loop in Programming:for
loop is used when you know in advance how many times you want to execute the block of code.variable
) takes the value of each item in the sequence during each iteration.for (initialization; condition; increment/decrement) {Examples: C++
// Code to be executed repeatedly
}
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i < 5; i++)
cout << i << "\n";
return 0;
}
Java
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
}
}
Python3
for i in range(5):
print(i)
C#
using System;
class Program {
static void Main() {
for (int i = 0; i < 5; i++) {
Console.WriteLine(i);
}
}
}
JavaScript
for (let i = 0; i < 5; i++) {
console.log(i);
}
While Loop in Programming:
while
loop is used when you don’t know in advance how many times you want to execute the block of code. It continues to execute as long as the specified condition is true.The syntax of a while loop is straightforward:
while (condition){Examples: C++
# Code to be executed while the condition is true
}
#include <iostream>
using namespace std;
int main()
{
int count = 0;
while (count < 5) {
cout << count << "\n";
count += 1;
}
cout << endl;
return 0;
}
Java
public class Main {
public static void main(String[] args) {
int count = 0;
// Example: While loop to print numbers from 0 to 4
while (count < 5) {
System.out.println(count);
count += 1;
}
System.out.println();
}
}
Python3
count = 0
while count < 5:
print(count)
count += 1
C#
using System;
class Program
{
static void Main(string[] args)
{
int count = 0;
while (count < 5)
{
Console.WriteLine(count);
count += 1;
}
Console.WriteLine();
}
}
JavaScript
let count = 0;
while (count < 5) {
console.log(count);
count += 1;
}
console.log("Loop completed");
Do-While Loop in Programming:
do {Examples: C++
// body of do-while loop
} while (condition);
#include <iostream>
using namespace std;
int main()
{
int count = 5;
do {
count += 1;
} while (count < 5);
cout << "Final value of count = " << count;
return 0;
}
Java
public class Main {
public static void main(String[] args) {
// Initialize count to 5
int count = 5;
// Do-while loop: increment count while it is less than 5
do {
count += 1; // Increment count
} while (count < 5);
// Print the final value of count
System.out.println("Final value of count = " + count);
}
}
Python3
count = 5
while True:
count += 1
if not count < 5:
break
print("Final value of count =", count)
C#
using System;
class Program
{
static void Main()
{
// Initialize count to 5
int count = 5;
// Do-while loop
do
{
// Increment count by 1
count += 1;
} while (count < 5); // Continue while count is less than 5
// Output final value of count
Console.WriteLine("Final value of count = " + count);
}
}
JavaScript
// Initialize count to 5
let count = 5;
// Do-while loop: execute at least once and continue while count is less than 5
do {
count += 1; // Increment count
} while (count < 5);
// Print the final value of count
console.log("Final value of count = " + count);
Final value of count = 6Difference between For, While and Do-While Loop in Programming: Feature
for
Loop while
Loop
do-while
Loop
Syntax
for (initialization; condition; increment/decrement) {}
while (condition) { }
do { } while (condition);
Initialization Declared within the loop structure and executed once at the beginning. Declared outside the loop; should be done explicitly before the loop.Declared outside the loop structure
Condition Checked before each iteration. Checked before each iteration.Checked after each iteration.
Update Executed after each iteration. Executed inside the loop; needs to be handled explicitly.Executed inside the loop; needs to be handled explicitly.
Use Cases Suitable for a known number of iterations or when looping over ranges. Useful when the number of iterations is not known in advance or based on a condition.Useful when the loop block must be executed at least once, regardless of the initial condition.
Initialization and Update Scope Limited to the loop body. Scope extends beyond the loop; needs to be handled explicitly.Scope extends beyond the loop; needs to be handled explicitly.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4