๐ Scenario: You are creating a simple program that checks a person's age and whether they have a membership card to decide if they can enter a club.
๐ฏ Goal: Build a Rust program that uses nested if conditions to check if a person is old enough and has a membership card to enter the club.
๐ What You'll Learn
Create a variable age with the exact value 20.
Create a variable has_membership with the exact value true.
Use nested if conditions to check if age is 18 or more and if has_membership is true.
Print "Access granted" if both conditions are true, otherwise print "Access denied".
๐ก Why This Matters
๐ Real World
Many places like clubs or websites check multiple rules before allowing access. Nested conditions help check these rules clearly.
๐ผ Career
Understanding nested conditions is important for writing programs that make decisions based on several factors, a common task in software development.
Progress0 / 4 steps
1
Create the initial variables
Create a variable called age and set it to 20. Also create a variable called has_membership and set it to true.
Rust
Hint
Use let to create variables. For example, let age = 20;.
2
Add the first condition to check age
Inside the main function, add an if condition to check if age is greater than or equal to 18.
Rust
Hint
Use if age >= 18 { } to check the age.
3
Add nested condition to check membership
Inside the if age >= 18 block, add another if condition to check if has_membership is true.
Rust
Hint
Use if has_membership { } inside the first if block.
4
Print access result based on conditions
Inside the nested if has_membership block, print "Access granted". Otherwise, print "Access denied" if either condition fails.
Rust
Hint
Use println!("Access granted"); and println!("Access denied"); in the right places.
Practice
(1/5)
1.
What does nested conditions mean in Rust?
Choose the best description.
easy
A. Writing conditions without any if statements.
B. Using only one condition without any others.
C. Checking one condition inside another condition.
D. Ignoring conditions and running all code blocks.
Solution
Step 1: Understand the meaning of nested conditions
Nested conditions mean putting one if or else if inside another to check multiple steps.
Step 2: Compare options with definition
Only Checking one condition inside another condition. correctly describes checking one condition inside another.
Final Answer:
Checking one condition inside another condition. -> Option C
Quick Check:
Nested conditions = one condition inside another [OK]
Hint: Nested means inside another condition [OK]
Common Mistakes:
Thinking nested means only one condition
Confusing nested with multiple separate conditions
Ignoring the use of if inside another if
2.
Which of the following is the correct syntax for nested if conditions in Rust?
fn main() {
let x = 10;
if x > 5 {
if x < 15 {
println!("x is between 6 and 14");
}
}
}
easy
A. The code shown above is correct.
B. You must use else if instead of nested if.
C. Rust does not allow nested if statements.
D. You need to add semicolons after if conditions.
Solution
Step 1: Check Rust syntax rules for nested if
Rust allows if inside another if block without semicolons after conditions.
Step 2: Verify the given code
The code uses nested if correctly and prints the message if both conditions are true.
Final Answer:
The code shown above is correct. -> Option A
Quick Check:
Nested if syntax correct = true [OK]
Hint: Nested if blocks need braces, no semicolon after conditions [OK]
Common Mistakes:
Adding semicolons after if conditions
Thinking else if must replace nested if
Believing Rust disallows nested if
3.
What will be the output of this Rust code?
fn main() {
let num = 8;
if num > 5 {
if num < 10 {
println!("A");
} else {
println!("B");
}
} else {
println!("C");
}
}
medium
A. B
B. A
C. C
D. No output
Solution
Step 1: Check outer if condition
Variable num is 8, which is greater than 5, so enter the first if block.
Step 2: Check inner if condition
Inside, check if 8 is less than 10, which is true, so print "A".
Find the error in this nested condition Rust code:
fn main() {
let x = 3;
if x > 0
if x < 5 {
println!("x is between 1 and 4");
}
}
medium
A. The println! macro is used wrongly.
B. Incorrect comparison operators used.
C. Variable x is not declared.
D. Missing braces for the outer if block.
Solution
Step 1: Check syntax of outer if
The outer if lacks braces, so the inner if is not properly nested.
Step 2: Understand Rust's block requirements
Rust requires braces to group nested blocks clearly; missing braces cause syntax errors.
Final Answer:
Missing braces for the outer if block. -> Option D
Quick Check:
Braces needed for nested if blocks [OK]
Hint: Always use braces {} for nested if blocks [OK]
Common Mistakes:
Skipping braces for nested if
Misreading comparison operators
Assuming println! usage is wrong
5.
Write a nested condition in Rust that prints:
"Small and even" if a number is less than 10 and even,
"Small and odd" if less than 10 and odd,
"Large number" if 10 or more.
Which code snippet correctly implements this?
fn main() {
let n = 8;
// Choose the correct nested condition
}
hard
A. if n < 10 {
if n % 2 == 0 {
println!("Small and even");
} else {
println!("Small and odd");
}
} else {
println!("Large number");
}
B. if n % 2 == 0 {
if n < 10 {
println!("Small and even");
} else {
println!("Large number");
}
} else {
println!("Small and odd");
}
C. if n < 10 && n % 2 == 0 {
println!("Small and even");
} else if n < 10 {
println!("Small and odd");
} else {
println!("Large number");
}
D. if n < 10 {
println!("Small and even");
} else if n % 2 == 0 {
println!("Large number");
} else {
println!("Small and odd");
}
Solution
Step 1: Check outer condition for number size
if n < 10 {
if n % 2 == 0 {
println!("Small and even");
} else {
println!("Small and odd");
}
} else {
println!("Large number");
} first checks if n < 10, then inside checks even or odd.
Step 2: Verify inner conditions and outputs
If even, prints "Small and even"; else prints "Small and odd". If not less than 10, prints "Large number".
Final Answer:
Option A correctly implements the nested conditions as described. -> Option A
Quick Check:
Outer size check, inner even/odd check = if n < 10 {
if n % 2 == 0 {
println!("Small and even");
} else {
println!("Small and odd");
}
} else {
println!("Large number");
} [OK]
Hint: Check outer condition first, then inner even/odd [OK]
Common Mistakes:
Checking even before size
Mixing up print messages
Using flat conditions instead of nested
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.