If-else Statement in C

By | February 20, 2023

Prerequisite – Control Statements in C
In C, the “if-else” statement is used to execute one block of code if a certain condition is true, and a different block of code if the condition is false.

Syntax:
The basic syntax of the “if-else” statement in C is:

if (condition) {
  // Code to execute if condition is true
} else {
  // Code to execute if condition is false
}

Here, “condition” is any expression that evaluates to a boolean value (true or false). If the condition is true, the code inside the first set of braces will be executed; if the condition is false, the code inside the second set of braces will be executed.

You can have as many “else if” blocks as you need in order to check multiple conditions. The basic syntax of the “if-else if-else” statement in C is:

if (condition1) {
  // Code to execute if condition1 is true
} else if (condition2) {
  // Code to execute if condition2 is true
} else if (condition3) {
  // Code to execute if condition3 is true
} else {
  // Code to execute if all conditions are false
}

Here, the code inside the first set of braces will be executed if condition1 is true; if not, the code inside the second set of braces will be executed if condition2 is true; if not, the code inside the third set of braces will be executed if condition3 is true; if none of the conditions are true, the code inside the last set of braces will be executed.

Examples:
Here are some examples of how the “if-else” statement can be used in C:

Example-1:

#include <stdio.h>

int main() {
   int num = 10;
   
   if (num % 2 == 0) {
      printf("%d is even.\n", num);
   } else {
      printf("%d is odd.\n", num);
   }
   
   return 0;
}

In this example, the “if-else” statement is used to check whether the value of the variable “num” is even or odd. Since 10 is even, the condition “num % 2 == 0” is true and the code inside the first set of braces is executed, which prints “10 is even.” to the console.

Example-2:

#include <stdio.h>

int main() {
   int age;
   
   printf("Enter your age: ");
   scanf("%d", &age);
   
   if (age >= 18) {
      printf("You are eligible to vote.\n");
   } else {
      printf("You are not eligible to vote.\n");
      printf("You must be at least 18 years old.\n");
   }
   
   return 0;
}

In this example, the “if-else” statement is used to check whether the user’s age is greater than or equal to 18. If it is, the code inside the first set of braces is executed, which prints “You are eligible to vote.” to the console. If the age is less than 18, the code inside the second set of braces is executed, which prints “You are not eligible to vote.” and “You must be at least 18 years old.” to the console.

Example-3:

#include <stdio.h>

int main() {
   int num1, num2;
   
   printf("Enter two numbers: ");
   scanf("%d %d", &num1, &num2);
   
   if (num1 > num2) {
      printf("%d is greater than %d.\n", num1, num2);
   } else if (num1 < num2) {
      printf("%d is less than %d.\n", num1, num2);
   } else {
      printf("%d and %d are equal.\n", num1, num2);
   }
   
   return 0;
}

In this example, the “if-else if-else” statement is used to compare two numbers entered by the user. If the first number is greater than the second number, the code inside the first set of braces is executed, which prints “%d is greater than %d.” to the console. If the first number is less than the second number, the code inside the second set of braces is executed, which prints “%d is less than %d.” to the console. If the two numbers are equal, the code inside the third set of braces is executed, which prints “%d and %d are equal.” to the console.

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.