An if expression allows you to branch your code depending on conditions. You provide a condition and then state, “If this condition is met, run this block of code. If the condition is not met, do not run this block of code.”
Create a new project called branches in your projects directory to explore the if expression. In the src/main.rs file, input the following:
```rust
fn main() {
let number = 3;
if number < 5 {
println!("condition was true");
} else {
println!("condition was false");
}
}
```
You can use multiple conditions by combining if and else in an else if expression. For example:
```rust
fn main() {
let number = 6;
if number % 4 == 0 {
println!("number is divisible by 4");
} else if number % 3 == 0 {
println!("number is divisible by 3");
} else if number % 2 == 0 {
println!("number is divisible by 2");
} else {
println!("number is not divisible by 4, 3, or 2");
}
}
```
Because if is an expression, we can use it on the right side of a let statement to assign the outcome to a variable, as in Listing 3-2.
```rust
fn main() {
let condition = true;
let number = if condition { 5 } else { 6 };
println!("The value of number is: {number}");
}
```
**:: Reference ::** [Control Flow - The Rust Programming Language](https://doc.rust-lang.org/book/ch03-05-control-flow.html)