C Workshop · Comparison Operators
Lesson C

Comparison Operators

Comparisons ask a yes/no question: >, <, >=, <=, ==, !=. In C the result is 1 (true) or 0 (false).

What it is

A comparison asks a yes-or-no question about two values: bigger, smaller, equal, not equal. In C the answer is the number 1 (yes) or 0 (no). An if statement uses that answer to pick a path.

Approach

  1. Write the comparison (here a > b).
  2. If it is true, take the if body.
  3. Otherwise take else.

Where this is used in the real world

Diagram

1

a=5

left

2

>

compare

3

b=3

right

4

true

print Yes

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 a = 5, b = 3;
4    if (a > b) printf("Yes\n");
5    else printf("No\n");
6    return 0;
7}
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. 5 > 3 is:

Comparison yields 1 or 0.

2. Equality test spelling is:

Two equals.

3. This program prints:

if body runs.

4. if (a = b) is dangerous because:

Assignment, not compare.

5. != means:

Inequality.