This article explains control flow in Rust using if, else if, and else statements with examples for dynamic programming.
Control flow is a fundamental concept in programming that allows you to determine which parts of your code execute based on specific conditions. This concept is key for writing dynamic and efficient programs. In Rust, control flow is managed using conditional statements such as if, else if, and else. Below, we explore these constructs with clear examples.
The if statement is the most basic control flow construct. It executes a block of code only when a given condition evaluates to true. The syntax is as follows:
Copy
Ask AI
if condition { // code to execute if condition is true}
Consider the following example where a number is checked to determine whether it is less than 10:
Copy
Ask AI
fn main() { let number = 7; if number < 10 { println!("The number is less than 10"); }}
Running this code produces the following output:The number is less than 10
The else statement offers an alternative code block that executes when the condition in the if statement is false. For example, in the following code, the number is set to 15:
Copy
Ask AI
fn main() { let number = 15; if number < 10 { println!("The number is less than 10"); } else { println!("The number is 10 or greater"); }}
Since 15 is not less than 10, the else block executes, outputting:The number is 10 or greater
To evaluate multiple conditions sequentially, you can use the else if statement. As soon as a condition evaluates to true, its corresponding block is executed, and the remaining conditions are skipped. The syntax is:
Copy
Ask AI
if condition1 { // code if condition1 is true} else if condition2 { // code if condition2 is true} else { // code if none of the conditions are true}
Consider this example where a number is tested against several conditions:
Copy
Ask AI
fn main() { let number = 20; if number < 10 { println!("The number is less than 10"); } else if number < 20 { println!("The number is between 10 and 19"); } else { println!("The number is 20 or greater"); }}
In this code, both the first condition (number < 10) and the second condition (number < 20) are false, so the else block executes, and the output is:The number is 20 or greater
You can build more complex conditional statements by combining multiple conditions with logical operators like AND (&&) and OR (||).
In Rust, combining conditions allows you to create highly specific checks. For example, use the && operator to ensure multiple conditions are true before executing a block.
Consider the example below where the number is initialized to 25:
Copy
Ask AI
fn main() { let number = 25; if number % 5 == 0 && number % 2 == 0 { println!("The number is divisible by both 5 and 2"); } else if number % 5 == 0 { println!("The number is divisible by 5 but not by 2"); }}
Here, the first if condition checks if the number is divisible by both 5 and 2. Since 25 is not divisible evenly by 2, the else if condition is evaluated, resulting in:The number is divisible by 5 but not by 2
Rust also permits using an if expression to assign a value to a variable. This feature streamlines conditional assignments. Consider the following example:
Copy
Ask AI
fn main() { let condition = true; let number = if condition { 5 } else { 10 }; println!("The value of number is: {}", number);}
Since the condition is true, the variable number is assigned a value of 5. If the condition were false, number would be assigned 10. The output of this execution is:The value of number is: 5