While loop
-
- Format:
while(条件表达式) { // 条件表达式其实就是一个结果为boolean类型的代码
循环体;
}
- Execution process: First judge the value of the conditional expression, if true executes the loop body, after executing the loop body will again judge the value of the conditional expression until the value of the condition expression is false, while the loop ends
Do-while Cycle
do{
循环体;
}while(条件表达式);
- Execution process: First executes the body of the loop once, then judge the value of the conditional expression, if true, then executes the loop body until the value of the conditional expression is false, and the Do-while loop ends
For loop
Format:
- for (starting condition ①; judging condition ②; loop change mode ③) {
循环体④;
}
- Execution process: First executes the start condition, then executes the judgment condition, if is true to walk the loop body, after the loop body execution completes, walks the circulation change way, then executes the judgment condition, if is true again the loop body, until the judgment condition result is false, then the loop ends.
Note: If the result of the first execution judgment condition is false then the loop ends directly and the loop body is not executed
- ①->②->④->③->②->④->③->②->④->③ (until ② is false loop end)
Note: If you know the number of loops, use a For loop if you do not explicitly loop the number of cycles using the while loop
- Print all integers between 1 ~ n
Prints the and of all integers between 1 ~ n
Print between 1 ~ n all odd and
Public classPrint {/*** This method is used to print all integers between 1 ~ N * *@paramN*/ Public voidPrint1_n (intN) { for(inti = 1; I <= N; i++) {System.out.println (i); } } /*** Print all integers between 1 ~ N and *@paramN*/ Public voidPrintsum1_n (intN) {//define one and sum ideas intsum = 0; for(inti = 1; I <= N; i++) {sum+ = i;//sum = sum + i;} System.out.println (sum); } /*** Print 1 ~ n for all odd and *@paramN*/ Public voidPrintjisum (intN) {intsum = 0; //get each number between 1 and N for(inti = 1; I <= N; i++) { //Judging, if it is odd, add, even no matter ~ if(i% 2 = = 1) {sum+=i; }} System.out.println (sum); }}
Enhanced for Loop
for(要遍历的容器中元素的数据类型 变量名 : 要遍历的容器) {
使用变量;
}
- Role: Traversing a container (array or single-column collection)
- Shortcut keys: Enter fore below the container you want to traverse press ALT +/ENTER
Dead loop
for(;;) {
}
// while循环的死循环
while(true) {
}
Break and Continue
- Break: Jump (end) loop
- Continue: End this cycle and proceed to the next cycle
For loop nested arrays, arraylist,hashmap and string traversal traversal array
Public classtest1_ traversal Array { Public Static voidMain (string[] args) {int[] arr = {1, 2, 3, 4, 5 }; //normal for Loop for(inti = 0; i < arr.length; i++) {System.out.println (arr[i]); } //Enhanced for Loop for(intI:arr) {System.out.println (i); } }}
Traversing the ArrayList collection
Public classtest2_ traversing a single-column collection { Public Static voidMain (string[] args) {ArrayList<String> list =NewArraylist<>(); List.add ("Zhang San"); List.add ("John Doe"); List.add ("Harry"); List.add ("Zhao Liu"); //normal for Loop for(inti = 0; I < list.size (); i++) {System.out.println (List.get (i)); } //Enhanced for Loop for(String string:list) {System.out.println (string); } }}
Traverse the Map Collection
Public classtest3_ traversal double-column collection { Public Static voidMain (string[] args) {HashMap<string, integer> map =NewHashmap<>(); Map.put ("Zhang San", 23); Map.put ("John Doe", 24); Map.put ("Zhao Liu", 26); Map.put ("Qi Zhou", 27); //KeySet (): Gets the collection of all the keys in the double-column collection for(String key:map.keySet ()) {System.out.println (key+ "=" +Map.get (key)); } }}
Traversing strings
Public classtest4_ Traversal string { Public Static voidMain (string[] args) {String s= "ABCDEFG"; //charAt (Index): Gets the character that specifies the position of the corner label for(inti = 0; I < s.length (); i++) {System.out.println (S.charat (i)+ ""); } //ToCharArray (): Converts a string into a character array Char[] CHS =S.tochararray (); for(CharC:chs) {System.out.println (c+ ""); } }}
The Break keyword
End Current Loop
Continue keywords
Skip this loop and perform the Next loop
Continue in the cycle of its promoting effect
Java Basics Loop