C
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Arrays and Strings
-
Section 5: Pointers
-
Section 6: Memory Management
-
Section 7: Structures and Unions
-
Section 8: The Preprocessor and Build Process
-
Section 9: Standard Library: stdio.h
-
Section 10: Standard Library: stdlib.h
-
Section 11: Standard Library: string.h
-
Section 12: Standard Library: ctype.h and wctype.h
-
Section 13: Standard Library: math.h, complex.h, fenv.h, tgmath.h
-
Section 14: Standard Library: Type and Limit Headers
-
Section 15: Standard Library: Error Handling and Debugging
-
Section 16: Standard Library: Localization and Encoding
-
Section 17: Standard Library: time.h
-
Section 18: Standard Library: Concurrency (C11)
-
Section 19: POSIX and System Programming (unistd.h)
-
Section 20: More Data Structures
-
Section 21: Algorithms in C
-
Section 22: Bitwise Operations
-
Section 23: Command-Line Programs
-
Section 24: Debugging and Best Practices
-
Section 25: Compiler and Language Internals
-
Section 26: Embedded and Cross-Platform Considerations
-
Section 27: Networking Basics
-
Section 28: Practical Projects
-
Section 29: Interview Practice
-
Section 30: C23 Modern Features
-
Section 31: More Practice and Review
19: If/Else Conditionals
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!
There are no comments for now.