Files
freeCodeCamp/guide/chinese/go/if-else-statements/index.md
2018-10-16 21:32:40 +05:30

45 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: if else Statements
localeTitle: 如果是其他声明
---
## 介绍
`if`指定的条件为**true** `if`语句执行语句。如果条件为**false** ,则可以使用`else`语句执行另一个语句。
**注意:** `else`语句是可选的。
```Go
x := 7
if x%2 == 0 {
// This statement is executed if x is even
} else {
// This statement is executed if x is odd
}
```
可以嵌套多个`if...else`语句来创建`else if`子句。
```go
x := 7
if x == 2 {
// this statement is executed if x is 2
} else if x == 4 {
// this statement is executed if x is 4
} else if x == 7 {
// this statement is executed if x is 7
} else {
// this statement is executed if none of the aboves is true
}
```
在Go中您可以在`if`条件之前加上语句。然后,包含的变量定义对完整的`if`块有效。
```go
if x := 3; x == 2 {
// this statement is executed if x is 2
} else if x == 3 {
// this statement is executed if x is 3
} else {
// this statement is executed if none of the aboves is true
}
```