CS Engineering Gyan

Conditional Statements in C

So far, every program in this series has executed its instructions in a straight line, one statement after another, without ever pausing to make a decision. Real programs rarely work this way. A login system needs to decide whether a password is correct, an e-commerce website needs to decide whether an item is in stock, and a grading program needs to decide which letter grade a score deserves. This kind of decision-making is handled through conditional statements.

Conditional statements allow a program to choose between different paths of execution based on whether a particular condition evaluates to true or false. Instead of following the same sequence of instructions every single time, the program can branch, skip certain sections entirely, or repeat different logic depending on the data it encounters while running.

In this tutorial, you will learn how the basic if statement works, how if-else extends it to handle two possible outcomes, how nested if statements and the else-if ladder handle multiple conditions, how the switch statement offers an alternative approach for specific kinds of comparisons, and how to avoid some of the most common mistakes beginners make when writing conditional logic.


The if Statement

The if statement is the simplest form of decision-making in C. It evaluates a condition, and if that condition turns out to be true, the block of code inside the if statement runs. If the condition is false, that block is skipped entirely, and the program continues with whatever comes after it.

Syntax

if (condition) {

    // code that runs only if condition is true

}

Example

#include <stdio.h>

int main() {

    int subscribers = 12000;

    if (subscribers > 10000) {

        printf("Channel has crossed 10K subscribers!");

    }

    return 0;

}

Output

Channel has crossed 10K subscribers!

Since the condition inside the parentheses evaluates to true in this example, the message inside the curly braces is displayed. If the subscriber count had been lower than 10000, the program would have produced no output at all, since there is nothing to handle the false case yet.


The if-else Statement

While a plain if statement only handles the true case, most real decisions involve two possible outcomes. The if-else statement adds an alternate block of code that runs specifically when the condition turns out to be false.

Syntax

if (condition) {

    // runs if condition is true

} else {

    // runs if condition is false

}

Example

#include <stdio.h>

int main() {

    int viewCount = 800;

    if (viewCount >= 1000) {

        printf("This video is performing well.");

    } else {

        printf("This video needs more promotion.");

    }

    return 0;

}

Output

This video needs more promotion.

Because the view count in this example falls below the threshold set in the condition, the else block executes instead, demonstrating how if-else guarantees that exactly one of the two blocks will always run.


Nested if Statements

Sometimes a decision depends on more than one factor, where a second condition only needs to be checked after the first one has already been confirmed true. This is achieved by placing one if statement inside another, known as nesting.

Example

#include <stdio.h>

int main() {

    int subscribers = 15000;

    int uploadsThisMonth = 6;

    if (subscribers > 10000) {

        if (uploadsThisMonth >= 4) {

            printf("Channel qualifies for the creator program.");

        } else {

            printf("Subscriber count is sufficient, but uploads are too low.");

        }

    } else {

        printf("Channel does not meet the subscriber requirement.");

    }

    return 0;

}

Output

Channel qualifies for the creator program.

In this example, the inner condition checking upload frequency is only evaluated after the outer condition regarding subscriber count has already passed, illustrating how nested conditions allow a program to check requirements in a specific, dependent order.


The else-if Ladder

When a program needs to check several distinct conditions in sequence, rather than just one or two, the else-if ladder provides a cleaner alternative to writing multiple separate nested if statements. Each condition is checked in order, and the first one that evaluates to true has its block executed, after which the remaining conditions are skipped entirely.

Syntax

if (condition1) {

    // runs if condition1 is true

} else if (condition2) {

    // runs if condition1 is false and condition2 is true

} else {

    // runs if none of the above conditions are true

}

Example

#include <stdio.h>

int main() {

    int score = 78;

    if (score >= 90) {

        printf("Grade: A");

    } else if (score >= 75) {

        printf("Grade: B");

    } else if (score >= 60) {

        printf("Grade: C");

    } else {

        printf("Grade: F");

    }

    return 0;

}

Output

Grade: B

Even though the score also satisfies the third condition checking for 60 or above, the ladder stops as soon as it finds the first true condition, which in this case is the check for 75 or above, meaning only Grade B is displayed.


The switch Statement

The switch statement offers an alternative way to handle multiple possible values of a single variable, particularly when comparing that variable against several fixed, specific values rather than ranges. It is often considered more readable than a long else-if ladder in these particular situations.

Syntax

switch (expression) {

    case value1:

        // code for value1

        break;

    case value2:

        // code for value2

        break;

    default:

        // code if no case matches

}

Example

#include <stdio.h>

int main() {

    int dayNumber = 3;

    switch (dayNumber) {

        case 1:

            printf("Monday upload scheduled.");

            break;

        case 2:

            printf("Tuesday upload scheduled.");

            break;

        case 3:

            printf("Wednesday upload scheduled.");

            break;

        default:

            printf("No upload scheduled today.");

    }

    return 0;

}

Output

Wednesday upload scheduled.

The break statement plays a critical role here, since it stops execution from continuing into the next case once a match has been found. Without it, the program would keep executing every subsequent case block until it either hits a break or reaches the end of the switch statement entirely, a behavior known as fall-through.


Comparing if-else and switch

Aspect if-else Ladder switch Statement
Best Suited For Range-based conditions or comparisons involving multiple different variables. Comparing a single variable against several fixed, specific values.
Readability Can become harder to read as the number of conditions grows. Often more readable when checking many possible fixed values.
Flexibility Supports complex conditions involving relational and logical operators. Limited to checking exact matches, typically with integers or characters.

Using Logical Operators Within Conditions

Conditional statements often become more powerful when combined with the logical operators covered in an earlier tutorial, allowing a single condition to check multiple requirements simultaneously rather than relying purely on nested if statements.

Example

#include <stdio.h>

int main() {

    int age = 20;

    int hasID = 1;

    if (age >= 18 && hasID == 1) {

        printf("Access granted.");

    } else {

        printf("Access denied.");

    }

    return 0;

}

Output

Access granted.

Combining conditions this way often results in cleaner, more compact code compared to writing separate nested if statements for each individual requirement.


Best Practices for Writing Conditional Statements


Common Mistakes Beginners Make

Mistake Correct Practice
Using a single equals sign instead of a double equals sign inside a condition. Always use the double equals sign when comparing values for equality inside a condition.
Forgetting the break statement inside switch cases. Include a break statement at the end of each case, unless fall-through behavior is intentionally desired.
Writing overly deep nested if statements that are hard to follow. Consider restructuring the logic using an else-if ladder or combining conditions with logical operators.
Assuming an else-if ladder checks every condition regardless of earlier matches. Remember that the ladder stops at the first true condition and skips the remaining checks entirely.
Using switch with data types it does not support, such as floating-point values. Use switch only with integer or character expressions, and rely on if-else for other data types.

Frequently Asked Interview Questions

  1. What is the purpose of an if statement in C?
    An if statement allows a program to execute a block of code only when a specified condition evaluates to true.
  2. What is the difference between if and if-else?
    An if statement only handles the true case, while if-else also provides an alternate block of code that runs when the condition is false.
  3. What is a nested if statement?
    A nested if statement is an if statement placed inside another if statement, used when a second condition should only be checked after the first one is true.
  4. What is the else-if ladder used for?
    The else-if ladder is used to check several distinct conditions in sequence, executing the block for the first condition that evaluates to true.
  5. What is the role of the break statement inside a switch case?
    The break statement stops execution from continuing into subsequent cases once a matching case has already been handled.
  6. What happens if the break statement is omitted in a switch case?
    Execution falls through into the following case or cases, continuing to run their code until a break is encountered or the switch statement ends.
  7. What is the default case in a switch statement?
    The default case runs when none of the other specified cases match the value of the expression being evaluated.
  8. What data types can be used with a switch statement in C?
    A switch statement in C generally supports integer and character expressions, rather than floating-point or string values.
  9. When should a switch statement be preferred over an if-else ladder?
    A switch statement is generally preferred when comparing a single variable against several specific fixed values, rather than checking ranges or multiple different variables.
  10. Can logical operators be used inside an if condition?
    Yes, logical operators such as AND and OR can be used within an if condition to combine multiple requirements into a single check.
  11. What is a common mistake when writing equality checks inside conditions?
    A common mistake is using a single equals sign instead of a double equals sign, which performs an assignment rather than a comparison.
  12. Does the else-if ladder evaluate every condition even after finding a true one?
    No, once a true condition is found, its block executes and the remaining conditions in the ladder are skipped entirely.

Summary

Conditional statements give C programs the ability to make decisions, transforming a rigid sequence of instructions into flexible logic that responds differently depending on the data it encounters. The if statement handles the simplest case, if-else extends this to cover two possible outcomes, nested if statements and the else-if ladder manage more complex decisions involving multiple conditions, and the switch statement offers a clean alternative when comparing a single variable against several fixed values.

In this tutorial, you learned how each of these constructs works individually, saw how they can be combined with logical operators for more expressive conditions, and reviewed some of the common mistakes that trip up beginners when writing decision-making logic. With a solid grasp of conditional statements in place, you are now ready to explore loops, which allow a program to repeat a block of code multiple times based on a condition.


← Previous: Input & Output Next: Loops in C →

Home Visit Our YouTube Channel