5. Making Decisions II

Making Decisions II

Now we combine several decisions to solve richer problems. The same rule still applies: every diamond asks one yes/no question and has exactly two labelled exits. Here are three worked examples.

Example (a): Largest of three numbers

Problem

Read three numbers and display the largest. We keep a variable largest, start it at the first number, then compare it with the other two, updating it whenever we find something bigger.

Start READ a, b, c largest = a b > largest ? Yes largest = b No c > largest ? Yes largest = c No PRINT largest End

Pseudocode

START
  INPUT a, b, c
  SET largest = a
  IF b > largest THEN
    SET largest = b
  END IF
  IF c > largest THEN
    SET largest = c
  END IF
  OUTPUT largest
END

Trace table

Sample input: a = 3, b = 9, c = 5.

StepabclargestDecision
INPUT a, b, c395——
largest = a3953—
IF b > largest39539 > 3 → Yes
largest = b3959—
IF c > largest39595 > 9 → No
OUTPUT largest3959—

In Python

a = int(input("Enter a: "))
b = int(input("Enter b: "))
c = int(input("Enter c: "))
largest = a
if b > largest:
    largest = b
if c > largest:
    largest = c
print("The largest is", largest)

Output: (with a 3, b 9, c 5)

The largest is 9

Example (b): Student grade calculator

Problem

Read a test score from 0 to 100 and print a letter grade: A for 90+, B for 80–89, C for 70–79, D for 60–69, otherwise F. This is a ladder of decisions checked from the top down.

Start READ score score ≥ 90 ? Yes grade = "A" No score ≥ 80 ? Yes grade = "B" No score ≥ 70 ? Yes grade = "C" No score ≥ 60 ? Yes grade = "D" No grade = "F" PRINT grade End

Pseudocode

START
  INPUT score
  IF score >= 90 THEN
    SET grade = "A"
  ELSE IF score >= 80 THEN
    SET grade = "B"
  ELSE IF score >= 70 THEN
    SET grade = "C"
  ELSE IF score >= 60 THEN
    SET grade = "D"
  ELSE
    SET grade = "F"
  END IF
  OUTPUT grade
END

Trace table

Sample input: score = 85.

StepscoreTestResultgrade
INPUT score85———
IF score ≥ 908585 ≥ 90No—
ELSE IF score ≥ 808585 ≥ 80Yes—
grade = "B"85——B
OUTPUT grade85——B

In Python

score = int(input("Enter the score (0-100): "))
if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
elif score >= 60:
    grade = "D"
else:
    grade = "F"
print("Grade:", grade)

Output: (with score 85)

Grade: B

Example (c): Leap-year check

Problem

Read a year and decide whether it is a leap year. The rule: a year is a leap year if it is divisible by 4, except that years divisible by 100 are not leap years — unless they are also divisible by 400 (then they are).

Start READ year year mod 4 = 0 ? No PRINT "Not leap" Yes year mod 100 = 0 ? No PRINT "Leap year" Yes year mod 400 = 0 ? Yes PRINT "Leap year" No PRINT "Not leap" End

Pseudocode

START
  INPUT year
  IF year mod 4 = 0 THEN
    IF year mod 100 = 0 THEN
      IF year mod 400 = 0 THEN
        OUTPUT "Leap year"
      ELSE
        OUTPUT "Not a leap year"
      END IF
    ELSE
      OUTPUT "Leap year"
    END IF
  ELSE
    OUTPUT "Not a leap year"
  END IF
END

Trace table

Sample input: year = 2000.

StepyearTestResultOutput
INPUT year2000———
IF year mod 4 = 020002000 mod 4 = 0Yes—
IF year mod 100 = 020002000 mod 100 = 0Yes—
IF year mod 400 = 020002000 mod 400 = 0Yes—
OUTPUT2000——Leap year

In Python

year = int(input("Enter a year: "))
if year % 4 == 0:
    if year % 100 == 0:
        if year % 400 == 0:
            print(year, "is a leap year")
        else:
            print(year, "is not a leap year")
    else:
        print(year, "is a leap year")
else:
    print(year, "is not a leap year")

Output: (with year 2000)

2000 is a leap year

Experienced programmers often fold this into one line — if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0): — but the nested version matches the flowchart step by step, which makes the rule easier to learn.