Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to label the outer loop correctly.
Rust
fn main() {
'outer: for i in 1..=3 {
for j in 1..=3 {
if i == 2 && j == 2 {
break [1];
}
println!("i = {}, j = {}", i, j);
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using a label that is not defined.
Omitting the apostrophe in the label name.
โ Incorrect
The label 'outer is used to break out of the outer loop from inside the inner loop.
2fill in blank
mediumComplete the code to continue the outer loop when a condition is met.
Rust
fn main() {
'outer: for i in 1..=3 {
for j in 1..=3 {
if j == 2 {
continue [1];
}
println!("i = {}, j = {}", i, j);
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using continue without a label inside nested loops.
Using a label that does not exist.
โ Incorrect
Using continue with the 'outer label skips to the next iteration of the outer loop.
3fill in blank
hardFix the error in the code by choosing the correct label to break the outer loop.
Rust
fn main() {
'start: for x in 1..=5 {
for y in 1..=5 {
if x * y > 6 {
break [1];
}
println!("x = {}, y = {}", x, y);
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using a label that is not defined.
Using break without a label inside nested loops.
โ Incorrect
The label 'start is defined on the outer loop and must be used to break it.
4fill in blank
hardFill both blanks to correctly label and break the outer loop.
Rust
fn main() {
[1] for num in 1..=4 {
for letter in ['a', 'b', 'c'].iter() {
if num == 3 {
break [2];
}
println!("{} - {}", num, letter);
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using the label with colon in the break statement.
Mismatching label names between loop and break.
โ Incorrect
The outer loop is labeled 'outer: and the break uses 'outer to exit it.
5fill in blank
hardFill all three blanks to label the loops and continue the outer loop correctly.
Rust
fn main() {
[1] for i in 1..=3 {
[2] for j in 1..=3 {
if i == 2 && j == 1 {
continue [3];
}
println!("i = {}, j = {}", i, j);
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using the inner loop label in continue instead of the outer.
Including colon in the continue statement.
โ Incorrect
The outer loop is labeled 'outer:, inner loop 'inner:, and continue uses 'outer to skip to next outer iteration.