Home>Article>Web Front-end> What is the execution flow of switch statement?

What is the execution flow of switch statement?

hzc
hzc Original
2020-06-16 15:27:36 8610browse

What is the execution flow of switch statement?

1. Switch statement format:

switch(表达式){ case 值1: 语句体1; break; case 值2: 语句体2; break; ... default: 语句体n+1; break; }

2. Format explanation:

Expression:byte, short, int, char

# Case The value latter: It is the content used for matching the value of the expression

# Break: The meaning of the table interruption

default: When all values do not match, the statement body default is executed;

3. Execution process:

A: First calculate the value of the expression

##3. Execution process:

B: Compare the calculated value according to the value behind the case. Once there is a match, the corresponding statement is executed, and it is over when it encounters Break.

C: If all cases do not match, execute statement body n 1

4. Example code:

Scanner sc= new Scanner(System.in); System.out.println("请输入数字1-7"); int i = sc.nextInt(); switch(i) { case 1: System.out.println("星期一"); break;    case 2:      System.out.println("星期二");      break;    case 3:      System.out.println("星期三");      break;    case 4:     System.out.println("星期四");      break;    case 5:      System.out.println("星期五");      break;    case 6:      System.out.println("星期六");      break;    case 7:      System.out.println("星期天1");      break;    default:      System.out.println("请输入正确数字:");      break;

Recommended tutorial: "JS Tutorial

The above is the detailed content of What is the execution flow of switch statement?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn