Conditionals in C

1️⃣ What Are Conditionals?

Conditionals let your program make decisions.
You can tell the program:

πŸ‘‰ β€œIf this condition is true, do something. Otherwise, do something else.”


2️⃣ The if Statement

Syntax:

if (condition) {
    // code to execute if condition is true
}

πŸ›  Example:

int age = 18;

if (age >= 18) {
    printf("You are an adult.\n");
}

3️⃣ The if-else Statement

Use else when you want to do something different if the condition is false.

Syntax:

if (condition) {
    // true block
} else {
    // false block
}

πŸ›  Example:

int num = 5;

if (num % 2 == 0) {
    printf("Even number.\n");
} else {
    printf("Odd number.\n");
}

4️⃣ The else if Ladder

Use else if when you have multiple conditions.

Syntax:

if (condition1) {
    // block 1
} else if (condition2) {
    // block 2
} else {
    // block 3
}

πŸ›  Example:

int score = 85;

if (score >= 90) {
    printf("Grade: A\n");
} else if (score >= 75) {
    printf("Grade: B\n");
} else if (score >= 60) {
    printf("Grade: C\n");
} else {
    printf("Fail\n");
}

5️⃣ Logical Operators

You can combine conditions using:

OperatorMeaningExample
&&ANDif (a > 0 && b > 0)
``
!NOT (negation)if (!(a > 0)) (means a ≀ 0)

6️⃣ Relational Operators

OperatorMeaningExample
==Equals toif (a == b)
!=Not equal toif (a != b)
>Greater thanif (a > b)
<Less thanif (a < b)
>=Greater or equal toif (a >= b)
<=Less or equal toif (a <= b)

7️⃣ The switch Statement

The switch is great when you have multiple options to choose from, based on the value of a single variable.

Syntax:

switch (expression) {
    case value1:
        // code block
        break;
    case value2:
        // code block
        break;
    default:
        // default block
}

πŸ›  Example: Simple Menu

int choice = 2;

switch (choice) {
    case 1:
        printf("Pizza\n");
        break;
    case 2:
        printf("Pasta\n");
        break;
    case 3:
        printf("Burger\n");
        break;
    default:
        printf("Invalid choice\n");
}

πŸ” Key Points:

  • break; β†’ Stops execution of the switch. Without it, fall-through happens (the next case runs too).
  • default: β†’ Optional, runs if no case matches.
  • The switch can only test integers and characters.

8️⃣ Example: Switch with char

char grade = 'B';

switch (grade) {
    case 'A':
        printf("Excellent!\n");
        break;
    case 'B':
        printf("Good job.\n");
        break;
    case 'C':
        printf("Well done.\n");
        break;
    default:
        printf("Invalid grade.\n");
}

9️⃣ When to Use if-else vs. switch

Use CaseBetter Choice
Complex conditions (using >, <, &&, etc.)if-else
Checking a variable against multiple fixed valuesswitch
Comparing strings or floatsif-else (switch won’t work)

πŸ”§ Common Mistakes to Avoid

  • βœ… Always use {} braces if you have more than one statement in if or else.
  • ❌ Forgetting break; in switch leads to unintended execution of the next case (fall-through).
  • ❌ Using switch with floating-point numbers or strings (only integers/chars work).

βœ… Conclusion

  • if, if-else, and else if handle flexible condition checking.
  • switch is great for menu-driven programs or matching fixed values.
  • Choose the right tool based on your problem!

πŸ’‘ Next step idea: Try writing a calculator program using switch that handles +, -, *, / based on user choice.

Feedback Display

Learner Reviews