Difference between For, While and Do-While Loop in Programming Last Updated : 12 Apr, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: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: C++ #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); } Output0 1 2 3 4 While Loop in Programming: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: C++ #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"); Output0 1 2 3 4Do-While Loop in Programming: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: C++ #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); OutputFinal value of count = 6Difference between For, While and Do-While Loop in Programming:Featurefor Loopwhile Loopdo-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 More infoAdvertise with us Next Article Difference Between For Loop and Do while Loop in Programming A abhaystriver Follow Improve Article Tags : DSA Similar Reads Difference between While Loop and Do While Loop in Programming While loop and Do while loop concepts are fundamental to control flow in programming, allowing developers to create loops that repeat a block of code based on certain conditions. The choice between "while" and "do while" depends on the specific requirements of the program and the desired behavior of 5 min read Difference Between For Loop and Do while Loop in Programming For loop and Do while loop are control flow structures in programming that allow you to repeatedly execute a block of code. However, they differ in their syntax and use cases. It is important for a beginner to know the key differences between both of them. For Loop in Programming:The for loop is use 4 min read Difference between For Loop and While Loop in Programming Both for loops and while loops are control flow structures in programming that allow you to repeatedly execute a block of code. However, they differ in their syntax and use cases. It is important for a beginner to know the key differences between both of them. Difference between For Loop and While L 3 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 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 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 Like