Rust Programming
We use the while loop to execute a code block till the condition is true. The syntax for the while expression is:
while condition {// code block}// code block outside while loop{codeBox}
Here, the while loop evaluates the condition before proceeding further.
If the condition evaluates to:
- true - The code block inside the while loop is executed and the condition is evaluated again
- false - The loop terminates and the code block outside the while loop is executed
Example: Rust while Loop
fn main() {let mut counter = 1;// usage of while loopwhile counter < 6 {println!("{}", counter);counter += 1;}}{codeBox}
Output
12345{codeBox}
In the example above, we have a condition:
while counter < 6 {// code block}{codeBox}
Here, the loop keeps running till the counter variable is less than 6. Inside the loop, we are increasing the value of the counter by 1.
After 5th iteration, the value of counter will be 6, so the condition, counter < 6 becomes false and the loop is terminated.
Infinite while Loop
You can write a loop that never ends using the while expression. Let's look at an example,
fn main() {let counter = 1;// while loop with a condition that always evaluates to truewhile counter < 6 {println!("Loop forever!");}}{codeBox}
Output
Loop forever!Loop forever!Loop forever!...{codeBox}
This example code will print "Loop forever!" indefinitely because the condition counter < 6 always evaluates to true. It is because we never increase the value of the counter variable inside the loop. Thus, this program will run until the user terminates the program.
Example: Multiplication Table Using while Loop
fn main() {// variable to print multiplication table forlet i = 2;// counter variable that starts at 1let mut j = 1;// while loop that runs for 10 iterationswhile j <= 10 {// multiply i and jlet mult = i * j;// print multiplication result on each iterationprintln!("{} * {} = {}", i, j, mult);// increase value of counter variable jj += 1;}}{codeBox}
Output
2 * 1 = 22 * 2 = 42 * 3 = 62 * 4 = 82 * 5 = 102 * 6 = 122 * 7 = 142 * 8 = 162 * 9 = 182 * 10 = 20{codeBox}
Nested while Loop
We can use a while loop inside the body of another while loop. This is known as a nested while loop. A nested while loop looks like:
while outer_condition {// outer code block 1while inner_condition {// inner code block}// outer code block 2}{codeBox}
Let's print a pattern using a nested while loop,
fn main() {// outer loop counterlet mut i = 1;// outer loopwhile i <= 5 {// inner loop counterlet mut j = 1;// inner loopwhile j <= 5 {print!("*");// increase inner loop counterj += 1;}println!("");// increase outer loop counteri += 1;}}{codeBox}
Output
*************************{codeBox}
In the above example,
- The outer while loop iterates 5 times
- The inner while loop inside of the outer while loop also iterates 5 times
- The inner while loop prints an asterisk(*) → print!(*) on every iteration
- The inner while loop stops when the counter variable j reaches to 6 as the inner condition evaluates to false
- The outer while loop prints a new line → println!("") on every iteration and goes to the next iteration which will initiate the inner while loop again
- The outer while loop stops when the counter variable i reaches to 6 as the outer condition evaluates to false