# 章序号/节序号/节/笔记序号
codecademy, Condition
# 概念阐释
- `else if` 虽然可以满足多条件可能的语法,但代码不够简洁
- 所以用 `switch` 关键字代替
## 语法

# 举例子
```js
let groceryItem = 'papaya';
switch (groceryItem)//()里的是变量名,否用比较运算符?
{
case 'tomato': //case ‘’:是多选条件,如果变量值是tomato,则会print这里的内容
console.log('Tomatoes are $0.49');
break;//告诉计算机退出块,不在执行任何代码,否则会一直执行
case 'lime':
console.log('limes are $1.49');
break;
case 'papaya':
console.log('papayas are $1,29');
break;
default:// 如果以上条件都不符合(false)则执行default代码内容
console.log('Invalid item');
break;
}
```
# 类比、比较与对比
- [[js condition Else If 语句]]
# 问题
- [练习题](https://www.codecademy.com/courses/introduction-to-javascript/lessons/control-flow/exercises/switch)
```js
let athleteFinalPosition = 'first place';
```
- *注意标点符号!*
- 不在每一个case后面用console,只在condition写好后用一个console,先magic eight ball那样。
**1.**
Let’s write a `switch` statement to decide what medal to award an athlete.
`athleteFinalPosition` is already defined at the top of **main.js**. So start by writing a `switch` statement with `athleteFinalPosition` as its expression.
**2.**
Inside the `switch` statement, add three `case`s:
- The first `case` checks for the value `'first place'`
- If the expression’s value matches the value of the `case` then `console.log()` the string `'You get the gold medal!'`
- The second `case` checks for the value `'second place'`
- If the expression’s value matches the value of the `case` then `console.log()` the string `'You get the silver medal!'`
- The third `case` checks for the value `'third place'`
- If the expression’s value matches the value of the `case` then `console.log()` the string `'You get the bronze medal!'`
Remember to add a `break` after each `console.log()`.
**3.**
Now, add a `default` statement at the end of the `switch` that uses `console.log()` to print `'No medal awarded.'`.
If `athleteFinalPosition` does not equal any value of our `case`s, then the string `'No medal awarded.'` is logged to the console.
Remember to add the `break` keyword at the end of the `default` case.
# 问题答案
- case 后面要有冒号,`if else`的简写`? :`
# 备注(经验集/错误集)