Paramclasses Academy - Blog

Tutorials Details

←
Post 8 of 15
→

What is Conditional Statements in C Language

What is Conditional Statements in C Language

October 14, 2026

1. Introduction

In Module 3, we learned about operators in C programming. Now, we will learn how to make decisions in a program using conditional statements.

In everyday life, we make decisions based on conditions. For example:

  • If a student scores 40 or more marks, the student passes.

  • If the temperature is high, the cooling system turns on.

  • If the entered password is correct, access is granted.

  • If a number is divisible by 2, it is even.

Similarly, C programming provides conditional statements that allow a program to execute different blocks of code depending on whether a condition is true or false.

In this module, you will learn if, if-else, else-if, nested if, switch, and the break and default statements.

2. What Is a Conditional Statement?

A conditional statement allows a program to make a decision based on a condition.

In C, an expression is treated as false if its value is zero and true if its value is nonzero.

The main conditional structures are:

  • if

  • if-else

  • else-if ladder

  • Nested if

  • switch

3. The if Statement

The if statement executes a block of code only when its condition is true.

Syntax

if (condition)
{
    // Statements executed when the condition is true
}

Example 1: Checking a Number

#include <stdio.h>

int main(void)
{
    int age = 20;

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

    return 0;
}

Output:

You are eligible by age.

Explanation

  • age >= 18 checks whether the age is at least 18.

  • Because age is 20, the condition is true.

  • The printf() statement executes.

If age were less than 18, the statement inside the if block would be skipped.

4. The if-else Statement

The if-else statement allows a program to choose between two alternatives.

Syntax

if (condition)
{
    // Executes when the condition is true
}
else
{
    // Executes when the condition is false
}

Example 2: Checking Even or Odd

#include <stdio.h>

int main(void)
{
    int number;

    printf("Enter an integer: ");

    if (scanf("%d", &number) != 1)
    {
        printf("Invalid input.\n");
        return 1;
    }

    if (number % 2 == 0)
    {
        printf("The number is even.\n");
    }
    else
    {
        printf("The number is odd.\n");
    }

    return 0;
}

Sample input:

Enter an integer: 7

Output:

The number is odd.

Explanation

  • The % operator calculates the remainder.

  • If number % 2 == 0, the number is even.

  • Otherwise, the number is odd.

This works for both positive and negative integers.

5. The else-if Ladder

An else-if ladder is useful when you need to check multiple conditions.

Syntax

if (condition1)
{
    // First condition is true
}
else if (condition2)
{
    // Second condition is true
}
else if (condition3)
{
    // Third condition is true
}
else
{
    // No previous condition is true
}

The conditions are checked in order. Once a condition is true, its corresponding block executes, and the remaining branches are skipped.

Example 3: Student Grade Calculator

Let's assign grades based on marks.

#include <stdio.h>

int main(void)
{
    int marks;

    printf("Enter marks (0-100): ");

    if (scanf("%d", &marks) != 1)
    {
        printf("Invalid input.\n");
        return 1;
    }

    if (marks < 0 || marks > 100)
    {
        printf("Marks must be between 0 and 100.\n");
    }
    else if (marks >= 90)
    {
        printf("Grade: A\n");
    }
    else if (marks >= 75)
    {
        printf("Grade: B\n");
    }
    else if (marks >= 60)
    {
        printf("Grade: C\n");
    }
    else if (marks >= 40)
    {
        printf("Grade: D\n");
    }
    else
    {
        printf("Grade: F (Fail)\n");
    }

    return 0;
}

Sample input:

Enter marks (0-100): 82

Output:

Grade: B

Explanation

The program checks the marks from highest to lowest grade threshold.

Since 82 is at least 75 but less than 90, the program displays Grade B.

Important: The order of conditions matters. Check higher grade thresholds before lower ones.

6. Nested if Statements

A nested if is an if statement placed inside another if statement.

It is useful when a decision depends on another decision.

Example 4: Login Check

#include <stdio.h>

int main(void)
{
    int usernameCorrect = 1;
    int passwordCorrect = 1;

    if (usernameCorrect)
    {
        if (passwordCorrect)
        {
            printf("Login successful.\n");
        }
        else
        {
            printf("Incorrect password.\n");
        }
    }
    else
    {
        printf("Incorrect username.\n");
    }

    return 0;
}

Output:

Login successful.

Explanation

  • The outer if checks whether the username is correct.

  • The inner if checks whether the password is correct.

  • The success message appears only when both checks pass.

This example demonstrates decision logic only. Real authentication systems must verify credentials securely rather than relying on fixed integer flags.

7. The switch Statement

The switch statement selects a branch based on the value of an integer-like expression, such as an integer or character.

It is useful when you need to select among several fixed options.

Syntax

switch (expression)
{
    case constant1:
        // Statements
        break;

    case constant2:
        // Statements
        break;

    default:
        // No case matched
}

Important keywords

  • switch: Evaluates the controlling expression.

  • case: Specifies a constant value to match.

  • break: Exits the switch statement.

  • default: Handles values that match no case.

Example 5: Day of the Week

#include <stdio.h>

int main(void)
{
    int day;

    printf("Enter a number (1-7): ");

    if (scanf("%d", &day) != 1)
    {
        printf("Invalid input.\n");
        return 1;
    }

    switch (day)
    {
        case 1:
            printf("Monday\n");
            break;

        case 2:
            printf("Tuesday\n");
            break;

        case 3:
            printf("Wednesday\n");
            break;

        case 4:
            printf("Thursday\n");
            break;

        case 5:
            printf("Friday\n");
            break;

        case 6:
            printf("Saturday\n");
            break;

        case 7:
            printf("Sunday\n");
            break;

        default:
            printf("Invalid day number.\n");
    }

    return 0;
}

Sample input:

Enter a number (1-7): 3

Output:

Wednesday

Why is break important?

In a typical switch statement, break prevents execution from continuing into the next case.

If break is omitted, execution may continue into subsequent case labels. This behavior is called fall-through and can be intentional, but it often causes beginner mistakes.

8. Multiple Cases in a switch

You can group multiple cases when they should perform the same action.

Example 6: Vowel or Consonant

#include <stdio.h>

int main(void)
{
    char ch;

    printf("Enter a lowercase English letter: ");

    if (scanf(" %c", &ch) != 1)
    {
        printf("Invalid input.\n");
        return 1;
    }

    switch (ch)
    {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            printf("Vowel\n");
            break;

        default:
            if (ch >= 'a' && ch <= 'z')
            {
                printf("Consonant\n");
            }
            else
            {
                printf("Not a lowercase English letter.\n");
            }
    }

    return 0;
}

Sample input:

Enter a lowercase English letter: e

Output:

Vowel

The leading space in " %c" skips whitespace characters before reading the character.

This example handles lowercase English letters. An extended version could convert uppercase letters to lowercase before checking.

9. Difference Between if-else and switch

Feature

if-else

switch

Conditions

Supports complex expressions

Matches an expression against constant case values

Comparisons

Supports ranges and logical conditions

Does not directly express arbitrary ranges

Data

Can test a wide range of expression types and conditions

The controlling expression must have an appropriate integer or enumeration type

Best use

Ranges and complex decisions

Fixed menu choices and discrete options

Example

Use if-else to check whether marks are above or below a threshold.

Use switch to select an action from menu choices such as 1, 2, or 3.

10. Real-World Project: Simple Calculator

Let's build a calculator using switch.

#include <stdio.h>

int main(void)
{
    double a, b;
    char op;

    printf("Enter an expression (example: 10 + 5): ");

    if (scanf("%lf %c %lf", &a, &op, &b) != 3)
    {
        printf("Invalid input format.\n");
        return 1;
    }

    switch (op)
    {
        case '+':
            printf("Result: %.2f\n", a + b);
            break;

        case '-':
            printf("Result: %.2f\n", a - b);
            break;

        case '*':
            printf("Result: %.2f\n", a * b);
            break;

        case '/':
            if (b != 0.0)
            {
                printf("Result: %.2f\n", a / b);
            }
            else
            {
                printf("Error: Division by zero.\n");
            }
            break;

        default:
            printf("Invalid operator.\n");
    }

    return 0;
}

Sample input:

Enter an expression (example: 10 + 5): 10 + 5

Output:

Result: 15.00

This project combines input validation, conditional logic, arithmetic operators, and switch.

11. Common Beginner Mistakes

  1. Forgetting braces around a multi-statement block.

  2. Using = instead of == when comparing values.

  3. Placing else without a matching if.

  4. Writing else if conditions in the wrong order.

  5. Forgetting break in a switch statement when fall-through is not intended.

  6. Forgetting to handle invalid menu choices.

  7. Checking a condition without validating user input.

  8. Using switch when the decision requires arbitrary ranges or complex Boolean expressions.

12. Practical Exercises

Write programs to complete the following exercises:

  1. Check whether a number is positive, negative, or zero.

  2. Check whether an integer is even or odd.

  3. Find the larger of two numbers using if-else.

  4. Find the largest of three numbers.

  5. Create a grade calculator using an else-if ladder.

  6. Check whether a year is a leap year.

  7. Display a day of the week using switch.

  8. Create a menu-driven calculator using switch.

  9. Check whether a lowercase letter is a vowel or consonant.

  10. Create a simple eligibility checker using nested if statements.

13. Interview Questions

Q1. What is a conditional statement in C?

A statement that controls which block of code executes based on a condition.

Q2. What is the difference between if and if-else?

if executes a block only when its condition is true. if-else provides an alternative block when the condition is false.

Q3. What is an else-if ladder?

A sequence of conditions checked in order until one is true or the final else is reached.

Q4. What is a nested if?

An if statement inside another conditional block.

Q5. Why do we use switch?

To select among multiple branches based on a controlling expression's value.

Q6. What is the purpose of break in a switch statement?

It exits the switch statement and prevents unintended fall-through.

Q7. What is the purpose of default?

It provides a branch for values that do not match any case.

Q8. Can a switch statement directly check a range such as marks >= 90?

No. Use if-else for arbitrary relational conditions and ranges.

14. Quick Revision

  • if executes code when a condition is true.

  • if-else selects between two alternatives.

  • else-if checks multiple conditions in sequence.

  • Nested if statements handle dependent decisions.

  • switch selects a branch based on a matching case value.

  • case labels represent constant values.

  • break exits the switch statement.

  • default handles unmatched values.

  • Validate input before using it in decision-making logic.

15. Homework

Create a Student Result Management Program with these requirements:

  1. Ask the user to enter marks for three subjects.

  2. Validate the input and ensure each mark is between 0 and 100.

  3. Calculate the total and average.

  4. If any subject mark is below 40, declare the student as failed.

  5. Otherwise, assign a grade using the following rules:

    • Average of 90 or above: Grade A

    • Average of 75 to below 90: Grade B

    • Average of 60 to below 75: Grade C

    • Average of 40 to below 60: Grade D

  6. Display the total, average, result, and grade.

Expected output format

Enter marks for three subjects: 85 78 92

Total Marks: 255
Average: 85.00
Result: Pass
Grade: B

Conclusion

In Module 4, you learned how to control program execution using if, if-else, else-if, nested if, and switch. You also practiced using conditions to build a calculator and a student grading system.

←
Post 8 of 15
→