Bird
Raised Fist0
Rustprogramming~10 mins

If–else expression in Rust - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - If–else expression
Evaluate condition
↓
Condition true?
No→Execute else block
↓
Use else result
↓
Execute if block
↓
Use if result
↓
Continue with result
The program checks a condition. If true, it runs the 'if' block and uses its result. Otherwise, it runs the 'else' block and uses that result.
Execution Sample
Rust
let number = 7;
let result = if number % 2 == 0 {
    "even"
} else {
    "odd"
};
println!("{} is {}", number, result);
This code checks if 'number' is even or odd and stores the string result, then prints it.
Execution Table
StepActionConditionCondition ResultBlock ExecutedResult Value
1Evaluate condition number % 2 == 07 % 2 == 0falseelse"odd"
2Assign result variableN/AN/AN/A"odd"
3Print outputN/AN/AN/A7 is odd
💡 Condition is false, so else block runs; program prints '7 is odd' and ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
number7777
resultuninitializeduninitialized"odd""odd"
Key Moments - 2 Insights
Why does the else block run even though the condition uses '=='?
Because the condition '7 % 2 == 0' evaluates to false (7 divided by 2 leaves remainder 1), so the else block runs as shown in execution_table step 1.
Is the if–else expression itself a value?
Yes, the if–else expression returns a value from either block, which is assigned to 'result' as shown in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'result' after step 2?
A"even"
B"odd"
C7
Duninitialized
💡 Hint
Check the 'Result Value' column in execution_table row for step 2.
At which step does the program decide which block to execute?
AStep 1
BStep 3
CStep 2
DAfter printing
💡 Hint
Look at the 'Condition Result' and 'Block Executed' columns in execution_table.
If 'number' was 8 instead of 7, what would 'result' be after step 2?
A"odd"
B8
C"even"
Duninitialized
💡 Hint
Think about the condition 'number % 2 == 0' when number is 8, and check execution_table logic.
Concept Snapshot
If–else expression in Rust:
let result = if condition {
    value_if_true
} else {
    value_if_false
};
It evaluates condition, runs one block, and returns its value.
Useful for choosing values based on conditions.
Full Transcript
This Rust code uses an if–else expression to check if a number is even or odd. It evaluates the condition 'number % 2 == 0'. If true, it returns "even"; otherwise, it returns "odd". The returned value is stored in 'result'. Finally, it prints the number and whether it is even or odd. The execution table shows the condition evaluation, block chosen, and final output. Variables 'number' and 'result' change as the program runs. This example helps understand how if–else expressions return values in Rust.

Practice

(1/5)
1. What does an if-else expression in Rust do?
easy
A. Declares a variable without value
B. Repeats a block of code multiple times
C. Defines a new function
D. Chooses between two options and returns a value

Solution

  1. Step 1: Understand the purpose of if-else

    An if-else expression lets the program pick one of two paths based on a condition.
  2. Step 2: Recognize that it returns a value

    In Rust, if-else expressions return a value that can be stored in a variable.
  3. Final Answer:

    Chooses between two options and returns a value -> Option D
  4. Quick Check:

    If-else expression = choice with value [OK]
Hint: If-else picks one path and returns a value [OK]
Common Mistakes:
  • Thinking if-else repeats code like a loop
  • Confusing if-else with function definition
  • Ignoring that if-else returns a value
2. Which of the following is the correct syntax for an if-else expression in Rust?
easy
A. let x = if condition { 5 } else { 10 };
B. if (condition) then { 5 } else { 10 };
C. let x = if condition: 5 else: 10;
D. let x = if condition { 5 } elseif { 10 };

Solution

  1. Step 1: Check Rust if-else syntax

    Rust uses if condition { value1 } else { value2 } with braces and no parentheses.
  2. Step 2: Identify correct option

    let x = if condition { 5 } else { 10 }; matches Rust syntax exactly with variable assignment and braces.
  3. Final Answer:

    let x = if condition { 5 } else { 10 }; -> Option A
  4. Quick Check:

    Rust if-else syntax = let x = if condition { 5 } else { 10 }; [OK]
Hint: Use braces and no parentheses in Rust if-else [OK]
Common Mistakes:
  • Using parentheses around condition
  • Writing elseif instead of else if
  • Using colons instead of braces
3. What is the output of this Rust code?
let number = 7;
let result = if number % 2 == 0 { "even" } else { "odd" };
println!("{}", result);
medium
A. odd
B. even
C. 7
D. Syntax error

Solution

  1. Step 1: Check the condition number % 2 == 0

    7 % 2 equals 1, which is not 0, so condition is false.
  2. Step 2: Determine which branch runs

    Since condition is false, the else branch runs, returning "odd".
  3. Final Answer:

    odd -> Option A
  4. Quick Check:

    7 is odd, so output = odd [OK]
Hint: Check condition result to pick if or else branch [OK]
Common Mistakes:
  • Assuming 7 is even
  • Confusing modulo operator
  • Expecting number to print instead of result
4. Identify the error in this Rust code:
let x = 10;
let y = if x > 5 { 1 };
println!("{}", y);
medium
A. Variable y is not declared
B. Incorrect comparison operator
C. Missing else branch in if expression
D. println! macro syntax error

Solution

  1. Step 1: Check if-else expression completeness

    When assigning with if, Rust requires both if and else branches to return a value.
  2. Step 2: Identify missing else branch

    The code has no else branch, so y's value is undefined, causing a compile error.
  3. Final Answer:

    Missing else branch in if expression -> Option C
  4. Quick Check:

    if-else must have else when used as expression [OK]
Hint: Always include else when assigning from if [OK]
Common Mistakes:
  • Forgetting else branch when assigning
  • Thinking if alone returns a value
  • Misreading error as println! issue
5. You want to assign a variable grade based on a score: if score is 90 or above, grade is 'A', else 'F'. Which code correctly uses if-else expression?
hard
A. let grade = if score > 90 { 'A' } else if score < 90 { 'F' };
B. let grade = if score >= 90 { 'A' } else { 'F' };
C. let grade = if score >= 90 { 'A' }; else { 'F' };
D. let grade = if score >= 90 then { 'A' } else { 'F' };

Solution

  1. Step 1: Check correct if-else expression syntax

    Rust uses if condition { value } else { value } without 'then' or semicolons inside.
  2. Step 2: Validate condition and branches

    let grade = if score >= 90 { 'A' } else { 'F' }; correctly checks >= 90 and returns 'A' or 'F' in else branch.
  3. Final Answer:

    let grade = if score >= 90 { 'A' } else { 'F' }; -> Option B
  4. Quick Check:

    Correct syntax and logic = let grade = if score >= 90 { 'A' } else { 'F' }; [OK]
Hint: Use braces and else for complete if-else expression [OK]
Common Mistakes:
  • Using 'then' keyword
  • Missing else branch
  • Incorrect semicolon placement
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard