Nested application of a statement
Statement nesting form. In fact, there are statements in the statement. Various forms, no fixed format and routines.
1, print even
The code is as follows |
|
for (int x=1;x<=10;x++) {if (x%2==1) Continue System.out.prinln ("x=" +x); } |
Add Sum, counter
1, get the 1~10 and print.
Train of thought://1, defines variables for storing ever-changing and.
int sum = 0;
2, define the variable, and record the constantly changing number of added.
int x = 1;
3, define the cycle, repeat the process of addition.
Use while to reflect
The code is as follows |
|
while (x<=10) { sum = sum + x; x + +; } System.out.println ("sum=" +sum); |
Circular Note:
Be sure to identify which statements need to be involved in the loop and which do not need
Look at one more example
code as follows |
|
Class ForTest2 { public static void Main (string[] Arg s) { ////For. 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 notation */ for (int a:arr) System.out.println (a); /** * Legacy notation */ 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[][] = {{1,2,3},{4,5,6,},{7,8,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 ("!!"); /** * traverses the */ for (int i=0;i<list.size (); i++) { System.out.println (List.get (i)) based on the collection class length; } /** * Iterates through the */ for (Iterator i = List.iterator (); I.hasnext ();) { System.out.println (I.N EXT ()); } /** * Based on the new For-each traversal */ for (String element:list) System.out.println (element); } } |
For collections, it is not easy to delete the contents of the collection (in fact, it is also called iterator) compared with iterator.
In addition to simply traversing and reading the contents, it is not recommended to use the enhanced for.
The program flow is like this, first define two long variable limit,factoritl, and attach the initial value, and then execute for loop, the format of the program to write clearly, you can clearly understand:
The code is as follows |
|
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); } } |
The outer for, first define a variable i and attach initial value 1, then check whether I is less than or equal to limit, set up into the loop body, execute factoritl=1; then execute the next for loop, first give factor an initial value of 2, and then check if the factor is less than or equal to I, if set Then enter the inner layer for, execute factoritl *= factor; and print, for internal program after execution, execute factor++, then check whether Factor<=i is set up, if set up, continue to execute for internal program, if not, then Exit For loop body, continue execution next line of code, in this example, After launching the internal for, continue to perform the external loop comparison operation, and then execute the internal for, so loop until the program completely quits