Table of Contents📚
While loops🔁
What is a while loop❓
While loop are used to execute a block of code as long as a condition is true.
The while keyword🔁
The while
keyword is used to create a while loop.
Example:
let mut cats = 0;
while count < 5 {
println!("There are {} cats 🐈", count);
// Increment `count`
count += 1;
}
While cats
is less than 5, we will print the value of cats
on a new line, and then increment cats
by 1.
Output:
There are 0 cats 🐈
There are 1 cats 🐈
There are 2 cats 🐈
There are 3 cats 🐈
There are 4 cats 🐈
The break and continue keywords🔑
We can also use the break
and continue
keywords as seen in the Infinite Loop ♾️ Section.