V. switch case loop structure for loop while do while loop

catalogue

1.1 switch-case 

1.2 circulation structure

1.2.1   for loop

1.2.2 while loop  

1.2.3 do while cycle

1.3 small exercises  

1.3.1 printing Diamond:

1.3.2 99 multiplication table

1.3.3 prime number within 100 (written test questions)  

1.1 switch-case 

switch(expression){
case Constant 1:
    Execute statement 1;
    //break;

case Constant 2:
    Execute statement 2;
    //break;
...
default:
    Execute statement n;
    //break;
}
  • According to the values in the switch expression, the constants in each case are sequentially matched. Once the match is successful, the corresponding case structure is entered, the execution statement is invoked, and the following statements are continued, unless the break structure or the end of execution are pushed out of the switch structure.
  • The expression in switch can only be one of the following six data types: byte, short, chat, int, enumeration type and String type
  • Only constants can be declared after case, and ranges cannot be declared
  • default is an optional structure with flexible location
  • Any structure that can use switch case can use if else, and vice versa
  • If you can use both if else and switch case (there are few branches of switch expression), switch case is preferred and the efficiency is high

Example 1: for students with scores greater than 60, the output is "qualified", and for students with scores lower than 60, the output is "unqualified".

		Scanner scan = new Scanner(System.in);
		System.out.println("Please input the score: ");
		int score = scan.nextInt();
		switch(score/10) {
		case 6:
			System.out.println("qualified");
			break;
		case 7:
			System.out.println("qualified");
			break;
		case 8:
			System.out.println("qualified");
			break;
		case 9:
			System.out.println("qualified");
			break;
		case 10:
			System.out.println("qualified");
			break;
		default:
			System.out.println("unqualified");
		}
  • If the results of multiple case s in the structure are the same, you can consider merging, and the code of the above question is changed as follows:
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Please input the score: ");
    		int score = scan.nextInt();
    		switch(score/10) {
    		case 6:
    		case 7:
    		case 8:
    		case 9:
    		case 10:
    			System.out.println("qualified");
    			break;
    		default:
    			System.out.println("unqualified");
    		}
  • Better solution  
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Please input the score: ");
    		int score = scan.nextInt();
    		switch(score/60) {
    		case 0:
                System.out.println("unqualified");
                break;
    		case 1:
    			System.out.println("qualified");
    			break;
    		}

Example 2: input "month" and "day" of 2021 from the keyboard, and output the date as the day of 2021.  

		Scanner scan = new Scanner(System.in);
		System.out.println("Please input the month and day: ");
		int month = scan.nextInt();
		int day = scan.nextInt();
		int sum = 0;
		switch (month) {
		case 12:
			sum += 30;
		case 11:
			sum += 31;
		case 10:
			sum += 30;
        //......
		case 2:
			sum += 31;
		case 1:
			sum += day;
		}
		System.out.println(sum);

1.2 circulation structure

  • Execute specific code repeatedly when certain conditions are met
  • classification
    • for loop
    • while Loop
    • do-while Loop  
  • Four elements
    • Initialization condition
    • Loop condition (boolean type)
    • Circulatory body
    • Iterative condition
  • Loop nesting generally does not exceed three layers, otherwise the code is cumbersome

1.2.1   for loop

  • structure
    for(initial condition ;Cycle condition;Iterative condition){
        Circulatory body
    }

1.2.2 while loop  

  • structure
    Initialization condition;
    while(Cycle condition){
        Circulatory body;
        Iterative condition;
    }
  •   Pay attention to iterative conditions and avoid dead loops

1.2.3 do while cycle

  • structure
    Initialization condition;
    do{
        Circulatory body;
        Iterative condition;
    }while(Cycle condition)
  •   Compared with the while loop, the do while loop executes the loop body at least once whether it is full or not

1.3 small exercises  

1.3.1 printing Diamond:

		for(int i = 1; i <= 4; i ++) {
			for(int j = 0; j < 4 - i; j++) {
				System.out.print(' ');
			}
			for(int j = 0; j < 2*i-1; j++) {
				System.out.print('*');
			}
			System.out.println();
		}
		for(int i = 0; i < 3; i ++) {
			for(int j = 0; j <= i; j++) {
				System.out.print(' ');
			}
			for(int j = 0; j < 2*(3-i)-1; j++) {
				System.out.print('*');
			}
			System.out.println();
		}

1.3.2 99 multiplication table

		for(int i = 1; i <= 9; i++) {
			for(int j = 1; j <= i; j++) {
				System.out.print("" + i + "*" + j + '=' + i * j + "  ") ;
			}
			System.out.println();
		}

 

1.3.3 prime number within 100 (written test questions)  

Prime numbers are also called prime numbers. A natural number greater than 1, except 1 and itself, which cannot be divided by other natural numbers, is called a prime number; Otherwise, it is called a composite number (rule 1 is neither a prime number nor a composite number).

		boolean isFlag = true;
		for(int i = 2; i < 100; i++) {
			for(int j = 2; j <= Math.sqrt(i); j++) {
				if(i % j == 0) {
					isFlag = false;
					break;
				}
			}
			if(isFlag) {
				System.out.print("" + i + "  ");
			}
			isFlag = true;
		}
		System.out.println();

Keywords: Java

Added by xynah on Wed, 13 Oct 2021 17:53:47 +0300