What is the execution flow of switch statement?

hzc
Release: 2020-06-16 15:27:36
Original
8592 people have browsed it

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; }
Copy after login

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;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!