Hello! In this lesson, we'll explore the powerful concept of loop control flow in Rust. Control flow in loops allows you to manage the execution of code more effectively within your while
and for
loops. . Specifically, we'll delve into using conditionals inside loops, the loop
construct, controlling loops with break
and continue
statements, and understanding their significance.
Control flow mechanisms are essential for building complex and functional logic in your programs. By the end of this lesson, you'll be proficient in using these tools to write more efficient and readable code.
Let's get started!
Let's first explore how to incorporate conditionals inside loops. This will help you perform specific actions based on dynamic conditions evaluated during each iteration.
Rust1fn main() { 2 let mut num = 0; 3 while num <= 10 { 4 if num % 2 == 0 { 5 println!("{} is even", num); 6 } else { 7 println!("{} is odd", num); 8 } 9 num += 1; 10 } 11} 12/* Output: 13 0 is even 14 1 is odd 15 2 is even 16 3 is odd 17 4 is even 18 5 is odd 19 6 is even 20 7 is odd 21 8 is even 22 9 is odd 23 10 is even 24*/
In this example:
- We initialize
num
to 0. - The
while
loop runs as long asnum
is less than or equal to 10. - Inside the loop, we use an
if
statement to check ifnum
is even or odd, then print the approprita message. - We increment
num
by 1.
In Rust, an infinite loop can be created using the loop
keyword. To stop the loop, use the break
keyword to stop execution of the loop. This is useful when you want to stop a loop once a particular requirement is met. Let's take a look.
Rust1fn main() { 2 let mut count = 0; 3 loop { 4 if count == 5 { 5 break; 6 println!("This does not get printed"); 7 } 8 println!("Count is: {}", count); 9 count += 1; 10 } 11} 12/* Output: 13 Count is 0 14 Count is 1 15 Count is 2 16 Count is 3 17 Count is 4 18*/
In this example:
- We initialize
count
to 0. - The
loop
runs indefinitely until thebreak
condition is met. - When
count
equals 5, thebreak
statement exits the loop. - The print statement inside the
if
block does not get executed because the loop stop execution
The continue
statement is used to skip the rest of the loop iteration and proceed to the next iteration. This is beneficial when you need to bypass certain actions based on a condition. Let's look at an example where we want to only print odd numbers.
Rust1fn main() { 2 for number in 1..10 { 3 if number % 2 == 0 { 4 continue; 5 println!("This does not get printed"); 6 } 7 println!("Odd number: {}", number); 8 } 9} 10/* Output: 11 Odd number: 1 12 Odd number: 3 13 Odd number: 5 14 Odd number: 7 15 Odd number: 9 16*/
In this example:
- The
for
loop iterates over the range1..10
. - If the number is even, the
continue
statement skips the rest of the current iteration. - The print statement inside the
if
block does not get executed because the loop skips to the next iteration - If the number is odd, it gets printed.
Combining break
and continue
with conditionals allows for complex control flow within your loops. Here’s an example of using both together:
Rust1fn main() { 2 let mut num = 0; 3 loop { 4 if num > 10 { 5 break; 6 println!("This does not get printed"); 7 } 8 if num % 2 != 0 { 9 num += 1; 10 continue; 11 println!("This does not get printed"); 12 } 13 println!("{} is even", num); 14 num += 1; 15 } 16} 17/* Output: 18 0 is even 19 2 is even 20 4 is even 21 6 is even 22 8 is even 23 10 is even 24*/
In this example:
- The
num
variable is initialized to 0. - The
loop
runs indefinitely until thebreak
condition is met. - Inside the loop:
- First, it checks if
num
is greater than 10 with the conditionif num > 10
. If this condition is true, thebreak
statement terminates the loop. - If not, it checks if
num
is odd with the conditionif num % 2 != 0
. If this condition is true:num
is incremented by 1.- The
continue
statement skips the rest of the loop iteration, causing the loop to proceed to the next iteration. - The
println!
statement after thecontinue
statement does not get executed, as thecontinue
statement transfers control to the beginning of the loop immediately.
- If
num
is even, it reaches theprintln!
statement, which prints the even number message, and then incrementsnum
by 1.
- First, it checks if
Fantastic work! Today, you've delved into the essentials of loop control flow in Rust. You've learned how to use conditionals inside loops, control loop execution with break
and continue
, and how to combine these tools for complex loop behaviors.
Loop control flow is a powerful technique that allows you to write more efficient and controlled loops. Equipped with this knowledge, it's time to apply what you've learned in practice. Dive into the upcoming exercises to solidify your understanding and explore the true potential of loop control in Rust. Happy coding!