Difference between while and do-while loop in C, C++, Java Last Updated : 17 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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> using namespace std; int main() { int i = 5; while (i < 10) { i++; cout << "GFG\n"; } return 0; } C #include <stdio.h> int main() { int i = 5; while (i < 10) { printf("GFG\n"); i++; } return 0; } Java import java.io.*; class GFG { public static void main(String[] args) { int i = 5; while (i < 10) { i++; System.out.println("GfG"); } } } OutputGFG 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 <iostream> using namespace std; int main() { int i = 5; do { i++; cout << "GFG\n"; } while (i < 10); return 0; } C #include <stdio.h> int main() { int i = 5; do { printf("GFG\n"); i++; } 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); } } OutputGFG GFG GFG GFG GFG Here is the difference table:whiledo-whileCondition is checked first then statement(s) is executed.Statement(s) is executed atleast once, thereafter condition is checked.It might occur statement(s) is executed zero times, If condition is false.At least once the statement(s) is executed.No semicolon at the end of while. while(condition)Semicolon at the end of while. while(condition);Variable in condition is initialized before the execution of loop.variable may be initialized before or within the loop.while loop is entry controlled loop.do-while loop is exit controlled loop.while(condition) { statement(s); }do { statement(s); } while(condition); Comment More infoAdvertise with us Next Article Difference between while and do-while loop in C, C++, Java P pp_pankaj Follow Improve Article Tags : Java Difference Between Programming Language C Language C++ Computer Science Fundamentals DSA Loops & Control Structure +4 More Practice Tags : CPPJava Similar Reads 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 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(1) and while(0) in C language Prerequisite: while loop in C/C++ In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The boolean condition is either true or false. while(1) It is an infinite loop which will run till a break 3 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 Difference between scanf() and gets() in C scanf()It is used to read the input(character, string, numeric data) from the standard input(keyboard).It is used to read the input until it encounters a whitespace, newline or End Of File(EOF). For example see the following code:Â C // C program to see how scanf() // stops reading input after white 3 min read Difference between C++ and Go C++ was developed by Bjarne Stroustrup at Bell Labs in 1979 as an extension of the C languageC++ is a general-purpose programming language and is widely used nowadays for competitive programming. It has imperative, object-oriented, and generic programming features. C++ is a widely popular language a 2 min read Similarities Between Java and C++ Both are very successful and popular programming languages. Though there are many differences between the both, there are considerable similarities which are given as follows: 1. Both C++ and Java support Object Oriented Programming OOPs is a modular approach, which allows the data to be applied wit 4 min read Solidity - While, Do-While, and For Loop Loops are used when we have to perform an action over and over again. While writing a contract there may be a situation when we have to do some action repeatedly, In this situation, loops are implemented to reduce the number of lines of the statements. Solidity supports following loops too ease down 3 min read do...while Loop in C C do...while loop is a type of loop that executes a code block until the given condition is satisfied. Unlike the while loop, which checks the condition before executing the loop, the do...while loop checks the condition after executing the code block, ensuring that the code inside the loop is execu 4 min read Java Do While Loop Java do-while loop is an Exit control loop. Unlike for or while loop, a do-while check for the condition after executing the statements of the loop body.Example:Java// Java program to show the use of do while loop public class GFG { public static void main(String[] args) { int c = 1; // Using do-whi 4 min read Like