Conditional Statement (If-Else) in C

Definition:

The if-else statement is a decision-making statement in the C programming language. It allows a program to execute one block of code when the condition is true and another block when the condition is false.

Syntax:

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

Explanation:

The condition inside the if statement is evaluated first. If the condition is true, the statements inside the if block are executed. Otherwise, the statements inside the else block are executed.


Program: Check Positive or Negative Number

Explanation: This program accepts a number from the user. If the number is greater than zero, it displays Positive Number; otherwise, it displays Negative Number.


#include <stdio.h>
#include <conio.h>

main()
{

    int Number;

    clrscr();

    printf("Enter the Number : ");

    scanf("%d",&Number);

    if(Number > 0)
    {

        printf("\nPositive Number");

    }

    else
    {

        printf("\nNegative Number");

    }

    getch();

}

Output:

Enter the Number : 25

Positive Number


Enter the Number : -15

Negative Number

Practice Exercises

Task 1: Write a C program to check whether the entered number is greater than 10 or less than or equal to 10 using the if-else statement.

Goal: Learn how to compare a number using the if-else statement.
💡 Hint: Compare the entered number with 10.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

main()
{
    int Number;

    clrscr();

    printf("Enter the Number : ");
    scanf("%d",&Number);

    if(Number > 10)
    {
        printf("\nNumber is Greater than 10");
    }
    else
    {
        printf("\nNumber is Less than or Equal to 10");
    }

    getch();
}

Output
Enter the Number : 15

Number is Greater than 10
Explanation: The program accepts a number from the user and compares it with 10. If the entered number is greater than 10, it displays "Number is Greater than 10"; otherwise, it displays "Number is Less than or Equal to 10".