Difference between for and do-while loop in C, C++, Java Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 condition; increment/decrement) { statement(s) } Flowchart: Example: C #include <stdio.h> int main() { int i = 0; for (i = 5; i < 10; i++) { printf("GFG\n"); } return 0; } C++ #include <iostream> using namespace std; int main() { int i = 0; for (i = 5; i < 10; i++) { cout << "GFG\n"; } return 0; } Java import java.io.*; class GFG { public static void main(String[] args) { int i = 0; for (i = 5; i < 10; i++) { System.out.println("GfG"); } } } Output: GFG GFG GFG GFG GFG do-while loop: do while loop is similar to while loop with the only difference that it checks for the condition after executing the statements, and therefore is an example of Exit Control Loop. Syntax: do { statements.. } while (condition); Flowchart: Example: C #include <stdio.h> int main() { int i = 5; do { printf("GFG\n"); i++; } while (i < 10); return 0; } C++ #include <iostream> using namespace std; int main() { int i = 5; do { i++; cout << "GFG\n"; } while (i < 10); return 0; } Java import java.io.*; class GFG { public static void main(String[] args) { int i = 5; do { i++; System.out.println("GfG"); } while (i < 10); } } Output: GFG GFG GFG GFG GFG Here is the difference table: For loop Do-While loop Statement(s) is executed once the condition is checked. Condition is checked after the statement(s) is executed. It might be that statement(s) gets executed zero times. Statement(s) is executed at least once. For the single statement, bracket is not compulsory. Brackets are always compulsory. Initialization may be outside or in condition box. Initialization may be outside or within the loop. for loop is entry controlled loop. do-while is exit controlled loop. for ( init ; condition ; iteration ) { statement (s); } do { statement(s); } while (condition); Comment More infoAdvertise with us Next Article Difference between Sentinel and Counter Controlled Loop in C P pp_pankaj Follow Improve Article Tags : DSA loop Loops & Control Structure Similar Reads Difference between for and while loop in C, C++, Java 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 prov 5 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 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 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 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 Nested Loops in C++ with Examples Nested loop means a loop statement inside another loop statement. That is why nested loops are also called as "loop inside loop". Syntax for Nested For loop: for ( initialization; condition; increment ) { for ( initialization; condition; increment ) { // statement of inside loop } // statement of ou 3 min read Like