Showcase else-if (#234)

This commit is contained in:
Luca Palmieri 2024-12-18 16:53:14 +01:00 committed by GitHub
parent fe40e6e2d0
commit 60947aaacd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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 ## Booleans
The condition in an `if` expression must be of type `bool`, a **boolean**.\ The condition in an `if` expression must be of type `bool`, a **boolean**.\