I. Nested application of statements
Statement nesting form. In fact, there are still statements in the statement. There are various forms without fixed formats and routines.
1. Print even numbers
The code is as follows: |
Copy code |
For (int x = 1; x <= 10; x ++) {If (x % 2 = 1) Continue; System. out. prinln ("x =" + x ); } |
II. Accumulate sum and counter
1. Obtain 1 ~ And print.
Idea: // 1. Define variables to store ever-changing sums.
Int sum = 0;
// 2. Define the variable and record the variable number.
Int x = 1;
// 3. Define the loop and repeat the addition process.
Use while to reflect
The code is as follows: |
Copy code |
While (x <= 10) { Sum = sum + x; X ++; } System. out. println ("sum =" + sum ); |
Loop note:
Be sure to specify which statements need to participate in the loop and which do not need
Let's look at another instance.
The code is as follows: |
Copy code |
Class ForTest2 { Public static void main (String [] args) { // Use. Int sum = 0; For (int x = 0; x <= 10; x ++) { Sum + = x; } System. out. println ("for sum =" + sum ); } } Package com. test. For_Each; Import java. util. ArrayList; Import java. util. Iterator; Import java. util. List; Public class ForTest { Public static void main (String args []) { Int arr [] = {1, 2, 3, 4, 5, 6, 7, 8 }; /** * New writing method */ For (int a: arr) System. out. println (); /** * Old-style writing */ For (int I = 0; I <arr. length; I ++) System. out. println (arr [I]); String arr2 [] = {"good", "stream", "oh ","!! "}; For (String a2: arr2) System. out. println (a2 );
Int arr3 [] [] = {, 3}, {, 6, },{, 9 }}; For (int a31 []: arr3) { For (int a32: a31) { System. out. println (a32 ); } System. out. println (); }
List <String> list = new ArrayList <String> (); List. add ("good "); List. add ("Stream "); List. add ("oh "); List. add ("!! "); /** * Traversal based on the collection class length */ For (int I = 0; I <list. size (); I ++) { System. out. println (list. get (I )); }
/** * Traverse by iterator */ For (Iterator I = list. iterator (); I. hasNext ();) { System. out. println (I. next ()); }
/** * Based on the new for-each traversal */ For (String element: list) System. out. println (element ); } } |
Compared with iterator, the set content cannot be conveniently deleted (in fact, iterator is also called internally)
In addition to simple traversal and reading of the content, we do not recommend using enhanced.
The program process is like this. First define two long variables limit and factoritl, and attach the initial values, then execute the for loop to clearly write the program format, you can clearly understand:
The code is as follows: |
Copy code |
For (int I = 1; I <= limit; I ++ ){ Factoritl = 1; For (int factor = 2; factor <= I; factor ++ ){ Factoritl * = factor; System. out. println (I + "I = is" + factoritl ); } } |
Outer for, first define a variable I with the initial value 1, then check if I is less than or equal to limit, then enter the loop body, execute factoritl = 1; then execute the next for loop, first, attach the initial value 2 to the factor, and then check whether the factor is less than or equal to I. If it is true, enter the inner layer for, execute factoritl * = factor; and print it. After the for internal program is executed, execute factor ++, and then check whether factor <= I is true. If it is true, continue executing the for internal program. If it is not true, exit the for loop body and continue executing the next line of code, in this example, after the internal for statement is launched, the comparison operation of the external loop is continued, and then the internal for statement is executed until the program exits completely.