Select Statement – Switch

Select statement–switch

switch statement format:

< div> share picture

Execution process:

  1. First calculate the value of the expression
  2. Second, it is compared with case in turn. Once there is a corresponding value, the corresponding statement will be executed. During the execution, it will end when it encounters a break.
  3. Finally, if all the cases do not match the value of the expression, the body of the default statement will be executed, and the program will end.

Execution flow chart

Share picture

Code example

public class Demo07Switch {

public static void main(String[] args) {

//Define variables to determine the day of the week
int weekday = 6;
//switch statement to achieve selection
switch (weekday) {
case 1:
System.out.println(
"Monday");
break;
case 2:
System.out.println(
"Tuesday");
break;
case 3:
System.out.println(
"Wednesday");
break;
case 4:
System.out.println(
"Thursday");
break;
case 5:
System.out.println(
"Friday");
break;
case 6:
System.out.println(
"Saturday");
break;
case 7:
System.out.println(
"Sunday");
break;
default:
System.out.println(
"error");
break;
}
}
}

Execution result

Share picture

Precautions for using switch statement:

< p>1. The values ​​after multiple cases cannot be repeated.

2. Only the following data types can be included in the parentheses after switch:

  • Basic data types: byte/short/char/int
  • Quotation Data type: String character string, enum enumeration

3. The format of the switch statement can be very flexible: the order can be reversed, and the break statement can also be omitted. “Which case is matched will be executed downwards from which position, until the break is encountered or the whole is over.”

The penetration of the case

  • In the switch statement, if break is not written after the case, penetration will occur, that is, the value of the next case will not be judged. Run directly backwards until a break is encountered, or the overall switch ends.

switch statement format:

share picture

Execution process:

  1. First calculate the value of the expression
  2. Secondly, compare with case in turn. Once there is a corresponding value, the corresponding statement will be executed. During the execution, it will end when it encounters a break.
  3. Finally, if all the cases do not match the value of the expression, the body of the default statement will be executed, and the program will end.

Execution flow chart

Share picture

Code example

public class Demo07Switch {

public static void main(String[] args) {

//Define variables to determine the day of the week
int weekday = 6;
//switch statement to achieve selection
switch (weekday) {
case 1:
System.out.println(
"Monday");
break;
case 2:
System.out.println(
"Tuesday");
break;
case 3:
System.out.println(
"Wednesday");
break;
case 4:
System.out.println(
"Thursday");
break;
case 5:
System.out.println(
"Friday");
break;
case 6:
System.out.println(
"Saturday");
break;
case 7:
System.out.println(
"Sunday");
break;
default:
System.out.println(
"error");
break;
}
}
}

Execution result

Share picture

Precautions for using switch statement:

< p>1. The values ​​after multiple cases cannot be repeated.

2. Only the following data types can be included in the parentheses after switch:

  • Basic data types: byte/short/char/int
  • Quotation Data type: String character string, enum enumeration

3. The format of the switch statement can be very flexible: the order can be reversed, and the break statement can also be omitted. “Which case is matched will be executed downwards from which position, until the break is encountered or the whole is over.”

The penetration of the case

  • In the switch statement, if break is not written after the case, penetration will occur, that is, the value of the next case will not be judged. Run directly backwards until a break is encountered, or the overall switch ends.

public class Demo07Switch {

public static void main(String[] args) {

//Define variables to determine the day of the week
int weekday = 6;
//switch statement to achieve selection
switch (weekday) {
case 1:
System.out.println(
"Monday");
break;
case 2:
System.out.println(
"Tuesday");
break;
case 3:
System.out.println(
"Wednesday");
break;
case 4:
System.out.println(
"Thursday");
break;
case 5:
System.out.println(
"Friday");
break;
case 6:
System.out.println(
"Saturday");
break;
case 7:
System.out.println(
"Sunday");
break;
default:
System.out.println(
"error");
break;
}
}
}

Leave a Comment

Your email address will not be published.