Home >Common Problem >What is the usage of do while statement?

What is the usage of do while statement?

coldplay.xixi
coldplay.xixiOriginal
2020-08-15 10:06:4148421browse

The usage of do while statement is: first execute the statement in the loop body; then judge whether the condition is true, if it is true, continue the loop, if it is false, terminate the loop.

What is the usage of do while statement?

The usage of do while statement is:

1. The difference between do-while loop and while loop is: It first executes the statements in the loop body and then determines whether the condition is true. If true, the loop continues, if false, the loop is terminated. Therefore, the do-while loop must execute the loop statement at least once. Similarly, when there are many statements participating in a loop, they must be enclosed by "{" and "}".

The general syntax of a do-while loop is:

do{
语句;
}while(条件)

Note: A semicolon must be added after the expression of the do-while statement.

Example 1: Write a guessing game that requires guessing a number between 1 and 10. Compare the number guessed by the user with the standard value, and give tips so that the next guess can be closer. Standard value until guessed correctly.

In the Java directory of drive D, create a new file "GuessSample.java". Open the "GuessSample.java" file with Notepad and enter the following code:

What is the usage of do while statement?

2. Code structure analysis

The program function is mainly to demonstrate the do-while loop Use of statements.

The program internally sets a standard value (answer) to be stored in the variable number, and declares a guess variable to receive the value guessed by the user. The do-while structure can ensure that the user can guess the number at least once. In the loop body, the user is prompted to enter the number to guess. After the user enters the guessed value, the if-else statement and the correct answer are used to judge. If the user guesses If the value is greater than the answer, the user is prompted that the number is too large; if the value guessed by the user is less than the answer, the user is prompted that the number is too small.

The expression after the while statement (guess != number) is used to determine whether the loop condition is met. If the value entered by the user is not equal to the answer, the expression returns true and the loop body continues to execute. The user is required to continue guessing according to the prompts. Otherwise, the result returned by the expression is false, the loop condition is not satisfied, and the program no longer executes the loop body, but directly executes the statements following the loop body.

Compile the "GuessSample.java" file, enter "javac GuessSample.java" in the command line window and execute the command. After the compilation is passed, enter "java GuessSample" in the command line window to run the Java program, and the command line window displays The following information:

What is the usage of do while statement?

3. From the above results, it can be seen that when the input value is not equal to the answer, the program will give corresponding prompts and continue to execute the loop. When the value entered by the user is equal to the answer, the program no longer executes the loop and outputs the guessed answer.

What is the usage of do while statement?

Related recommendations: Programming Video Course

The above is the detailed content of What is the usage of do while statement?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn