Making Decisions

So far, our programs run straight through. Line 1, then line 2, then line 3. Every time.

Real programs need to make choices. “If the user is old enough, let them in. Otherwise, turn them away.”

That’s called control flow. You control which code runs based on conditions.

Making Decisions with if

The if statement runs code only when something is true:

int age = 18;

if (age >= 18) {
    printf("You can vote.\n");
}

Read this as: “If age is greater than or equal to 18, then print this message.”

The code inside the curly braces { } only runs when the condition in parentheses is true.

Either/Or with if-else

Sometimes you want to do one thing if true, something else if false:

int score = 75;

if (score >= 60) {
    printf("You passed!\n");
} else {
    printf("You failed.\n");
}

Only one message prints. Never both.

Multiple Options with else if

For more than two possibilities:

int score = 85;

if (score >= 90) {
    printf("Grade: A\n");
} else if (score >= 80) {
    printf("Grade: B\n");
} else if (score >= 70) {
    printf("Grade: C\n");
} else if (score >= 60) {
    printf("Grade: D\n");
} else {
    printf("Grade: F\n");
}

The computer checks each condition from top to bottom. When it finds a true one, it runs that code and skips the rest.

If score is 85:

  • Is 85 >= 90? No. Skip.
  • Is 85 >= 80? Yes! Print “Grade: B” and we’re done.

Comparison Operators

These symbols compare two values:

SymbolMeaningExample
==equalsx == 5 (is x equal to 5?)
!=not equalsx != 5 (is x not equal to 5?)
<less thanx < 5
>greater thanx > 5
<=less than or equalx <= 5
>=greater than or equalx >= 5

The Most Common Mistake

Notice that “equals” uses TWO equal signs: ==

One equal sign = means “put this value in the variable.”

Two equal signs == means “check if these are the same.”

if (x = 5)   // WRONG! This puts 5 into x, then checks if 5 is true
if (x == 5)  // RIGHT! This checks if x equals 5

Every C programmer makes this mistake at some point. The compiler might not stop you because technically it’s valid code. It just doesn’t do what you expect.

This is C trusting you. Be careful.

Combining Conditions

You can combine multiple conditions using logical operators:

SymbolMeaningTrue when…
&&ANDboth sides are true
||ORat least one side is true
!NOTthe condition is false
int age = 16;
int has_permit = 1;  // 1 means true

// AND: both must be true
if (age >= 16 && has_permit) {
    printf("You can drive with an adult.\n");
}

// OR: at least one must be true
if (age < 13 || age > 65) {
    printf("You get a discount.\n");
}

// NOT: flips true to false (and false to true)
if (!has_permit) {
    printf("You need a permit first.\n");
}

True and False in C

Here’s something important: C doesn’t have special true and false words. Instead:

  • Zero (0) means false
  • Anything else means true
int x = 5;
if (x) {
    printf("This prints because x is not zero\n");
}

int y = 0;
if (y) {
    printf("This does NOT print because y is zero\n");
}

This is why we use 1 for true and 0 for false:

int game_over = 0;  // false
int is_raining = 1; // true

The switch Statement

When comparing one value against many options, switch is cleaner than lots of if-else:

char grade = 'B';

switch (grade) {
    case 'A':
        printf("Excellent!\n");
        break;
    case 'B':
        printf("Good job!\n");
        break;
    case 'C':
        printf("Acceptable.\n");
        break;
    case 'D':
        printf("Needs improvement.\n");
        break;
    case 'F':
        printf("Failed.\n");
        break;
    default:
        printf("Invalid grade.\n");
        break;
}

Don’t Forget break!

Each case needs a break at the end. Without it, the code “falls through” to the next case:

int day = 2;

switch (day) {
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
        printf("Weekday\n");
        break;
    case 6:
    case 7:
        printf("Weekend\n");
        break;
}

Here, falling through is intentional. Days 1-5 all print “Weekday”.

But if you forget break by accident, your code runs cases you didn’t want. This is a common bug.

The Conditional Operator (Shorthand If)

There’s a shortcut for simple if-else statements:

int age = 20;

// The long way
char *status;
if (age >= 18) {
    status = "adult";
} else {
    status = "minor";
}

// The short way
char *status = (age >= 18) ? "adult" : "minor";

The ?: is called the conditional operator or ternary operator (ternary means “three parts”).

It works like this:

condition ? value_if_true : value_if_false

Read it as: “If age >= 18, use ‘adult’. Otherwise, use ‘minor’.”

It’s useful for short, simple choices:

int a = 10, b = 20;
int max = (a > b) ? a : b;  // max is 20

printf("You have %d item%s\n", count, (count == 1) ? "" : "s");

Don’t use it for complicated conditions. It gets hard to read. When in doubt, use regular if-else.

Complete Example: Grade Calculator

#include <stdio.h>

int main(void) {
    int score;

    printf("Enter your test score: ");
    scanf("%d", &score);

    // Check if valid
    if (score < 0 || score > 100) {
        printf("Invalid score!\n");
        return 1;
    }

    // Determine grade
    char grade;
    if (score >= 90) {
        grade = 'A';
    } else if (score >= 80) {
        grade = 'B';
    } else if (score >= 70) {
        grade = 'C';
    } else if (score >= 60) {
        grade = 'D';
    } else {
        grade = 'F';
    }

    // Print result
    printf("Your grade is: %c\n", grade);

    // Bonus message
    if (grade == 'A') {
        printf("Excellent work!\n");
    } else if (grade == 'F') {
        printf("See me after class.\n");
    }

    return 0;
}

Try It Yourself

  1. Write a program that asks for your age and tells you if you can vote (18+), drive (16+), or neither
  2. Write a program that takes a number 1-7 and prints the day of the week (use switch)
  3. Write a program that checks if a number is positive, negative, or zero
  4. What happens if you use = instead of ==? Try it!

Common Mistakes

  • Using = instead of == for comparison
  • Forgetting break in switch statements (causes fall-through)
  • Forgetting curly braces: without { }, only the next line is part of the if

Without braces:

if (score >= 90)
    printf("Grade A\n");
    printf("Great job!\n");  // THIS ALWAYS RUNS! Not part of the if!

With braces (correct):

if (score >= 90) {
    printf("Grade A\n");
    printf("Great job!\n");  // Only runs when score >= 90
}

Always use braces. Always.

Next Up

In Part 7, we’ll learn about loops - how to repeat actions without writing the same code over and over.


Enjoyed This?

If this helped something click, subscribe to my YouTube channel. More content like this, same approach - making things stick without insulting your intelligence. It’s free, it helps more people find this stuff, and it tells me what’s worth making more of.