if-else Statements in C: Master the C Control Flow

Table of Contents

if Statement in C

The if statement in C is a decision-making statement that executes a block of code only when a given condition is true. The code inside the if block is only executed if the condition evaluates to true; otherwise, it is skipped.

Syntax of the if Statement

 
if (condition) {
    // statements executed if condition is true
}

Write a program that accepts a candidate’s age as an integer input. Verify that the candidate is at least 18 years old. If the condition is met, print the message: “Candidate eligible for voting.

				
					#include <stdio.h>

int main() {
    int age = 21;

    if (age > 18) {
        printf("Candidate eligible for voting\n");
    }
    return 0;
}

// Candidate eligible for voting

				
			

In the example above, we use the variable age to test whether age is greater than 18 (using the > operator). As age is 21, and we know that 21 is greater than 18, we print to the screen that “Candidate eligible for voting”.

 

If-else Statement in C

The else statement is used along with the if statement.  It executes a block of code when the condition in the if statement is false.

In real life we make decisions every day. If it rains take an umbrella. Otherwise, wear sunglasses. Similarly, in C programming, the if-else statement allows a program to make decisions based on conditions.

Syntax:

 
if (condition) {
     statements executed if condition is true
} else {
     statements executed if condition is false
}

Key Points:

  • The condition must evaluate to either true (1) or false (0)
  • Curly braces {} group multiple statements
  • Indentation improves readability (very important for clean code)
Flowchart diagram showing the logic structure of an If-Else statement in C programming.
A visual representation of how an If-Else statement splits program execution based on a true or false condition.
				
					#include <stdio.h>

int main() {
    int x = 9;
    if (x % 2 == 0) {
        printf("Even number\n");
    } else {
        printf("Odd number\n");
    }
    return 0;
}

// Odd number
				
			

In the example above, the if condition is tested and found to be false (since 9 is not divisible by 2), so the program skips the code inside the if block and executes the code inside the else block to print “Odd number”.

 

Nested if-else in C

A nested if-else is an if-else statement placed inside another if or else block, allowing for hierarchical condition checking where a condition is tested only if a previous condition is met.

Syntax:

 
if (condition1) {
    if (condition2) {
statements executed if condition1 and condition2 are true } else {
statements executed if condition1 is true and condition2 is false } } else {
statements executed if condition1 is false }

Write a C program that checks if a given integer is positive. If it is positive, further check if it is even or odd and print the result; otherwise, indicate that the number is not positive.

				
					#include <stdio.h>

int main() {
    int num = 7;
    if (num > 0) {
        if (num % 2 == 0) {
            printf("Positive Even Number\n");
        } else {
            printf("Positive Odd Number\n");
        }
    } else {
        printf("Number is not positive\n");
    }

    return 0;
}

// Positive Odd Number
				
			

In the example above, we first check the outer condition to see if num is positive; since 7 is greater than 0, we proceed inside to the nested check where we find that 7 is not divisible by 2, so we print “Positive Odd Number”.

 

Else-If Statements

The else-if statement allows you to check multiple conditions in sequence; if the previous if condition is false, the program checks the next else-if condition, executing only the code block associated with the first true condition. The else if ladder is cleaner and more readable than deep nesting.

 
if (condition1) {
    // statements
} else if (condition2) {
    // statements 
} else if (condition3) {
    // statements 
} else {
       statements executed if all above conditions are false
}
Flowchart diagram showing an Else-If ladder in C programming where multiple conditions are checked in sequence.
The Else-If Ladder: A step-by-step visualization of how a program checks multiple conditions one after another until it finds a match.

Write a C program that accepts a temperature value (in Celsius) and categorizes it: print “Hot” if above 30, “Warm” if above 20, “Cool” if above 10, and “Cold” otherwise.

				
					#include <stdio.h>

int main() {
    int temp = 22; 

    if (temp > 30) {
        printf("It is Hot.\n");
    } else if (temp > 20) {
        printf("It is Warm.\n");
    } else if (temp > 10) {
        printf("It is Cool.\n");
    } else {
        printf("It is Cold.\n");
    }

    return 0;
}

// It is Warm.
				
			

In this example, we check the variable temp against descending thresholds; since temp is 22 (which is not > 30 but is > 20 the program executes the second block and prints “It is Warm.

Why Use else if?

  • Easy to read
  • Efficient execution
  • Cleaner logic

 

Logical Operators Used in if-else

There are mainly three types of logical operators used in C. They are AND, OR, NOT

1. AND (&&)

Use AND (&&) when both conditions must be true.

				
					#include <stdio.h>

int main() {
    int age = 25;
    int hasID = 1; // 1 represents True

    if (age >= 18 && hasID == 1) {
        printf("Allowed to enter\n");
    } else {
        printf("Access denied\n");
    }
    return 0;
}

// Allowed to enter
				
			

Explanation: In this example, we check if age is at least 18 AND if hasID is true; since both conditions are met, the program prints “Allowed to enter”.

 

OR (||)

Use the OR logical operator (represented by ||) when a block of code should execute if any of the specified conditions is true.

				
					#include <stdio.h>

int main() {
    int marks = 45;

    // Check if marks are very low OR very high
    if (marks < 30 || marks > 90) {
        printf("Score is extreme\n");
    } else {
        printf("Score is average\n");
    }
    return 0;
}

// Score is average
				
			

Explanation: In this example, we check if marks is less than 30 OR greater than 90; since 45 is neither, the condition is false and it prints “Score is average”.

 

NOT (!)

The NOT operator (!) reverses a condition:

  • If a condition is true, ! makes it false.
  • If a condition is false, ! makes it true.

This is useful when you want to check that something is not the case:

				
					#include <stdio.h>

int main() {
    int isRaining = 0; // 0 represents False

    if (!isRaining) {
        printf("Go outside\n");
    } else {
        printf("Stay inside\n");
    }
    return 0;
}

// Go outside
				
			

Explanation: In this example, we invert the value of isRaining using the NOT operator since isRaining is 0 (False), the condition becomes True, and it prints “Go outside”.

 

Applications

Decision Making:Used to make decisions in a program by choosing one action out of two or more based on a condition.
For example, it can check whether a number is positive, negative or zero and execute the appropriate code.

2. Grading System: Used in student evaluation systems to assign grades based on marks obtained. Different conditions are checked to decide whether a student gets Grade A, B, C or Fail.

3. Login Authentication: Commonly used in login systems to compare entered usernames and passwords with stored values. If the credentials match, access is granted; otherwise an error message is displayed.

4. Even or Odd Checking: Helps determine whether a given number is even or odd by checking the remainder when divided by 2. This logic is widely used in mathematical and logical programs.

5. Menu-Driven Programs: Used in menu-driven applications to perform different operations based on the user’s choice.
For example, in a calculator program, it selects addition, subtraction, multiplication or division.

 

Frequently Asked Questions (FAQs) 

1) What is an if-else in C?

if-else in C is a decision-making statement that executes one block of code when a condition is true and another block when the condition is false.

 

2) What is a nested if-else statement in C?

A nested if-else statement is an if-else statement placed inside another if-else to check multiple conditions step by step.

 

3) What is an if-else ladder statement?

An if-else ladder is used to test multiple conditions in sequence. The first condition that evaluates to true gets executed, and the remaining conditions are skipped.

 

4) Can we use else without if in C?

No, the else statement cannot be used without an if statement. It must always be associated with an if.

 

5) Which is better: nested if-else or if-else ladder?

The if-else ladder is generally better because it improves readability and avoids deep nesting, making the code easier to understand and maintain.