0
0
Rustprogramming~10 mins

Nested conditions in Rust - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if a number is positive.

Rust
let num = 5;
if num [1] 0 {
    println!("Positive number");
}
Drag options to blanks, or click blank then click option'
A<
B>
C==
D<=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<' instead of '>' will check for negative numbers.
Using '==' will only check if the number is zero.
2fill in blank
medium

Complete the code to check if a number is between 1 and 10 (inclusive).

Rust
let num = 7;
if num >= 1 && num [1] 10 {
    println!("Number is between 1 and 10");
}
Drag options to blanks, or click blank then click option'
A<=
B>
C==
D<
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<' excludes 10 from the range.
Using '>' or '==' does not check the upper bound correctly.
3fill in blank
hard

Fix the error in the nested condition to print "Valid" if x is positive and less than 100.

Rust
let x = 50;
if x > 0 {
    if x [1] 100 {
        println!("Valid");
    }
}
Drag options to blanks, or click blank then click option'
A>=
B<=
C<
D==
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<=' includes 100, which is not intended.
Using '>=' or '==' will not check the correct range.
4fill in blank
hard

Fill both blanks to create a nested condition that prints "In range" if y is between 10 and 20 (exclusive).

Rust
let y = 15;
if y [1] 10 {
    if y [2] 20 {
        println!("In range");
    }
}
Drag options to blanks, or click blank then click option'
A>
B>=
C<
D<=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '>=' or '<=' includes the boundary values.
Mixing up the operators will cause incorrect range checks.
5fill in blank
hard

Fill all three blanks to create a nested condition that prints "Valid score" if score is between 50 and 100 (inclusive) and is even.

Rust
let score = 80;
if score [1] 50 {
    if score [2] 100 {
        if score % 2 [3] 0 {
            println!("Valid score");
        }
    }
}
Drag options to blanks, or click blank then click option'
A>=
B<=
C==
D!=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '>' or '<' excludes boundary values.
Using '!=' instead of '==' for even check causes wrong results.