Java Basics Loop

Source: Internet
Author: User

While loop
      1. Format:
      2. while(条件表达式) { // 条件表达式其实就是一个结果为boolean类型的代码
      3. 循环体;
      4. }
      • 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
    • Format:
    1. do{
    2. 循环体;
    3. }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:

  1. for (starting condition ①; judging condition ②; loop change mode ③) {
  2. 循环体④;
  3. }
  4. 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

  5. Print all integers between 1 ~ n

    Prints the and of all integers between 1 ~ n

    Print between 1 ~ n all odd and
  6.  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
    • Meaning of traversal: getting every element in a container

    • Format:

    1. for(要遍历的容器中元素的数据类型 变量名 : 要遍历的容器) {
    2. 使用变量;
    3. }
    • 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
    1. for(;;) {
    2. }
    3. // while循环的死循环
    4. while(true) {
    5. }
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

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.