Java while and do-while statements (translated from Java tutorials)

Source: Internet
Author: User

From http://www.cnblogs.com/ggjucheng/archive/2012/12/16/2820841.html

English from http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

The while statement continuously executes the statement in the block when the specific condition is true. Its statement can be expressed as follows:

 
While (expression) {Statement (s )}

A boolean value must be returned for the expression calculated by the while statement. If the expression is calculated as true, the while statement executes all the statements in the while block. The while statement continues to test the expression, and then executes its blocks until the expression is calculated as false. Use the while statement to print the values from 1 to 10, as shown in figureWhiledemoProgram:

 
Class whiledemo {public static void main (string [] ARGs) {int COUNT = 1; while (count <11) {system. out. println ("count is:" + count); count ++ ;}}}

Use the while statement to implement an infinite loop:

 
While (true) {// your code goes here}

JavaProgramming LanguageThe do-while statement is also supported. The statement is as follows:

Do {Statement (s)} while (expression );

The difference between the do-while statement and the while statement is that the expression calculated by do-while is at the bottom of the loop rather than the top. Therefore, the do block statement will be executed at least once, as shown in figureThe dowhiledemo program is as follows:

 
Class dowhiledemo {public static void main (string [] ARGs) {int COUNT = 1; do {system. out. println ("count is:" + count); count ++;} while (count <11 );}}

 

Related Article

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.