Skip to content

Commit

Permalink
Showcase else-if
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeMathWalker committed Dec 18, 2024
1 parent 7fb0910 commit 4fc2710
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions book/src/02_basic_calculator/03_if_else.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,38 @@ if number < 5 {
}
```

### `else if` clauses

Your code drifts more and more to the right when you have multiple `if` expressions, one nested inside the other.\

```rust
let number = 3;

if number < 5 {
println!("`number` is smaller than 5");
} else {
if number >= 3 {
println!("`number` is greater than or equal to 3, but smaller than 5");
} else {
println!("`number` is smaller than 3");
}
}
```

You can use the `else if` keyword to combine multiple `if` expressions into a single one:

```rust
let number = 3;

if number < 5 {
println!("`number` is smaller than 5");
} else if number >= 3 {
println!("`number` is greater than or equal to 3, but smaller than 5");
} else {
println!("`number` is smaller than 3");
}
```

## Booleans

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

0 comments on commit 4fc2710

Please sign in to comment.