ProgramGuru

Choose a branch with if and else

Choose a branch with if and else

A school uses a stated pass mark to choose a message. An if statement evaluates a Boolean condition and runs its branch when true. An else branch handles the alternative, so the program can give one appropriate message rather than always printing both.

Braces enclose each branch. Conditions must produce Bool, not a number treated as true or false. An else-if chain checks conditions in order and runs the first matching branch, which makes overlapping thresholds sensitive to order.

What you’ll learn

  • Trace the single selected if/else branch.
  • Check the pass threshold immediately below and at its boundary.

Choose a branch with if and else in the example

  1. Read score

    A school uses a stated pass mark to choose a message.

    Read score
  2. Evaluate >= 75

    Braces enclose each branch.

    Evaluate >= 75
  3. Run one branch

    The comparison includes 75, so the pass branch runs.

    Run one branch
The comparison includes 75, so the pass branch runs. The else branch is skipped. Only one line appears because these branches are alternatives, not two independently executed print statements.

Work through choose a branch with if and else

Save this complete program as main.swift and run it with the Swift toolchain. No separate input file is needed.

Swift · complete program

let score = 75
if score >= 75 {
  print("Pass")
} else {
  print("More practice")
}

Expected output

Pass

Follow the execution

The comparison includes 75, so the pass branch runs. The else branch is skipped. Only one line appears because these branches are alternatives, not two independently executed print statements.

Try this program in the Swift compiler. Paste the complete source above.

Practice: Choose a branch with if and else

Your task: What prints at score 74?

Swift · complete program

let score = 75
if score >= 75 {
  print("Pass")
} else {
  print("More practice")
}

Worked answer

More practice prints because 74 does not meet score >= 75. Checking a boundary and the value immediately below it is a useful way to catch an unintended greater-than comparison.

Check overlapping thresholds

For several grade bands, check higher thresholds first if their conditions overlap. Otherwise an early broad condition can absorb every higher score. Keep the rule tied to the school policy rather than treating this teaching threshold as universal.

Check your decision

What prints at score 74?
Choose one answer. You can try again.

Answer and explanation

More practice prints because 74 does not meet score >= 75.

More practice prints because 74 does not meet score >= 75. Checking a boundary and the value immediately below it is a useful way to catch an unintended greater-than comparison.

Choose a branch with if and else in practice

More practice prints because 74 does not meet score >= 75. Checking a boundary and the value immediately below it is a useful way to catch an unintended greater-than comparison. For several grade bands, check higher thresholds first if their conditions overlap.

Further reading: official Swift language guide.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Match cases with switch →