Day-4: Rust Explore Control Flow with If-Else Statements and Loops.
Table of contents
Introduction:
Control flow structures are essential in programming languages as they allow us to make decisions and repeat actions based on certain conditions. In Rust, control flow is achieved through if-else statements and loops. In this article, we will dive into the world of control flow in Rust, exploring if-else statements and different types of loops available in the language.
If-Else Statements:
If-else statements allow you to execute different blocks of code based on specified conditions. The syntax of if-else statements in Rust is straightforward:
if condition {
// Code block executed if the condition is true
} else {
// Code block executed if the condition is false
}
Here's an example to illustrate the usage of if-else statements:
fn main() {
let number = 10;
if number < 0 {
println!("The number is negative.");
} else if number > 0 {
println!("The number is positive.");
} else {
println!("The number is zero.");
}
}
In the example above, we compare the value of number
with different conditions to determine whether it's positive, negative, or zero. The appropriate code block is executed based on the evaluation of the conditions.
Loops in Rust:
Loops enable repetitive execution of a block of code until a specific condition is met. Rust provides three types of loops: loop
, while
, and for
.
Loop:
The
loop
keyword creates an infinite loop that continues until explicitly terminated. To exit the loop, you can use thebreak
keyword.
fn main() {
let mut counter = 0;
loop {
println!("Current count: {}", counter);
counter += 1;
if counter >= 5 {
break;
}
}
}
In the example above, we use a loop
to print the current value of counter
until it reaches 5. Once the condition is met, the break
statement is executed, terminating the loop.
While:
The
while
loop executes a block of code as long as a given condition is true. The condition is checked before each iteration.
fn main() {
let mut counter = 0;
while counter < 5 {
println!("Current count: {}", counter);
counter += 1;
}
}
In the above example, the while
loop continues to execute as long as counter
is less than 5. After each iteration, the value of counter
is incremented.
For:
The
for
loop is used to iterate over a collection of elements, such as arrays or ranges. It automatically handles the iteration process, making it convenient for looping operations.
fn main() {
let numbers = [1, 2, 3, 4, 5];
for number in numbers.iter() {
println!("Current number: {}", number);
}
}
In the example above, we use a for
loop to iterate over each element in the numbers
array. The loop assigns each element to the variable number
, and the corresponding block of code is executed.
Conclusion:
Control flow structures, such as if-else statements and loops, empower developers to make decisions and repeat actions in their programs. In this article, we explored the usage of if-else statements to execute different code blocks based on conditions. We also covered the loop
, while
, and for
loops for repetitive execution of code. Understanding and utilizing these control flow mechanisms in Rust will enable you to create more dynamic and efficient programs. Happy coding in Rust!
I hope this helps, you!!
More such articles:
https://www.youtube.com/@maheshwarligade
\==========================**=========================
If this article adds any value to you then please clap and comment.
Let’s connect on Stackoverflow, LinkedIn, & Twitter.