WhatsApp us

If else Statement in C Programming

  • Tech Area
  • Last updated on: January 8, 2026



In this tutorial, We will see how to use if and if else statement in C programming language.

C if Statement

The syntax of the if statement in C programming is:

if (test expression) 
{
   // code
}

How if statement works?

The if statement evaluates the test expression inside the parenthesis ().

If the test expression is evaluated to true, statements inside the body of if are executed.

If the test expression is evaluated to false, statements inside the body of if are not executed.

if statement

// Program to display a number if it is negative

#include<stdio.h>
#include<conio.h>
void main()
{
    int number;
    clrscr();
    printf("Enter an integer: ");
    scanf("%d", &number);

    // true if number is less than 0
    if (number < 0) {
        printf("You entered %d.\n", number);
    }

    printf("The if statement example.");
    getch();
}

Output 1

Enter an integer: -1
You entered -1.
The if statement example.

When the user enters -1, the test expression number<0 is evaluated to true. Hence, You entered –1 is displayed on the screen.

Output 2

Enter an integer: 2
The if statement example.

When the user enters 2, the test expression number<0 is evaluated to false and the statement inside the body of if is not executed

C if….else Statement

The if statement may have an optional else block. The syntax of the if..else statement is:

if (test expression) {
    // run code if test expression is true
}
else {
    // run code if test expression is false
}

How if…else statement works?

If the test expression is evaluated to true,

statements inside the body of if are executed.

statements inside the body of else are skipped from execution.

If the test expression is evaluated to false,

statements inside the body of else are executed.

statements inside the body of if are skipped from execution.

if….else statement

#include<stdio.h>
#include<conio.h>
void main()
{
    int age;
    clrscr();
    printf("Enter your age = ");
    scanf("%d", &age);

    // true if age is greater than and equal to 18
    if (age>=18) {
        printf("You can vote.");
    }
    else {
        printf("You can not vote.");
    }
    getch();
}

Output

Enter your age = 12
You can not vote.

When the user enters 12, the test expression age>=18 is evaluated to false. Hence, the statement inside the body of else is executed.


Subscribe us via Email

Join 20,000+ subscriber

Subscribe on YouTube

PHP Projects
Matrimonial Portal Project in PHP & MySQL Last Updated: December 22, 2025
Event Management System Project in PHP & MySQL Last Updated: December 6, 2025
Online Shopping System Project in PHP MySQL Last Updated: November 26, 2025
Hostel management system project in PHP and MySQL Last Updated: February 14, 2024
Online Pizza Delivery project in PHP Last Updated: February 4, 2024
Parking Management System project in PHP Last Updated: November 5, 2023
Visitors Management System project in PHP Last Updated: August 28, 2023
SNIPE – IT Asset management system v6.1.0 Last Updated: April 21, 2023
Employee Management System project in PHP Last Updated: January 21, 2023