Chapter 4 - control the flow of execution

Think in java study notes

pikzas

2019.03.06

Chapter 4 - control the flow of execution

Knowledge Point

while do-while和for

while loop will go inquiry to determine the conditions, if false, directly out of the loop once is not executed.

do-while loop will first pass through the loop body code, then query to determine the conditions, if false, will exit the loop.

for(initialization;boolean-expression;step){statement}

  • initialization only once
  • Then the result of determination based on boolean-expression is true into the circulation, false out of the loop
  • Execution statement
  • Execution step
  • Repeat Step

for loop comma operator can initialization time while initializing multiple of the same type of variable

class Demo{
   public static void test1() {
       System.out.println("--test1--");
       for (int i = 1 , j = i +5 ; (i < 5 && j < 8) ; i++ , j =i *2) {
           System.out.println("i = "+i+", j = "+j);
       } 
   }
}
//output
//i = 1, j = 6
//i = 2, j = 4
//i = 3, j = 6

foreach can only be used in the array of objects or implements Iterable interface

Exit all break after the current cycle comprises; Continue simply skip the current cycle, after which the cycle or how loud noise cycle

while (true) for (; true;) are equivalent.

Loop nested tags (lable :) for multiple layers, similar to the keyword goto

  1. General continue is out of the loop when operations where
  2. continue with labels is immediately behind the label out of this loop when the operations
  3. General circulation where the break is after the end of all operations, and operations after the break when the code is not enforced
  4. break with a label that is all subsequent operations cycle immediately after the end tag
class Demo{
    public static void main(String[] args){
        int i = 0;
        outer:
        for(;;){
            inner:
            for(;i<10;i++){
                System.out.println("i = "+i);
                if(i==2){
                    System.out.println("continue");
                    continue ;
                }
                if(i==3){
                    System.out.println("break");
                    i++;
                    break;
                }
                if(i==7){
                    System.out.println("continue outer");
                    i++;
                    continue outer;
                }
                if(i==8){
                    System.out.println("break outer"); 
                    break outer;;
                }
                
                for (int k = 0;k < 5;k++ ) {
                    if(k==3){
                        System.out.println("continue inner");
                        continue inner;
                    }
                }
            }
        }
      
    }
}

//output
//i = 0
//continue inner
//i = 1
//continue inner
//i = 2
//continue
//i = 3
//break
//i = 4
//continue inner
//i = 5
//continue inner
//i = 6
//continue inner
//i = 7
//continue outer
//i = 8
//break outer

switch(indicator) case:statement;break;

indicator only byte char short int string and enumeration, can not be a boolean long float double;
and see also the following example can successfully run
without break Note case penetrate.

class Demo{
    public static void main(String[] args){
        byte i = 0b10;
            switch (i){
                case 2:
                    System.out.println(1111);
                    break;
                case 0b11:
                    System.out.println(2222);
                    break;
                default: 
                    System.out.println("xxxxx");
        }
    }
}
class Demo{
    public static void main(String[] args){
        short i = 126;
        switch (i){
            case (char)23:
                System.out.println(1111);
                break;
            case 0b1111110:
                System.out.println(2222);
                break;
            default:
                System.out.println("xxxxx");
        }
    }
}

Guess you like

Origin www.cnblogs.com/Pikzas/p/12158974.html