Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close
    Lesson4 / 20

    Decisions: make the program choose the right path

    The goal is 30 minutes, and you study for exactly 30. Should the program say “Goal met”? The equality case will tell us whether the condition matches the rule.

    Predict a goal check

    We want the message Goal met. whenever completed study minutes are at least the goal. Read the program and predict both output lines. The two numbers are equal on purpose: a boundary often reveals whether a condition matches the requirement.

    C#
    using System;
    
    int completedMinutes = 30;
    int goalMinutes = 30;
    bool reachedGoal = completedMinutes >= goalMinutes;
    
    if (reachedGoal)
    {
        Console.WriteLine("Goal met.");
    }
    else
    {
        Console.WriteLine("Keep going.");
    }
    
    Console.WriteLine("Check complete.");

    What prints?

    A Keep going., then Check complete.
    B Goal met., then Check complete.
    C Both Goal met. and Keep going., then Check complete.
    D Nothing, because the numbers are equal.
    Show answer

    Correct answer: B

    30 >= 30 is true. The if block runs, the else block is skipped, and execution continues after the branch.

    Option A: At least includes equality.

    Option C: One if/else chooses one of its two branches.

    Option D: Equality does not stop execution.

    Hint: Translate >= as at least.

    A condition is a yes/no question

    A comparison produces a bool result. Use > for greater than, < for less than, >= for at least, <= for at most, == for equal, and != for not equal. The single equals sign assigns a value; the double equals sign compares values. Parentheses enclose the condition in an if statement.

    Text
    completedMinutes >= goalMinutes ?
              /                 \
           true                 false
        Goal met.             Keep going.
              \                 /
                 Check complete.

    Braces mark the statements belonging to each block. Indentation makes the structure easier to read, but braces determine its boundaries. A statement after the whole if/else runs after either branch completes normally. This is why Check complete. is printed for both a successful goal and an unfinished goal.

    Trace the equal-boundary case

    1. completedMinutes is 30 and goalMinutes is 30.
    2. The comparison 30 >= 30 produces true.
    3. The if block prints Goal met.; the else block is skipped.
    4. Execution continues after the closing brace and prints Check complete.

    The rule says at least 30 minutes. Which comparison includes exactly 30?

    A completedMinutes > 30
    B completedMinutes = 30
    C completedMinutes < 30
    D completedMinutes >= 30
    Show answer

    Correct answer: D

    >= includes 30 itself and values above it.

    Option A: > excludes equality, so it rejects the boundary value 30.

    Option B: A single = is assignment, not the comparison required here.

    Option C: < selects values below the goal.

    Test the boundary, not only one happy example

    Keep goalMinutes at 30. Predict the first message for completedMinutes values 29, 30 and 31 before running each edit. Expect Keep going., Goal met., and Goal met. respectively. A condition that works for 31 can still be wrong at 30.

    Add a third case

    For planned time, our rule has three cases: negative is invalid, zero means no plan yet, and positive means a plan exists. An else if adds another condition. Conditions are checked in order; the first matching branch runs, and the remaining branches in that chain are skipped.

    Write your attempt before reading this complete solution. Start with plannedMinutes set to 0:

    C#
    using System;
    
    int plannedMinutes = 0;
    
    if (plannedMinutes < 0)
    {
        Console.WriteLine("Minutes cannot be negative.");
    }
    else if (plannedMinutes == 0)
    {
        Console.WriteLine("No study time planned yet.");
    }
    else
    {
        Console.WriteLine("Study time is planned.");
    }

    What happens when plannedMinutes is 0 in the second program?

    A Only No study time planned yet. prints.
    B Only Minutes cannot be negative. prints.
    C Only Study time is planned. prints.
    D All three messages print.
    Show answer

    Correct answer: A

    0 is not below zero, but it is equal to zero, so the else-if block runs.

    Option B: Zero is not negative.

    Option C: The earlier matching branch handles zero, so the final else is skipped.

    Option D: An if/else-if/else chain selects its first matching branch.

    Change the value to -1 and then 5. Expect the negative warning and the planned-time message, respectively. Keep these three values in a tiny test table beside your code. When a program has more branches, a table helps you see which paths you have actually tried.

    Explain back: What is different about = and ==? Why do 29, 30 and 31 make a better goal test set than 31 alone?

    Reference: Microsoft — selection statements.

    Quick reference

    • Comparisons produce bool values.
    • = assigns; == compares equality.
    • >= includes equality; > does not.
    • Braces define which statements belong to a branch.
    • An if/else-if/else chain runs its first matching branch.
    • Check values below, at and above important boundaries.
    Sign in to mark lessons done and keep your place in the course.Sign in

    TechInterviewNotes Editorial Team · Updated 27 Oct 2026 ·