If-else Statement in C

Updated

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.

Comment
Next tutorial
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.

Related tutorials

  • Call by value, call by address and call by reference in C++

    The parameters of the function call are known as actual parameters and the parameters of the function definition are known as formal parameters. There are 3 ways of…

    3 min read
  • 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. GATE CS 2027 Online Test Series72 tests in the real GATE…

    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