C else...if Statement

You are Here:

C else...if Statement

The else...if statement executes a block if the specified condition is true. This occurs only when the preceeding condition in the if or else if failed.

Note: You can use multiple else if statement one after the other.

The following 2 example programs will help you to understand the if statement better.

Single else...if Statement

In the below example program, the else...if block will be executed because the value in the variable x is lesser than 50 but greater than 20.

Source Code

C Compiler
#include <stdio.h> int main() { int x = 30; if(x > 50){ printf("x is greater than 50"); } else if(x > 20){ // This block will be executed printf("x is greater than 20 but lesser than 50"); } else{ printf("x is lesser than 20"); } return 0; }

Output

x is greater than 20 but lesser than 50

Multiple else...if Statement

In the below example program, the value in a variable x is 12.

  1. First, if condition checks whether x > 50 and returns false.
  2. Second, else...if condition checks whether x > 20 and returns false.
  3. Third, else...if condition checks whether x > 10 and returns true and executes the block of code.
  4. Fourth, else block is skipped as we have true condition.

Source Code

C Compiler
#include <stdio.h> int main() { int x = 12; if(x > 50){ printf("x is greater than 50"); } else if(x > 20){ printf("x is greater than 20 but lesser than 50"); } else if(x > 10){ // This block will be executed printf("x is greater than 10 but lesser than 20"); } else{ printf("x is lesser than 10"); } return 0; }

Output

x is greater than 10 but lesser than 20
congratulation

Congratulation! you have learned everything about else...if statement in C Programming.

Reminder

Hi Developers, we almost covered 98% of String functions and Interview Question on C with examples for quick and easy learning.

We are working to cover every Single Concept in C.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author