A conditional loop is called a “while-loop.” The conditional loop is used in programs that require condition evaluation. The loop is carried out when the condition is met; if not, it is ended.
while condition
//block statements;
The while loop in the syntax above assesses the condition. Block statements are carried out if the condition is true; else, the loop is ended. ‘loop’, ‘if’, ‘else’, or ‘break’ statements can be combined with this built-in construct in Rust.
fn main()
{
let mut i=1;
while i<=10
{
print!("{}", i);
print!(" ");
i=i+1;
}
}
1 2 3 4 5 6 7 8 9 10
Since ‘i’ in the example above is a mutable variable, its value is changeable. The while loop runs until ‘i’s’ value is either equal to or less than 10.
fn main()
{
let array=[10,20,30,40,50,60];
let mut i=0;
while i<6
{
print!("{}",array[i]);
print!(" ");
i=i+1;
}
}
10 20 30 40 50 60
The while loop is used to iterate through the items of an array in the example above.
CodingAsk.com is designed for learning and practice. Examples may be made simpler to aid understanding. Tutorials, references, and examples are regularly checked for mistakes, but we cannot guarantee complete accuracy. By using CodingAsk.com, you agree to our terms of use, cookie, and privacy policy.
Copyright 2010-2024 by Refsnes Data. All Rights Reserved.