C if...else Statements

C if...else Statements: Controlling Program Flow

In real life, we make decisions every day based on certain conditions. For example, "If it is raining, I will take an umbrella; otherwise, I will wear sunglasses." Programming is no different!

In C programming, conditional statements allow your code to make decisions and execute different blocks of code based on whether a specific condition is true or false. This is known as control flow.

Mastering the if...else statements is critical for any C programmer, as it forms the backbone of logic in almost every software application.


1. The if Statement

The if statement is the simplest form of decision-making in C. It tells the compiler to execute a specific block of code only if a given condition evaluates to true (a non-zero value).

Syntax:

if (condition) {
  // Code to be executed if the condition is true
}

If the condition is false (evaluates to 0), the code inside the curly braces {} is completely ignored, and the program continues with the next line of code after the if block.

Simple if Example

#include <stdio.h>

int main() { int age = 20;

// Checking if age is greater than or equal to 18 if (age >= 18) { printf("You are eligible to vote!\n"); }

printf("Program continues...\n"); return 0; }


2. The if...else Statement

What if you want to execute one block of code when the condition is true, and a different block of code when it is false? This is where the else statement comes in.

Syntax:

if (condition) {
  // Code to run if condition is true
} else {
  // Code to run if condition is false
}

The if...else Example

#include <stdio.h>

int main() { int time = 20;

if (time < 18) { printf("Good day.\n"); } else { printf("Good evening.\n"); }

return 0; }

In the example above, since time is 20, the condition time < 18 is false. Therefore, the code inside the else block runs, printing "Good evening."


3. The if...else if...else Ladder

Often, you will need to test multiple conditions. The else if statement allows you to chain together multiple checks. The program will evaluate them one by one from top to bottom. As soon as a condition is true, its corresponding code block is executed, and the rest of the ladder is skipped.

Syntax:

if (condition1) {
  // Code for condition1
} else if (condition2) {
  // Code for condition2
} else if (condition3) {
  // Code for condition3
} else {
  // Code if none of the above are true
}

Grading System Example

#include <stdio.h>

int main() { 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"); }

return 0; }


4. Nested if...else Statements

You can place an if or if...else statement inside another if or else statement. This is called nesting. It is useful when you need to test for a condition, and based on that result, test for another condition.

Nested if Example

#include <stdio.h>

int main() { int num = 35;

if (num > 0) { printf("Number is positive.\n"); // Nested if if (num % 2 == 0) { printf("Number is also even.\n"); } else { printf("Number is also odd.\n"); } } else { printf("Number is negative or zero.\n"); }

return 0; }

Pro Tip: While nesting is powerful, nesting too deep (e.g., more than 3 levels) can make your code very difficult to read. Try to use logical operators (&&, ||) to combine conditions when possible!


5. The Ternary Operator (Short Hand If...Else)

If you have a simple if...else statement that assigns a value to a variable, you can use the ternary operator ? : as a shorthand. This makes your code more concise and readable.

Syntax:

variable = (condition) ? expressionTrue : expressionFalse;

Ternary Operator Example

#include <stdio.h>

int main() { int time = 20;

// Using Ternary Operator (time < 18) ? printf("Good day.\n") : printf("Good evening.\n");

return 0; }


Common Mistakes & Best Practices

When writing conditions in C, beginners often fall into a few common traps. Here is how to avoid them:

1. The Single Equal Sign Trap (= vs ==)

In C, a single equals sign = is an assignment operator, while a double equals == is an equality operator. Bad: if (x = 10) (This assigns 10 to x, evaluates to true, and creates a bug). Good: if (x == 10) (This correctly checks if x is equal to 10).

2. Missing Curly Braces

If you omit curly braces {}, only the very next line is considered part of the if statement. It is a best practice to always use curly braces, even for single lines, to prevent future bugs.


Exercise 1 of 2

?

Which operator is used for equality comparison in an `if` statement?

Exercise 2 of 2

?

What will `if (0)` evaluate to in C?