Home  >  Article  >  Java  >  Do-while statement usage and examples in Java

Do-while statement usage and examples in Java

WBOY
WBOYforward
2023-04-27 10:13:072189browse

Explanation

The do-while statement consists of the keywords do and while. It is the most typical loop-first-then-judge process control structure among loop statements. It is different from the other two loop statements. .

Execution process

When the do-while statement is executed, the loop body is executed first, and then the loop condition is judged. If the loop condition is not true, the loop ends. If the loop condition is true, the loop body will continue executing. After the execution of the loop body is completed, it will be deduced in sequence.

Example

do-while can execute the statement at least once, and determine whether to enter the next loop based on the condition in the while.

int i = 1;
int sum = 0;
do {
    sum += i;
    i++;
} while (i <= 100);
System.out.println("1累加到100的结果是:" + sum);

No matter whether i is less than or equal to 100, sum = i;.

will be performed first.

The above is the detailed content of Do-while statement usage and examples in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete