Java Learning (6), loop structure, and java learning loop structure

Source: Internet
Author: User

Java Learning (6), loop structure, and java learning loop structure

1. while Loop: Also known as "When Type Loop"

While (loop condition ){

// Cyclic operation statement

}

Key points: ① judge first and then execute ② the number of cycles is not fixed ③ avoid endless Loops

Example: enter a positive integer and output it in reverse order. For example, input 12345 and output 54321.

1 import java. util. optional; 2 public class dowhileforDemo {3 public static void main (String [] args) 4 {5 // enter a positive integer and output it in reverse order, for example, enter 12345, output 54321 6 bytes input = new partition (System. in); 7 System. out. println ("enter a positive integer"); 8 int number = input. nextInt (); 9 while (number! = 0) 10 {11 int res = number % 10; 12 System. out. print (res); 13 // number = number/10; 14 number/= 10; 15} 16} 17}View Code

2. do while LOOP: It is also known as a type-to-type loop.

Key points: ① execute first and then judge ② execute at least once ③ do not miss the last semicolon

Do {

// Cyclic operation statement

} While (loop statement );

Example: Calculate the sum of n consecutive natural numbers starting from 1. When the sum of the two numbers is just over 100, calculate the value of n.

1 public class dowhileforDemo {2 public static void main (String [] args) 3 {4 // calculates the sum of n consecutive natural numbers starting from 1, and ends when the sum of the values is just over 100, evaluate the value of n 5 int number2 = 0; 6 int sum = 0; 7 do {8 number2 ++; 9 sum + = number2; 10} while (sum <= 100 ); 11 System. out. println ("sum =" + sum); 12 System. out. println ("number2 =" + number2); 13} 14}View Code

3. for Loop

For (expression 1; expression 2; expression 3 ){

// Cyclic operation;

}

Expression 1: Initialize the parameter and assign values to the variable.

Expression 2: Conditional judgment

Expression 3: update the loop body variable to prevent endless loops.

Execution sequence: 1. parameter initialization 2. Condition judgment 3. cyclic operation 4. Update cyclic body variable 5. Condition Judgment 6. cyclic Operation 7. Update cyclic body variable .... (Repeated steps 2, 3, and 4)

Example: Print 1-10

1 public class dowhileforDemo {2 public static void main (String [] args) 3 {4 // print 1-10 5 for (int I = 1; I <= 10; I ++) 6 {7 System. out. println (I); 8} 9} 10}View Code

The three expressions of the for loop can be omitted.

① Expression 1 omitted

1 public class dowhileforDemo {2 public static void main (String [] args) 3 {4 // print 1-10 5 int I = 1; 6 for (; I <= 10; I ++) 7 {8 System. out. println (I); 9} 10} 11}View Code

② If expression 2 is omitted, the condition will always be true, forming an endless loop.

③ Expression 3 is omitted, which may also result in permanent conditions and an endless loop.

But it can be written in a loop operation.

1 public class dowhileforDemo {2 public static void main (String [] args) 3 {4 // print 1-10 5 int I = 1; 6 (; I <= 10;) 7 {8 System. out. println (I); 9 I ++; 10} 11} 12}View Code

④ All three expressions are omitted, and the conditions are always true, forming an endless loop.

The semicolon cannot be omitted.

 

4. Comparison between a while loop and a for Loop

The while loop is an uncertain loop (the number of cycles is uncertain), and The for loop is a definite loop (the number of cycles is determined)

The two can be converted to each other.

When the number of cycles is determined, use for Loop

When the number of loops is unknown, use the while LOOP

 

V. Dual Loop

For (expression 1; expression 2; expression 3) {// outer loop

For (expression 1; expression 2; expression 3) {// Inner Loop

}

}

Key points: ① A loop containing a loop statement is called a dual loop.

② When the External Loop is executed once, the internal loop must be completed, and then return to the External Loop to enter the next loop until the execution of the External Loop is completed.

Example: A triangle array with the number * is output in 9 rows. The first line outputs 1 X, and the second line outputs 2 X *... 9th rows output 9 x *

1 public class dowhileforDemo {2 public static void main (String [] args) 3 {4 // a total of 9 lines of triangle array with output * numbers are output, and 1 X is output in the first line *, output 2 X *... 9th rows output 9x5 for (int I = 1; I <= 9; I ++) 6 {7 // The number of rows printed by the External Loop Control 8 for (int j = 1; j <= I; j ++) 9 {10 // Number of X printed by Inner Loop Control 11 System. out. print ("*"); 12} 13 System. out. println (); 14} 15} 16}View Code

 

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.