The concept of loops comes into play if we wish to run the block of statements more than once. A loop runs the code contained in its body until it reaches its conclusion, at which point it restarts right where it left off.
It is not a conditional loop, is it? This keyword instructs Rust to keep running the code block repeatedly unless you manually end the loop explicitly.
loop{
//block statements
}
Block statements are run an unlimited number of times in the syntax above.
fn main()
loop
{
println!("Hello javaTpoint");
}
Hello javaTpoint
Hello javaTpoint
Hello javaTpoint
Hello javaTpoint
.
.
.
infinite times
In this example, until we explicitly end the loop, “Hello javaTpoint” is printed again. The “ctrl+c” command is typically used to end a loop.
To break out of the loop, use the ‘Break’ keyword. The loop will run endlessly if the ‘break’ keyword is not used.
fn main()
let mut i=1;
loop
{
println!("Hello javaTpoint");
if i==7
{
break;
}
i+=1;
}}
Hello javaTpoint
Hello javaTpoint
Hello javaTpoint
Hello javaTpoint
Hello javaTpoint
Hello javaTpoint
Hello javaTpoint
The fact that i is a mutable variable in the example above indicates that the counter variable is subject to change for use in the future.
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.