In programming, control flow refers to the order in which instructions are executed. It allows developers to make decisions based on conditions and repeat actions multiple times. Rust provides several constructs for controlling the flow of a program, including conditional statements and loops.
Conditional statements allow your program to execute different blocks of code based on certain conditions. The most common conditional statement in Rust is the if expression.
The basic structure of an if statement is as follows:
let number = 3;
if number < 5 {
println!("condition was true");
} else {
println!("condition was false");
}
In this example, the program checks if `number` is less than 5. If the condition is true, it prints "condition was true". Otherwise, it prints "condition was false".
#### Multiple Conditions
You can use multiple conditions by combining them with logical operators like `&&` (and) and `||` (or):
```rust
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");
}
In this example, the program checks multiple conditions and executes the block of code associated with the first true condition.
### Loops
Loops allow you to repeat a block of code until a certain condition is met. Rust provides three types of loops: `loop`, `while`, and `for`.
#### Loop
The `loop` keyword creates an infinite loop that continues indefinitely until it is explicitly broken using the `break` statement:
```rust
let mut counter = 0;
loop {
println!("Counter: {}", counter);
counter += 1;
if counter == 5 {
break;
}
}
In this example, the program prints the value of `counter` and increments it by 1 in each iteration. The loop breaks when `counter` reaches 5.
#### While Loop
The `while` loop continues to execute as long as a condition is true:
```rust
let mut number = 3;
while number != 0 {
println!("{}!", number);
number -= 1;
}
println!("LIFTOFF!!!");
In this example, the program prints the value of number and decrements it by 1 in each iteration. The loop stops when number becomes 0.
The for loop is used to iterate over a collection, such as an array or range:
let a = [10, 20, 30, 40, 50];
for element in a.iter() {
println!("the value is: {}", element);
}
In this example, the program iterates over each element in the array a and prints its value.
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");
}
}
fn main() {
let mut counter = 0;
loop {
println!("Counter: {}", counter);
counter += 1;
if counter == 5 {
break;
}
}
let mut number = 3;
while number != 0 {
println!("{}!", number);
number -= 1;
}
println!("LIFTOFF!!!");
let a = [10, 20, 30, 40, 50];
for element in a.iter() {
println!("the value is: {}", element);
}
}
In the next section, we will explore more advanced control structures, including if-else statements and loops with labels. Understanding these concepts will help you write more complex and efficient Rust programs.