C Workshop · Grade Calculator
Lesson C

Grade Calculator

Map a score onto a letter with chained else if tests, highest bar first.

What it is

A grade calculator maps a score onto a letter by checking the highest bar first. 95 should be A, not C. The chain is a staircase you walk down until a step fits.

Approach

  1. Read the score.
  2. if (n >= 90) A, else if (n >= 75) B, else if (n >= 50) C, else F.
  3. Print one letter.

Where this is used in the real world

Diagram

1

85

score

2

>= 90?

no

3

>= 75?

yes

4

B

print

Run algorithm

Each step highlights the matching C line. Program output appears only when a line that prints actually runs.

Ready
C code
1#include <stdio.h>
2int main(void) {
3    int n;
4    scanf("%d", &n);
5    if (n >= 90) printf("A\n");
6    else if (n >= 75) printf("B\n");
7    else if (n >= 50) printf("C\n");
8    else printf("F\n");
9    return 0;
10}
What this line does
Press Run algorithm. The highlight will move through the C code.
True branch
the if body runs
False branch
else / skip
Program output
(no output yet)

Check your understanding

Five questions. Pick one answer each, then Check answers. The correct choice turns green.

1. Score 85 prints:

>= 75 and not >= 90.

2. Why test 90 before 50?

Order matters.

3. Score 49 prints:

Final else.

4. Score 90 prints:

>= 90.

5. This chain is better as switch when:

Ranges want if; exact days want switch.