General Structure
The do-while loop executes the statements at least once before checking the condition.
The general form is:
do { instruções; } while(condição);
Difference between for and while loops
Simple Example
A program that loops until the user enters the letter 'q':
// Demonstra o laço do-while. class DWDemo { public static void main(String args[]) throws java.io.IOException { char ch; do { System.out.print("Press a key followed by ENTER: "); ch = (char) System.in.read(); // obtém um char } while(ch != 'q'); } }
Improved Guessing Game
A guessing program that loops until the user guesses the correct letter:
see Guess4.java
Explanation of the Divination Program
Execution Example
Typical execution of the guessing program
I'm thinking of a letter between A and Z. Can you guess it: A ...Sorry, you're too low Try again! I'm thinking of a letter between A and Z. Can you guess it: Z ...Sorry, you're too high Try again! I'm thinking of a letter between A and Z. Can you guess it: K ** Right **
Important Detail
Conclusion
The above is the detailed content of The do-while loop in Java. For more information, please follow other related articles on the PHP Chinese website!