Loop with break
๐ Scenario: You are writing a simple Rust program that counts numbers starting from 1. You want to stop counting once you reach a certain number.
๐ฏ Goal: Build a Rust program that uses a loop to count numbers starting from 1 and stops the loop using break when the count reaches 5.
๐ What You'll Learn
Create a variable called
count and set it to 1Create a variable called
max_count and set it to 5Use a
loop to repeatedly print the current countInside the loop, use
if and break to stop the loop when count is greater than or equal to max_countIncrease
count by 1 each time the loop runsPrint the final message
"Counting finished!" after the loop ends๐ก Why This Matters
๐ Real World
Loops with break are used in programs to repeat tasks until a certain condition is met, like waiting for user input or processing data until done.
๐ผ Career
Understanding loops and break statements is essential for writing efficient and controlled code in many programming jobs.
Progress0 / 4 steps