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

Do-while statement usage and examples in Java

WBOY
Release: 2023-04-27 10:13:07
forward
2326 people have browsed it

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);
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template