Java language-differences between three types of loop statements and java-three types of statements

Source: Internet
Author: User

Java language-differences between three types of loop statements and java-three types of statements

------- Android training, java training, and hope to communicate with you! ----------

 

 

First: for Loop

Loop Structure for statement format: for (initialization expression; conditional expression; Operation expression after loop) {loop body;} eg:
1 class Dome_For2 {2 public static void main (String [] args) {3 // System. out. println ("Hello World! "); 4 // calculate the even numbers of 1-10 and 5 int sum = 0; 6 for (int I = 1; I <= 10; I ++) {7 if (I % 2 = 0) {// judge Statement 8 sum + = I; // sum 9} 10} 11 System. out. println (sum); 12} 13}

The output structure is 30.

The second while statement

Loop structure while statement format:

Initialization statement;

While (Judgment Condition Statement) {loop body statement; Control Condition Statement;} eg:
1 class Demo_While {2 public static void main (String [] args) {3 // calculate the sum of 1-100 4 int sum = 0; // define the initial sum as 0 5 int I = 1; // define the first number of 6 while (I <= 100) {// judgment Condition Statement 7 sum + = I; // sum = sum + I; 8 I ++; // Let variable I auto increment 9} 10 System. out. println ("sum =" + sum); 11} 12}

Output result: sum = 5050

Third do... while statement

Loop Structure do... while statement format:

Initialization statement; do {loop body statement; Control Condition Statement;} while (Judgment Condition Statement); eg:
1 class demodomaindowhile {2 public static void main (String [] args) {3 // evaluate the sum of 1-and 4 int sum = 0; // define the variable sum, 5 int I = 1; // defines the variable I 6 do {// do is dry 7 // System. out. println ("I =" + I); // loop body Statement 8 sum + = I; 9 I ++; 10} 11 while (I <= 100 ); // judgment Condition Statement 12 System. out. println ("sum =" + sum); // output result 13} 14}

Output result: sum = 5050

Summary:  The differences between the three loop statements:

1. do... while loop executes at least one loop body.
2. for a while loop, you must first determine whether the condition is true, and then decide whether to execute the loop body statement.

 

 

 

------- Windows Phone 7 Mobile Phone development,. Net training, and we look forward to communicating with you! -------

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.