Skip to content

Commit 60947aa

Browse files
Showcase else-if (#234)
1 parent fe40e6e commit 60947aa

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

book/src/02_basic_calculator/03_if_else.md

+32
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,38 @@ if number < 5 {
3636
}
3737
```
3838

39+
### `else if` clauses
40+
41+
Your code drifts more and more to the right when you have multiple `if` expressions, one nested inside the other.
42+
43+
```rust
44+
let number = 3;
45+
46+
if number < 5 {
47+
println!("`number` is smaller than 5");
48+
} else {
49+
if number >= 3 {
50+
println!("`number` is greater than or equal to 3, but smaller than 5");
51+
} else {
52+
println!("`number` is smaller than 3");
53+
}
54+
}
55+
```
56+
57+
You can use the `else if` keyword to combine multiple `if` expressions into a single one:
58+
59+
```rust
60+
let number = 3;
61+
62+
if number < 5 {
63+
println!("`number` is smaller than 5");
64+
} else if number >= 3 {
65+
println!("`number` is greater than or equal to 3, but smaller than 5");
66+
} else {
67+
println!("`number` is smaller than 3");
68+
}
69+
```
70+
3971
## Booleans
4072

4173
The condition in an `if` expression must be of type `bool`, a **boolean**.\

0 commit comments

Comments
 (0)