VOOZH about

URL: https://www.geeksforgeeks.org/dsa/difference-between-for-while-and-do-while-loop-in-programming/

⇱ Difference between For, While and Do-While Loop in Programming - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Difference between For, While and Do-While Loop in Programming

Last Updated : 23 Jul, 2025

For Loop,While Loop, and Do-WhileLoop 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.

  • The for loop is used when you know in advance how many times you want to execute the block of code.
  • It iterates over a sequence (e.g., a list, tuple, string, or range) and executes the block of code for each item in the sequence.
  • The loop variable (variable) takes the value of each item in the sequence during each iteration.

For Loop Syntax:

for (initialization; condition; increment/decrement) {
// Code to be executed repeatedly
}

Examples:


Output
0
1
2
3
4
  • The 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.
  • It’s important to make sure that the condition eventually becomes false; otherwise, the loop will run indefinitely, resulting in an infinite loop.

While Loop Syntax:

The syntax of a while loop is straightforward:

while (condition){
# Code to be executed while the condition is true
}

Examples:


Output
0
1
2
3
4
  • The do-while loop is similar to the while loop, but with one key difference: it guarantees that the block of code will execute at least once before checking the condition.
  • This makes it useful when you want to ensure that a certain task is performed before evaluating a condition for continuation.
  • The loop continues to execute as long as the specified condition is true after the first execution. It's crucial to ensure that the condition eventually becomes false to prevent the loop from running indefinitely, leading to an infinite loop.

Syntax of do…while Loop:

do {

// body of do-while loop

} while (condition);

Examples:


Output
Final value of count = 6
Featurefor Loopwhile Loop

do-while Loop

Syntax

for (initialization; condition; increment/decrement) {}

while (condition) { }

do { } while (condition);

InitializationDeclared 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

ConditionChecked before each iteration.Checked before each iteration.

Checked after each iteration.

UpdateExecuted after each iteration.Executed inside the loop; needs to be handled explicitly.

Executed inside the loop; needs to be handled explicitly.

Use CasesSuitable 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 ScopeLimited to the loop body.Scope extends beyond the loop; needs to be handled explicitly.

Scope extends beyond the loop; needs to be handled explicitly.

Comment
Article Tags:
Article Tags: