Cyclic--WHILE loop structure in Java

Source: Internet
Author: User

1. Three elements of the cycle :

1) Initialization of the cyclic variables;

2) The conditions of the cycle;

3) superposition of cyclic variables;

2. syntax for the while loop:

while (Boolean expression) {

statement block;

}

Execution order:

1. Calculate the Boolean value;

2. If the Boolean value is ture, execute the statement block; after execution, in the calculation of the Boolean value, if it is ture, continue to execute the statement block, and so on, know that the Boolean value is False, the loop ends.

Example: a training institution with 250,000 people this year, an annual growth of 25%, how many years will it take to increase the number to 1 million?

public class Nian {
public static void Main (String[]args) {

Double ren=25;
int year=2012;
while (ren<=100) {
ren=ren* (1+0.25);
year++;
}SYSTEM.OUT.PRINTLN (year);
}
}

3. do and loop syntax:

do{

statement block;}while (boolean expression);

Execution order:

1. Execute the statement block first;

2. Computes the Boolean value for Ture to continue executing the statement block, and then computes the Boolean value, after executing the statement block, and so on, until the Boolean value is False, ending the loop.

Example:

public static void Main (String[]args) {
Double hua=0; int num=0;
Double she=0;
do{
if (num<10) {
SYSTEM.OUT.PRINTLN ("Celsius" +she+ "\ T" + "Fahrenheit" + (hua=she*9/5.0+32));
}
she=she+20;
num++;
}while (she<=250);

}

3. the difference between the while and do while:

Two statements are used for the execution of the loop structure, the difference is that while the loop condition is judged by the true and false, in the execution of the statement block, and does while the condition is true or FALSE, will first execute a statement block, and then in the judgment condition of the true and false, in the loop.

Cyclic--WHILE loop structure in Java

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.