If Statement in C

By | February 20, 2023

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

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

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

Here, “condition” is any expression that evaluates to a boolean value (true or false). If the condition is true, the code inside the curly braces will be executed; if the condition is false, the code inside the braces will be skipped and the program will continue executing the code after the braces.

You can also use the “else” keyword to specify an alternative block of code to execute if the condition is false:

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

Here, the code inside the first set of braces will be executed if the condition is true, and the code inside the second set of braces will be executed if the condition is false.

You can also use the “else if” keyword to check multiple conditions:

if (condition1) {
  // Code to execute if condition1 is true
} else if (condition2) {
  // Code to execute if condition2 is true
} else {
  // Code to execute if both condition1 and condition2 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 neither condition1 nor condition2 is true, the code inside the third set of braces will be executed.

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

Example-1:

#include <stdio.h>

int main() {
   int x = 10;
   
   if (x > 5) {
      printf("x is greater than 5.\n");
   }
   
   return 0;
}

In this example, the “if” statement checks whether the value of the variable “x” is greater than 5. Since the value of “x” is 10, the condition is true and the code inside the braces is executed, which prints “x is greater than 5.” 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");
   }
   
   return 0;
}

In this example, the “if” 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.” 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 (num2 > num1) {
      printf("%d is greater than %d.\n", num2, num1);
   } else {
      printf("Both numbers are equal.\n");
   }
   
   return 0;
}

In this example, the “if” 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 the first number followed by “is greater than” followed by the second number. If the second number is greater than the first number, the code inside the second set of braces is executed, which prints the second number followed by “is greater than” followed by the first number. If both numbers are equal, the code inside the third set of braces is executed, which prints “Both numbers 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.