If Statement in C

Last Updated :

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.

Comment
Next Article
Mithlesh Upadhyay Published 20 Feb, 2023 · 4 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Similar Reads

  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read