-->

C Programming Day 4 – Conditional Statements with Examples Beginner to Advanced

C Programming Day 4



Conditional Statements (if, if-else, Nested if, switch)


    Introduction

    Programming becomes truly powerful when a program can make decisions. Imagine an ATM machine that checks your PIN before allowing transactions, or a website that verifies your login credentials. These are examples of conditional statements.

    Conditional statements allow a program to execute different blocks of code depending on whether a condition is true or false.

    In this lesson, you'll learn every important decision-making statement in C with diagrams, syntax, examples, interview questions, and practical programs.

    By the end of this lesson, you will understand:

    1. What are Conditional Statements?
    2. Why Decision Making is Important
    3. Boolean Expressions
    4. if Statement
    5. if-else Statement
    6. Nested if Statement
    7. else-if Ladder
    8. switch Statement
    9. Difference Between if and switch
    10. Real-world Programs
    11. Common Mistakes
    12. Best Practices
    13. Interview Questions
    14. Practice Problems
    15. Summary


    What are Conditional Statements?

    Conditional statements help a program choose between multiple options based on a condition.

    Think of it like real life:

    • If it rains → Take an umbrella.
    • Otherwise → Wear sunglasses.

    Programming follows the same logic.

    Condition

    TRUE
     ↓
    Execute Code A

    FALSE
     ↓
    Execute Code B


    Why Decision Making is Important

    Decision making is used everywhere.

    Examples:

    • ATM PIN verification
    • Login system
    • Online shopping discounts
    • Student grade calculation
    • Voting eligibility
    • Electricity bill calculation
    • Salary bonus
    • Bank transactions

    Without conditional statements, programs cannot react intelligently.

    Comparison Operators

    Operator            Meaning

    ==                       Equal
    !=                    Not Equal
    >                 Greater Than
    <                    Less Than
    >=             Greater Than or Equal
    <=               Less Than or Equal

    Example

    int age = 20;
    if(age >= 18)
    {
        printf("Adult");
    }


    Logical Operators

    Operator    Meaning

    &&                       AND
    !                      NOT

    Example

    if(age>=18 && citizen==1)


    if Statement

    The if statement executes code only when the condition is true.

    Syntax

    if(condition)
    {
        statements;
    }

    Flowchart

    Start

    Condition?

    True → Execute Statement

    End

    False

    End

    Example 1

    #include <stdio.h>
    int main()
    {
        int age = 20;
        if(age >= 18)
        {
            printf("You are eligible to vote.");
        }
        return 0;
    }

    Output

    You are eligible to vote.

    Example 2

    #include <stdio.h>

    int main()
    {
        int number;
        printf("Enter number: ");
        scanf("%d",&number);
        if(number>0)
        {
            printf("Positive Number");
        }
        return 0;
    }


    if-else Statement

    When one condition has two possible outcomes.

    Syntax

    if(condition)
    {
        statements;
    }
    else
    {
        statements;
    }

    Flowchart

    Start

    Condition?

    True → Block A

    False → Block B

    End

    Example

    #include <stdio.h>

    int main()
    {
        int age;
        printf("Enter Age: ");
        scanf("%d",&age);
        if(age>=18)
        {
            printf("Adult");
        }
        else
        {
            printf("Minor");
        }
        return 0;
    }

    Output

    Enter Age: 16
    Minor


    Nested if Statement

    An if statement inside another if statement is called a Nested if.

    Useful when multiple conditions depend on each other.

    Syntax

    if(condition1)
    {
        if(condition2)
        {
            statements;
        }
    }

    Example

    #include <stdio.h>

    int main()
    {
        int age = 25;
        int citizen = 1;
        if(age>=18)
        {
            if(citizen==1)
            {
                printf("Eligible to Vote");
            }
        }
        return 0;
    }

    Real-Life Example

    Is Age >=18?

    YES

    Is Citizen?

    YES

    Allow Voting


    else-if Ladder

    Used when there are multiple conditions.

    Syntax

    if(condition1)
    {

    }
    else if(condition2)
    {

    }
    else if(condition3)
    {

    }
    else
    {

    }

    Grade Calculator

    #include <stdio.h>

    int main()
    {
        int marks;

        printf("Enter Marks: ");
        scanf("%d",&marks);

        if(marks>=80)
               printf("Grade A+");

        else if(marks>=70)
               printf("Grade A");

        else if(marks>=60)
               printf("Grade A-");

        else if(marks>=50)
               printf("Grade B");

        else
              printf("Fail");

        return 0;
    }


    switch Statement

    The switch statement selects one option from many possible choices.

    Best used for menus.

    Syntax

    switch(expression)
    {
    case value1:
        statements;
        break;

    case value2:
        statements;
        break;

    default:
        statements;
    }

    Example

    #include <stdio.h>

    int main()
    {
        int day;
        printf("Enter Day Number: ");
        scanf("%d",&day);

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

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

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

            default:
                printf("Invalid Day");
        }
        return 0;
    }

    Without break

    switch(choice)
    {
    case 1:
        printf("One");

    case 2:
        printf("Two");

    case 3:
        printf("Three");
    }

    Output

    One
    Two
    Three

    This is called fall-through.


    Difference Between if and switch

        if Statement                                         switch Statement

    Works with ranges                          Works with fixed values
    Supports logical operators                   No logical operators
    Can compare complex conditions       Compares one expression
    Slightly slower in some cases             Often faster for many discrete options
    Best for decision making                    Best for menu systems


    Real-World Decision-Making Programs

    Program 1 – Even or Odd

    #include <stdio.h>

    int main()
    {
        int n;

        scanf("%d",&n);

        if(n%2==0)
            printf("Even");
        else
            printf("Odd");

        return 0;
    }

    Program 2 – Largest Number

    #include <stdio.h>

    int main()
    {
        int a,b;

        scanf("%d%d",&a,&b);

        if(a>b)
            printf("%d",a);
        else
            printf("%d",b);

        return 0;
    }

    Program 3 – Leap Year

    #include <stdio.h>

    int main()
    {
        int year;

        scanf("%d",&year);

        if((year%400==0)||((year%4==0)&&(year%100!=0)))
            printf("Leap Year");
        else
            printf("Not Leap Year");

        return 0;
    }

    Program 4 – Login System

    #include <stdio.h>

    int main()
    {
        int password=1234;
        int input;

        scanf("%d",&input);

        if(input==password)
            printf("Login Successful");
        else
            printf("Wrong Password");

        return 0;
    }

    Program 5 – ATM Withdrawal

    #include <stdio.h>

    int main()
    {
        int balance=5000;
        int amount;

        scanf("%d",&amount);

        if(amount<=balance)
            printf("Transaction Successful");
        else
            printf("Insufficient Balance");

        return 0;
    }

    Program 6 – Calculator Using switch

    #include <stdio.h>

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

        scanf("%d %c %d",&a,&op,&b);

        switch(op)
        {
            case '+':
                printf("%d",a+b);
                break;

            case '-':
                printf("%d",a-b);
                break;

            case '*':
                printf("%d",a*b);
                break;

            case '/':
                if(b!=0)
                    printf("%d",a/b);
                else
                    printf("Division by Zero");
                break;

            default:
                printf("Invalid Operator");
        }
        return 0;
    }


    Common Mistakes

    ❌ Using = instead of = =

    Wrong

    if(a=5)

    Correct

    if(a==5)

    ❌ Forgetting break in switch

    case 1:
    printf("One");

    Always use

    break;

    unless you intentionally want fall-through.


    Missing braces for multiple statements

    Wrong

    if(age>18)
    printf("Adult");
    printf("Welcome");

    Correct

    if(age>18)
    {
        printf("Adult");
        printf("Welcome");
    }


    Best Practices

    • Use meaningful variable names.
    • Keep conditions simple and readable.
    • Prefer switch for menu-driven programs.
    • Use else-if instead of multiple separate if statements when conditions are mutually exclusive.
    • Indent code consistently.
    • Test edge cases (e.g., boundary values).


    Interview Questions

    1. What is a conditional statement?

    A statement that executes different code based on a condition.

    2. Difference between if and if-else?

    if executes code only when the condition is true, while if-else provides an alternative block when the condition is false.

    3. What is Nested if?

    An if statement placed inside another if statement.

    4. Why use switch?

    To simplify selection among multiple fixed choices.

    5. What is fall-through?

    When execution continues into the next case because break is omitted.


    Practice Problems

    1. Check whether a number is positive, negative, or zero.
    2. Find the largest among three numbers.
    3. Calculate student grades using an else-if ladder.
    4. Build a menu-driven calculator using switch.
    5. Determine whether a year is a leap year.
    6. Check voting eligibility based on age and citizenship.
    7. Verify login credentials with a username and password.
    8. Create an ATM withdrawal simulation with balance checking.


    Summary

    In Day 4, you learned how to control the flow of a C program using conditional statements. You explored the if, if-else, nested if, else-if ladder, and switch statement, along with comparison and logical operators. Through real-world examples such as login systems, ATM transactions, calculators, and grade evaluators, you saw how decision-making forms the foundation of interactive and intelligent applications.

    In the next lesson (Day 5), you'll learn Loops in C (for, while, and do-while) to execute repetitive tasks efficiently.


    Frequently Asked Questions (FAQ)

    What is a conditional statement in C?
    A conditional statement allows a program to make decisions and execute different code blocks based on whether a condition evaluates to true or false.
    When should I use if instead of switch?
    Use if for complex conditions involving ranges or logical operators. Use switch when comparing a single expression against multiple constant values.
    Can I use strings with a switch statement in C?
    No. Standard C switch statements work with integral types such as int and char, not strings.
    What is a nested if statement?
    A nested if is an if statement placed inside another if statement to evaluate dependent conditions.
    Why is the break statement important in switch?
    break prevents execution from continuing into the next case. Omitting it causes fall-through behavior.

    0/Post a Comment/Comments

    Thank You For Visit My Website.
    I Will Contact As Soon As Possible.