If you want to read data from a network socket until a character sequence is found, you first need to read the data and then check the data for the escape sequence.

do
{ 
   // read data
} while ( /* data is not escape sequence */ );
Answer from Enigma on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › while.html
The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-do-while-loop
Java do while loop | DigitalOcean
August 3, 2022 - The only time you should use a do-while loop is when you want to execute the statements inside the loop at least once, even though condition expression returns false. Otherwise, it’s always better to use a while loop. Java while loop looks cleaner than a do-while loop. That’s all for java do while loop. You should also look into java for loop and java continue statement. Reference: Oracle Documentation
🌐
Google Translate
translate.google.com › translate
The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
class DoWhileDemo { public static void main(String[] args){ int count = 1; do { System.out.println("Count is: " + count); count++; } while (count < 11); } } ... Copyright © 1995, 2024 Oracle and/or its affiliates.
Top answer
1 of 12
7

If you want to read data from a network socket until a character sequence is found, you first need to read the data and then check the data for the escape sequence.

do
{ 
   // read data
} while ( /* data is not escape sequence */ );
2 of 12
7

The while statement continually executes a block of statements while a particular condition is true

while (expression) {
     statement(s)
}

do-while evaluates its expression at the bottom of the loop, and therefore, the statements within the do block are always executed at least once.

do {
     statement(s)
} while (expression);

Now will talk about functional difference,

while-loops consist of a conditional branch instructions such as if_icmpge or if_icmplt and a goto statement. The conditional instruction branches the execution to the instruction immediately after the loop and therefore terminates the loop if the condition is not met. The final instruction in the loop is a goto that branches the byte code back to the beginning of the loop ensuring the byte code keeps looping until the conditional branch is met.

A Do-while-loops are also very similar to for-loops and while-loops except that they do not require the goto instruction as the conditional branch is the last instruction and is be used to loop back to the beginning A do-while loop always runs the loop body at least once - it skips the initial condition check. Since it skips first check, one branch will be less and one less condition to be evaluated.

By using do-while you may gain performance if the expression/condition is complex, since it is ensured to loop atleast once. In that casedo-while could call for performance gain

Very Impressive findings here, http://blog.jamesdbloom.com/JavaCodeToByteCode_PartOne.html#while_loop

Top answer
1 of 3
7

- First to me Iterating and Looping are 2 different things.

Eg: Increment a variable till 5 is Looping.

    int count = 0;

    for (int i=0 ; i<5 ; i++){

        count = count + 1;

   }

Eg: Iterate over the Array to print out its values, is about Iteration

    int[] arr = {5,10,15,20,25};

    for (int i=0 ; i<arr.length ; i++){

        System.out.println(arr[i]);

   }

Now about all the Loops:

- Its always better to use For-Loop when you know the exact nos of time you gonna Loop, and if you are not sure of it go for While-Loop. Yes out there many geniuses can say that it can be done gracefully with both of them and i don't deny with them...but these are few things which makes me execute my program flawlessly...

For Loop :

int sum = 0; 

for (int i = 1; i <= 100; i++) {

  sum += i; 

}

 System.out.println("The sum is " + sum);

The Difference between While and Do-While is as Follows :

- While is a Entry Control Loop, Condition is checked in the Beginning before entering the loop.

- Do-While is a Exit Control Loop, Atleast once the block is always executed then the Condition is checked.

While Loop :

int sum = 0; 
int i = 0;       // i is 0 Here

    while (i<100) {

      sum += i; 
      i++;

    }

  System.out.println("The sum is " + sum);

do-While :

int sum = 0; 
int i = 0;      // i is 0 Here

    do{ 

      sum += i; 
       i++
    }while(i < 100; );

     System.out.println("The sum is " + sum);

From Java 5 we also have For-Each Loop to iterate over the Collections, even its handy with Arrays.

ArrayList<String> arr = new ArrayList<String>();

arr.add("Vivek");
arr.add("Is");
arr.add("Good");
arr.add("Boy");

for (String str : arr){         // str represents the value in each index of arr

    System.out.println(str);     

 }
2 of 3
5

Your for loop looks good.

A possible while loop to accomplish the same thing:

int sum = 0;
int i = 1;
while (i <= 100) {
    sum += i;
    i++;
}
System.out.println("The sum is " + sum);

A possible do while loop to accomplish the same thing:

int sum = 0;
int i = 1;
do {
    sum += i;
    i++;
} while (i <= 100);
System.out.println("The sum is " + sum);

The difference between the while and the do while is that, with the do while, at least one iteration is sure to occur.

🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › flowsummary.html
Summary of Control Flow Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once. The for statement provides a compact way to iterate over a range of values.
🌐
Javatpoint
javatpoint.com › java-do-while-loop
Java Do While Loop - javatpoint
Java do while loop, java do while loop examples, nested do while loop in java, difference between java while loop and do while loop with concepts and examples.
🌐
iO Flood
ioflood.com › blog › do-while-loop-java
Do-While Loop in Java: Usage Guide with Examples
February 20, 2024 - The do-while loop in Java is a control flow statement that allows a block of code to be executed at least once and then repeatedly as long as a certain condition remains true. The loop will continue until the condition becomes false.
Find elsewhere
🌐
W3Schools
w3schools.com › java › java_while_loop_do.asp
Java Do/While Loop
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... The do/while loop is a variant of the while loop. This loop will execute ...
🌐
Tutorialspoint
tutorialspoint.com › java › java_do_while_loop.htm
Java - do...while Loop
Java do while loop is similar to a while loop, except that a do while loop is guaranteed to execute at least one time. The do-while loop is an exit control loop where the condition is checked after executing the loop's body.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › branch.html
Branching Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
An unlabeled break statement terminates the innermost switch, for, while, or do-while statement, but a labeled break terminates an outer statement. The following program, BreakWithLabelDemo, is similar to the previous program, but uses nested for loops to search for a value in a two-dimensional array.
🌐
W3Schools
w3schools.com › java › java_while_loop.asp
Java While Loop
In the next chapter, you will learn about the do while loop, which always runs the code at least once before checking the condition. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › if.html
The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
The while and do-while Statements · The for Statement · Branching Statements · Summary of Control Flow Statements · Questions and Exercises · Trail: Learning the Java Language Lesson: Language Basics Section: Control Flow Statements · Home Page > Learning the Java Language > Language Basics ·
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-do-while-loop-with-examples
Java Do While Loop - GeeksforGeeks
class GFG { public static void main(String args[]) { // Declaring and initialization expression int c = 1; // Do-while loop do { // Body of do-while loop // Print statement System.out.println("Hello World"); // Update expression c++; } // Test expression while (c < 6); } } ... The variable c is initialized to 1 before entering the loop. The do block executes first and prints "Hello World", ensuring the loop runs at least once. After printing, c is incremented using c++. The loop continues as long as the condition c < 6 is true, resulting in the message being printed 5 times. ... import java.io.*; class GFG { public static void main(String[] args) { int c = 1; do { // Only single statement in do block System.out.println("Hello GFG!"); } // This condition is false, so the loop will execute only once while (c >= 3); } }
Published   3 weeks ago
🌐
Oracle
docs.oracle.com › javase › specs › jls › se7 › html › jls-14.html
Chapter 14. Blocks and Statements
September 16, 2025 - The Java programming language, like C and C++ and many programming languages before them, arbitrarily decrees that an else clause belongs to the innermost if to which it might possibly belong. This rule is captured by the following grammar: Statement: StatementWithoutTrailingSubstatement LabeledStatement IfThenStatement IfThenElseStatement WhileStatement ForStatement StatementWithoutTrailingSubstatement: Block EmptyStatement ExpressionStatement AssertStatement SwitchStatement DoStatement BreakStatement ContinueStatement ReturnStatement SynchronizedStatement ThrowStatement TryStatement StatementNoShortIf: StatementWithoutTrailingSubstatement LabeledStatementNoShortIf IfThenElseStatementNoShortIf WhileStatementNoShortIf ForStatementNoShortIf
🌐
LinkedIn
linkedin.com › learning › oracle-java-certification-2-operators-and-decision-statements › while-and-do-while
While and do-while - Java Video Tutorial | LinkedIn Learning, formerly Lynda.com
The key difference between a while loop and a do while loop is that a do while loop executes its loop body at least once, because the loop condition is checked after each iteration.
Published   September 12, 2019
🌐
Programiz
programiz.com › java-programming › do-while-loop
Java while and do...while Loop
If the textExpression evaluates to true, the code inside the while loop is executed. The textExpression is evaluated again. This process continues until the textExpression is false. When the textExpression evaluates to false, the loop stops. To learn more about the conditions, visit Java relational and logical operators.
🌐
Iqra Technology
iqratechnology.com › academy › java › java-basic › java-while-do-while-loop
Master Java Loops: While & Do-While with Examples
June 13, 2025 - A while loop checks the condition first, whereas a do-while loop runs at least once before checking. 6. Can a while loop skip execution if the condition is false? Yes, it skips the code block if the condition is initially false.