Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to break the loop when the counter reaches 3.
Rust
let mut count = 0; loop { count += 1; if count == [1] { break; } } println!("Count stopped at {}", count);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using a number other than 3 causes the loop to break too early or too late.
โ Incorrect
The loop breaks when count equals 3, stopping the loop at the right time.
2fill in blank
mediumComplete the code to break the loop when the input number is zero.
Rust
let mut input = 5; loop { if input == [1] { break; } input -= 1; } println!("Loop ended because input is zero.");
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using 1 or -1 instead of 0 causes the loop to break at the wrong time.
โ Incorrect
The loop breaks when input reaches zero, stopping the countdown.
3fill in blank
hardFix the error in the loop to break when the variable reaches 4.
Rust
let mut num = 0; loop { num += 1; if num == [1] { break; } } println!("Stopped at {}", num);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using 3 or 5 causes the loop to break too early or too late.
โ Incorrect
The loop should break when num equals 4, so the correct value is 4.
4fill in blank
hardFill both blanks to create a loop that breaks when the counter is less than 2.
Rust
let mut counter = 5; loop { counter -= 1; if counter [1] [2] { break; } } println!("Counter stopped at {}", counter);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using '==' instead of '<' changes the stopping condition.
Using 3 instead of 2 breaks the loop at the wrong time.
โ Incorrect
The loop breaks when counter < 2, so the operator is < and the number is 2.
5fill in blank
hardFill all three blanks to break the loop when the value is greater than 7 and print the value.
Rust
let mut value = 0; loop { value += 2; if value [1] [2] { break; } } println!("Value is [3]", value);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using '<' instead of '>' changes the condition.
Forgetting '{}' in the print statement causes no value to show.
โ Incorrect
The loop breaks when value > 7. The print uses {} to show the value.