Conditional Statements in C: if and if else in C

Hello, budding programmers! Today, we’re going to delve into the world of conditional statements in C, specifically focusing on if, if-else, if-else ladder, and nested if-else statements. These are the building blocks of decision-making in your code, and mastering them is a huge step towards becoming a proficient C programmer. So, let’s get started!

Understanding Conditional Statements

In C programming, conditional statements are used to make decisions. The program checks a condition, and based on whether it’s true or false, it executes a certain block of code. The most common types of conditional statements in C are the if, else if, if-else ladder, and nested if-else statements.

The if Statement in C

The if statement is the most straightforward conditional statement in C. It checks a condition, and if it evaluates to true, the block of code within the if statement is executed. Here’s the basic syntax:

if (condition)
{
    // Code to be executed if condition is true
}
C

Example

#include <stdio.h>

int main()
{
    int number = 10;

    if (number > 5)
    {
        printf("The number is greater than 5.\n");
    }

    return 0;
}
C

In this example, the if statement checks whether the variable number is greater than 5. Since number is 10, which is indeed greater than 5, the statement within the if block is executed, and “The number is greater than 5.” is printed to the console.

The if-else Statement in C

The if-else statement is a two-way selection statement. It checks a condition, and if it’s true, it executes one block of code. If it’s false, it executes another block of code. Here’s the syntax:

if (condition)
{
    // Code to be executed if condition is true
}
else
{
    // Code to be executed if condition is false
}
C

Example

#include <stdio.h>

int main()
{
    int number = 3;

    if (number > 5)
    {
        printf("The number is greater than 5.\n");
    }
    else
    {
        printf("The number is not greater than 5.\n");
    }

    return 0;
}
C

In this example, the if-else statement checks the same condition as before. However, this time, since number is 3, which is not greater than 5, the statement within the else block is executed, and “The number is not greater than 5.” is printed to the console.

C if-else Ladder

An if-else ladder is a way of implementing multiple conditions in your program. It starts with an if statement, followed by multiple else if statements, and ends with an else statement. Here’s the syntax:

if (condition1)
{
    // Code to be executed if condition1 is true
}
else if (condition2)
{
    // Code to be executed if condition1 is false and condition2 is true
}
else if (condition3)
{
    // Code to be executed if the previous conditions are false and condition3 is true
}
...
else
{
    // Code to be executed if all conditions are false

}
C

Example

#include <stdio.h>

int main()
{
    int number = 15;

    if (number == 10)
    {
        printf("The number is 10.\n");
    }
    else if (number == 15)
    {
        printf("The number is 15.\n");
    }
    else
    {
        printf("The number is not 10 or 15.\n");
    }

    return 0;
}
C

In this example, the if-else ladder checks whether the variable number is 10 or 15. Since number is 15, the statement within the second else if block is executed, and “The number is 15.” is printed to the console.

Nested if-else in C

A nested if-else statement is an if-else statement with another if-else statement as its if block or else block. Here’s the syntax:

if (condition1)
{
    // Code to be executed if condition1 is true

    if (condition2)
    {
        // Code to be executed if condition1 and condition2 are true
    }
    else
    {
        // Code to be executed if condition1 is true and condition2 is false
    }
}
else
{
    // Code to be executed if condition1 is false
}
C

Example

#include <stdio.h>

int main()
{
    int number = 20;

    if (number > 10)
    {
        printf("The number is greater than 10.\n");

        if (number > 15)
        {
            printf("The number is also greater than 15.\n");
        }
        else
        {
            printf("But the number is not greater than 15.\n");
        }
    }
    else
    {
        printf("The number is not greater than 10.\n");
    }

    return 0;
}
C

In this example, the outer if statement checks whether the variable number is greater than 10. Since number is 20, which is greater than 10, the statement within the outer if block is executed, and “The number is greater than 10.” is printed to the console. Then, the inner if statement checks whether number is greater than 15. Since number is 20, which is also greater than 15, the statement within the inner if block is executed, and “The number is also greater than 15.” is printed to the console.

Code Examples

Let’s now look at two complete C programs that use if, if-else, if-else ladder, and nested if-else statements.

Code Example 1

#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;
}
C

This program asks the user to enter a number. It then checks whether the number is even or odd by using the modulus operator % to find the remainder of the number divided by 2. If the remainder is 0, the number is even, and if it’s not, the number is odd. The result is then printed to the console.

Code Example 2

#include <stdio.h

>

int main()
{
    int grade;

    printf("Enter your grade: ");
    scanf("%d", &grade);

    if (grade >= 90)
    {
        printf("You got an A!\n");
    }
    else if (grade >= 80)
    {
        printf("You got a B.\n");
    }
    else if (grade >= 70)
    {
        printf("You got a C.\n");
    }
    else if (grade >= 60)
    {
        printf("You got a D.\n");
    }
    else
    {
        printf("You got an F.\n");
    }

    return 0;
}
C

This program asks the user to enter their grade. It then checks the grade and prints the corresponding letter grade to the console. It uses an if-else ladder to check various conditions.

Wrapping Up

Conditional statements in C, such as if, if-else, if-else ladder, and nested if-else, are crucial for controlling the flow of your program. They allow your code to make decisions and execute different blocks of code based on certain conditions. Mastering these concepts is a key step in your journey to becoming a proficient C programmer.

Frequently Asked Questions

  1. What are the 4 conditional statements in C?

    The four conditional statements in C are if, if-else, nested if, and switch-case.

  2. What is the difference between if and if-else conditional statements in C?

    The if statement executes a block of code if a condition is true. The if-else statement, on the other hand, executes one block of code if a condition is true and another block of code if the condition is false.

  3. What are conditional statements in C?

    Conditional statements in C are used to perform different actions based on different conditions. They control the flow of the program.

  4. Can you have 3 conditions in an if statement in C?

    Yes, you can have multiple conditions in an if statement in C by using logical operators like && (and) and || (or).

  1. Control Statements Overview
  2. if and if-else in C
  3. switch in C
  4. For loop in C
  5. while Loop in C
  6. do-while Loop in C
  7. break, continue, goto in C
Scroll to Top