Difference Between For Loop and Do while Loop in Programming Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report 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 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 loop to print numbers from 0 to 4 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 loop to print numbers from 0 to 4 for (let i = 0; i < 5; i++) { console.log(i); } Output0 1 2 3 4 Do-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 Loop and Do while Loop:Featuresfor loopdo-while loopInitializationInitializes before loop startsNo initialization before loop startsCondition CheckingChecked before executing loop bodyChecked after executing loop bodyExecutionWill not execute if condition is initially falseAlways executes loop body at least onceExamplefor(int i = 0; i < 5; i++) { /* loop body */ }int i = 0; do { /* loop body */ } while(i < 5);Use CasesKnown and finite iterationsMust execute loop body at least onceControl Variable UpdatesUpdated within loop body or update expressionUpdated within loop body, typically at endTerminating ConditionCondition becoming falseCondition becoming false, but always runs onceComplexityPreferred for collections or known rangesUseful for ensuring initial execution Create Quiz Comment S savita8z3a3 Follow 1 Improve S savita8z3a3 Follow 1 Improve Article Tags : DSA Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 3 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 15 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 1 min read Problem of The Day - Develop the Habit of Coding 5 min read Like