Skip to Content
Course content

19: If/Else Conditionals

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Up until now, our programs have been linear—they start at the top and march straight to the bottom. But real software needs to make decisions. To do that, we use conditionals. I like to think of these as forks in the road: depending on whether a specific condition is true or false, the program takes a different path.

Defining a danger zone

Let's build a simple monitoring system for a fictional space station. We need to check the atmospheric pressure. If the pressure drops below a certain threshold, we need to trigger an alarm. In C, we do this with the if statement.

int pressure = 45;

if (pressure < 50) {
    printf("WARNING: Low pressure detected!\n");
}

The code inside the parentheses is the condition. It evaluates to either true or false. If pressure < 50 is true, the code inside the curly braces runs. If it's false, C simply skips over that entire block as if it wasn't even there.

Closing the loop with an else

Right now, if the pressure is fine, the program just stays silent. That's a bit unsettling for a space station operator; they want to know the system is actually working. We can use an else block to handle every case where the if condition fails.

int pressure = 60;

if (pressure < 50) {
    printf("WARNING: Low pressure detected!\n");
} else {
    printf("Pressure levels nominal.\n");
}

Now it's a binary choice. Either the pressure is low, or it isn't. There is no third option here.

Scaling up to multiple conditions

In the real world, things aren't usually binary. We don't just care about low pressure; we also care if the pressure is dangerously high. This is where else if comes in. It allows us to chain multiple checks together.

int pressure = 110;

if (pressure < 50) {
    printf("WARNING: Low pressure detected!\n");
} else if (pressure > 100) {
    printf("WARNING: High pressure detected!\n");
} else {
    printf("Pressure levels nominal.\n");
}

The beauty of this chain is that C stops checking as soon as it finds a true condition. If the pressure is 40, it triggers the first block and ignores the rest. It won't even look at the else if or the else.

The missing brace trap

I want to show you a mistake I made early in my career that still trips up experienced devs. In C, if you don't provide curly braces {}, the if statement only controls the one single line immediately following it.

I tried to write this logic to handle a critical failure:

if (pressure < 20)
    printf("CRITICAL FAILURE!\n");
    printf("Evacuating station!\n");

I thought I was being clever by skipping the braces for "cleaner" code. But when I tested it with a pressure of 80, the output was:

Evacuating station!

I almost had a heart attack. What happened? Because I omitted the braces, only the first printf was tied to the if. The second printf was just a regular line of code that ran every single time, regardless of the pressure. Always use curly braces, even for one-liners. It saves you from a debugging nightmare.

if (pressure < 20) {
    printf("CRITICAL FAILURE!\n");
    printf("Evacuating station!\n");
}



📋 Practical Task

The Automated Turnstile Validator

You are writing the logic for a subway turnstile. Create a program that uses a variable int balance to determine if a passenger can enter. Implement the following logic:

  • If the balance is 5 or more, print "Access Granted" and subtract 5 from the balance.
  • If the balance is between 1 and 4 (inclusive), print "Insufficient Funds".
  • If the balance is 0 or negative, print "Card Invalid".

Ensure you use if, else if, and else correctly, and remember to use curly braces for all blocks!

Rating
0 0

There are no comments for now.

to be the first to leave a comment.