Java loop and conditional statements

Java loop structure

while loop

public class Test {
   public static void main(String args[]) {
      int x = 10;
      while( x < 20 ) {
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }
   }
}

do ..... while loop

Regardless of whether the conditions for the establishment of a while statement will execute the contents of a do statement

public class Test {
   public static void main(String args[]){
      int x = 10;
 
      do{
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }while( x < 20 );
   }
}

for loop

public class Test {
   public static void main(String args[]){
      int x = 10;
 
      do{
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }while( x < 20 );
   }
}

Enhanced for loop

Disclaimer statement: declare a new local variable, the variable's type must match the type of array elements . Loop whose scope is defined in the block, and its value is equal to the array element at that time.

Expression: Expression is the name of the array to be accessed, or method that returns the value of the array.

public class Test {
   public static void main(String args[]){
      int [] numbers = {10, 20, 30, 40, 50};
 
      for(int x//声明语句 : numbers//表达式 ){
         System.out.print( x );
         System.out.print(",");
      }
      System.out.print("\n");
      String [] names ={"James", "Larry", "Tom", "Lacy"};
      for( String name : names ) {
         System.out.print( name );
         System.out.print(",");
      }
   }
}

break keyword

Out of the current cycle of the whole

continue keywords

Out of the current cycle, the next cycle into the

Java conditional statements

if ... else statement

if(布尔表达式)
{
   //如果布尔表达式为true将执行的语句
}

if(布尔表达式){
   //如果布尔表达式的值为true
}else{
   //如果布尔表达式的值为false
}

if...else...else if语句

    public static void main(String[] args) {
        int a=1,b=2,c=3;
        if(a>0) {
            System.out.print("a");
        }else if (b==2) {
            System.out.print("cc");
        }else {
            System.out.print("dd");
        }
    }

Nested if ... else

public class Test {

   public static void main(String args[]){
      int x = 30;
      int y = 10;

      if( x == 30 ){
         if( y == 10 ){
             System.out.print("X = 30 and Y = 10");
          }
       }
    }
}

Java switch case statement

Data type value in the case statement must be the same data type variable, and can be a constant or literal.

When it encounters a break statement, switch statement is terminated. The program jumps to the back of the switch statement executes the statement. case statement does not have to contain a break statement. If there is no break statement occurs, the program will continue with the next case statement until a break statement appears.

    public static void main(String[] args) {
        int grade=100;
        switch (grade) {
        case 100:
            System.out.print("不合格");
            break;
        case 50:
            System.out.print("合格");
        default:
            break;
        }
    }

Guess you like

Origin www.cnblogs.com/Mr-l/p/11870678.html