View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

All About Conditional Statements in C

Updated on 15/05/20252,998 Views

Debugging a C program without proper control flow is like tracing a straight wire through a maze. You know where it starts but not where it could have branched. Conditional statements in C help eliminate such guesswork. They guide the compiler to follow specific paths based on conditions, making logic visible and traceable during debugging. Without them, pinpointing logical flaws becomes tedious and inefficient. 

Explore our Online Software Development Courses from top universities.

As code complexity grows, the ability to control program flow becomes critical. These condition-based statements not only simplify decision-making but also make the program more readable and maintainable. They help isolate problems early and assist in testing different logical branches efficiently. Whether you're dealing with menu options or validating user input, conditional statements in C make debugging manageable and the logic crystal clear.

What Are Conditional Statements in C?

Writing error-free code is only half the challenge when developing in C. The real skill lies in making decisions based on dynamic conditions. Conditional statements in C help your program execute specific blocks of code only when certain criteria are met. This is vital in building logic that adapts to user input, environmental changes, or runtime data.

Whether you're validating a password, comparing scores, or navigating menu options, these conditional paths control the program's direction. Without them, every program would run in a straight line—rigid and impractical for real-world tasks.

Boost your career journey with the following online courses: 

Types of Conditional Statements in C

Below are the core types of Conditional Statements in C. Each includes syntax, a practical example, the output, and an explanation.

if statement

The if statement checks whether a given condition is true. If it is, the code block inside the if statement runs.

Syntax:

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

Example:

#include <stdio.h>
int main() {
    int marks = 75;
    if (marks > 50) {
        printf("Ram passed the exam.\n");
    }
    return 0;
}

Output:

Ram passed the exam.

Explanation: The condition marks > 50 evaluates to true, so the message is printed. If the condition were false, nothing would happen.

if-else statement

The if-else statement lets you handle both true and false outcomes of a condition.

Syntax:

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

Example:

#include <stdio.h>

int main() {
    int age = 16;
    if (age >= 18) {
        printf("Aniket is eligible to vote.\n");
    } else {
        printf("Aniket is not eligible to vote.\n");
    }
    return 0;
}

Output:

Aniket is not eligible to vote.

Explanation: Since age >= 18 is false, the program jumps to the else block.

Nested if-else statement

You can nest one if-else inside another to check multiple conditions in a hierarchy.

Syntax:

if (condition1) {
    if (condition2) {
        // code if both are true
    } else {
        // code if only condition1 is true
    }
} else {
    // code if condition1 is false
}

Example:

#include <stdio.h>
int main() {
    int marks = 85;
    if (marks >= 60) {
        if (marks >= 80) {
            printf("Pooja got distinction.\n");
        } else {
            printf("Pooja passed with first class.\n");
        }
    } else {
        printf("Pooja did not pass.\n");
    }
    return 0;
}

Output:

Pooja got distinction.

Explanation: Both conditions marks >= 60 and marks >= 80 are true, so the program executes the nested block.

Learn how nested if-else statements enhance the control flow by exploring their structure and use.

if-else-if ladder

This is useful when you want to evaluate multiple conditions sequentially.

Syntax:

if (condition1) {
    // code
} else if (condition2) {
    // code
} else {
    // default code
}

Example:

#include <stdio.h>

int main() {
    int score = 70;
    if (score >= 90) {
        printf("Prasun got Grade A.\n");
    } else if (score >= 75) {
        printf("Prasun got Grade B.\n");
    } else if (score >= 60) {
        printf("Prasun got Grade C.\n");
    } else {
        printf("Prasun failed.\n");
    }
    return 0;
}

Output:

Prasun got Grade C.

Explanation: Only the score >= 60 condition is true, so the corresponding block runs. The ladder skips the rest once a condition is satisfied.

Switch statement

switch is used for handling multiple discrete cases efficiently.

Syntax:

switch (expression) {
    case value1:
        // code
        break;
    case value2:
        // code
        break;
    default:
        // code if no match
}

Example:

#include <stdio.h>

int main() {
    int day = 3;
    switch (day) {
        case 1: printf("Monday\n"); break;
        case 2: printf("Tuesday\n"); break;
        case 3: printf("Wednesday\n"); break;
        default: printf("Invalid day\n");
    }
    return 0;
}

Output:

Wednesday

Explanation: Since day is 3, the program jumps to case 3 and prints the correct day.

Conditional (ternary) operator

The ternary operator in C provides a shorthand for if-else expressions, making code more concise. This is a compact version of if-else, written in a single line.

Syntax:

condition ? expression_if_true : expression_if_false;

Example:

#include <stdio.h>

int main() {
    int age = 20;
    age >= 18 ? printf("Shyam is an adult.\n") : printf("Shyam is a minor.\n");
    return 0;
}

Output:

Shyam is an adult.

Explanation: The condition evaluates to true, so the expression before the colon is executed.

Can You Use Logical Operators with Conditional Statements in C?

Yes, logical operators play a crucial role in building compound conditions inside if, else-if, or while statements. You can combine multiple conditions using && (AND), || (OR), and ! (NOT) to make decisions more precise and reliable.

These logical operators improve the clarity and flexibility of conditional logic, especially when checking multiple criteria in one line.

Example Using AND, OR, and NOT Operators in C

Example:

#include <stdio.h>

int main() {
    int age = 25;
    int salary = 30000;
    
    if (age >= 21 && salary >= 25000) {
        printf("Ankita is eligible for the loan.\n");
    } else {
        printf("Ankita is not eligible for the loan.\n");
    }
    
    if (!(age < 18)) {
        printf("Ankita is not a minor.\n");
    }

    return 0;
}

Output:

Ankita is eligible for the loan.

Ankita is not a minor.

Explanation The first condition uses AND to check both age and salary. The second uses NOT to invert the logic. Logical operators make conditions more expressive.

What Are Common Mistakes with Conditional Statements in C?

Several common mistakes can lead to unexpected outcomes:

  • Using = instead of == in conditions (assignment vs comparison)
  • Forgetting to enclose blocks in {} when multiple lines are intended
  • Not using break in switch cases, causing fall-through
  • Overusing nested if statements, which reduce readability
  • Misunderstanding operator precedence in compound expressions

Avoiding these mistakes makes your code more robust and maintainable. Control the flow of loops and conditionals with break and continue statements in C to refine your decision-making logic.

Example Programs of Conditional Statements in C

Below are three real-world examples categorized by difficulty level. Each one is complete with input/output and explanation.

Beginner-Level Example Using If-Else in C

Example:

#include <stdio.h>

int main() {
    int number;
    printf("Enter a number: ");
    scanf("%d", &number);

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

    return 0;
}

Output (Sample):

Enter a number: 4

The number is even.

Explanation: The modulus operator checks if the number is divisible by 2. Based on the result, it prints whether it's even or odd.

Intermediate-Level Example Using Nested If in C

Example:

#include <stdio.h>

int main() {
    int age = 30;
    int experience = 6;

    if (age > 25) {
        if (experience > 5) {
            printf("Rohan is eligible for the senior role.\n");
        } else {
            printf("Rohan needs more experience.\n");
        }
    } else {
        printf("Rohan does not meet the age criteria.\n");
    }

    return 0;
}

Output:

Rohan is eligible for the senior role.

Explanation: This example uses nested if conditions to check both age and experience. Only if both are valid does the final condition pass.

Advanced Example Using Switch Case in C

Example:

#include <stdio.h>

int main() {
    int option;
    printf("Choose a number (1-3): ");
    scanf("%d", &option);

    switch (option) {
        case 1: printf("You selected Breakfast.\n"); break;
        case 2: printf("You selected Lunch.\n"); break;
        case 3: printf("You selected Dinner.\n"); break;
        default: printf("Invalid choice.\n");
    }

    return 0;
}

Output :

Choose a number (1-3): 2

You selected Lunch.

Explanation: The switch statement evaluates option and matches it with a specific case. If no match is found, the default message appears.

Conclusion

Mastering Conditional Statements in C is essential for writing logical and interactive programs. From simple if checks to complex nested decisions and switch cases, these statements form the control flow backbone of any C application.

Once you understand how to use each type effectively—along with logical operators—you’ll be able to solve real-world programming challenges with precision and clarity.

FAQs

1. How Does the If Statement Work in C?

The if statement in C checks whether a given condition is true. If it is, the associated block executes. If the condition is false, the block is skipped and the program continues with the next statement.

2. What Is the If-Else Statement in C?

An if-else statement allows two paths: one executes when the condition is true, the other when it's false. It helps implement binary decisions and is one of the most widely used conditional statements in C.

3. How to Use Nested If-Else in C?

Nested if-else lets you place one conditional block inside another. It checks multiple related conditions in a hierarchy, helping implement more complex decision logic by narrowing down choices step-by-step.

4. Example of Nested If-Else Statement in C

The example of a Nested If-Else statement is as follows :-

int marks = 85;  
if (marks >= 60) {  
   if (marks >= 80) printf("Distinction");  
   else printf("First Class");  
}  
else printf("Fail");  

Output: Distinction

Explanation: Both conditions are evaluated in sequence. Since 85 ≥ 80, the innermost block runs.

5. What Is the If-Else-If Ladder in C?

The if-else-if ladder handles multiple conditions in order. It evaluates each condition from top to bottom until one is true, then executes the matching block and skips the rest.

6. How Does the Switch Case Work in C?

The switch statement evaluates an expression and matches its value to a case label. Once matched, the corresponding block runs. If no match is found, the default block executes, if present.

7. Difference Between Switch and If-Else in C

Switch is used for fixed, discrete values, while if-else handles complex logical conditions. Switch is cleaner for multiple constant comparisons, whereas if-else supports ranges and compound conditions.

8. Can We Use Logical Operators in Conditional Statements in C?

Yes, logical operators like &&, ||, and ! are commonly used inside conditions to combine or negate expressions, allowing better control and more expressive logic in if, while, or for blocks.

9. What Is the Use of Ternary Operator in C?

The ternary operator ?: is a shorthand if-else. It evaluates a condition and returns one of two values. It's mostly used for short, inline decisions within expressions or assignments.

10. Can Conditional Statements Be Used Inside Loops in C?

Yes, conditional statements can be placed inside for, while, or do-while loops to control logic on each iteration. This allows you to filter, skip, or exit based on specific runtime conditions.

11. What Happens If You Forget the Else Block in C?

If you skip the else block, only the if condition is checked. If it fails, nothing happens unless other code follows. The program continues normally unless further logic depends on that branch.

12. Can We Nest Switch Statements in C?

Yes, you can nest one switch inside another. However, it’s crucial to manage break statements properly and ensure readability, as deeply nested switch blocks can become hard to trace or debug.

13. Are Braces Mandatory in If Statements in C?

No, braces {} are optional if only one statement follows the if. But omitting them is discouraged, as adding more statements later without braces can introduce logical bugs and readability issues.

14. What Is a Common Mistake in If-Else Usage in C?

A common mistake is misplacing semicolons or missing braces, leading to code that looks correct but doesn't behave as expected. Also, incorrect condition grouping often causes unexpected branching.

15. Can You Replace Switch with If-Else Completely in C?

Yes, technically all switch logic can be written using if-else. However, switch offers better clarity and performance when dealing with many constant values or menu-style choices.

image

Take a Free C Programming Quiz

Answer quick questions and assess your C programming knowledge

right-top-arrow
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

Start Learning For Free

Explore Our Free Software Tutorials and Elevate your Career.

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918068792934

Disclaimer

1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.

2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.