"Java" talking about while and do-while

Source: Internet
Author: User

While syntax format:
while (Boolean expression) {
Statement
}
The Boolean expression is judged first, if true, the statement in the loop body is executed, then the Boolean expression is determined until the Boolean expression is false and the loop ends.
Usually with arithmetic operator (+ +--decrement)

Class Whiledemo{public static void Main (String [] args) {int x=1;while (x<4) {System.out.println ("OK"); x + +;}}

Do-while Syntax Format:
do{
Statement
}while (boolean expression);
Executes the loop body first, then determines whether the Boolean expression is true, and if so, resumes execution of the loop body, judging the Boolean expression until false to end the loop.

Class Dowhiledemo{public static void Main (String [] args) {int x=1;do{system.out.println ("OK Do_while"); x + +;} while (x<4);}}


The difference between the two: while is the first judgment in the execution if the judgment is not established, will not be executed; Do/while is executed first in judgment, regardless of whether the judgment is established.

"Java" talking about while and do-while

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.