switch/case statement
switch/case statement
When making a large number of selection judgments, if you still use the if/else structure, the code may become very complicated. It’s messy, so we use the switch/case structure:
switch(k) { case k1: 执行代码块 1 ; break; case k2: 执行代码块 2 ; break; default: 默认执行(k 值没有在 case 中找到匹配时); }
Syntax description:
Switch must be assigned an initial value, and the value matches each case value. Satisfies all statements after executing the case, and uses the break statement to prevent the next case from running. If all case values do not match, the statement after default is executed.
Assuming that students' test scores are evaluated on a 10-point full-score system, we grade the scores according to each grade and make different evaluations based on the grade of the scores.
rrreeswitch