Indefinite Loop - While and Loop
Explore how to implement indefinite loops in Rust using while and loop keywords. Learn to control iterations with conditions, manage mutable variables inside loops, and understand infinite loops for ongoing processes.
We'll cover the following...
We'll cover the following...
While loop
While loop also iterates until a specific condition is reached. However, in the case of a while loop, the number of iterations is not known in advance.
Syntax
The while keyword is followed by a condition that must be true for the loop to continue iterating and then begins the body of the loop.
The general syntax is :
Example
The following example makes use of a while loop to print a variable value. The loop terminates when the variable’s value modulus 2 equals 1.
Explanation
- A mutable variable,
var, is defined on line 2. - A mutable