
- C - Home
- C - Overview
- C - Features
- C - History
- C - Standards
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Basic Syntax
- C - User Input
- C - printf Function
- C - Format Specifiers
- Lexical Elements in C
- C - Tokens
- C - Keywords
- C - Identifiers
- Variables and Constants
- C - Variables
- C - Constants
- C - Const Qualifier
- C - Linkage
- Data Types and Type Conversions
- C - Data Types
- C - Literals
- C - Escape sequences
- C - Booleans
- C - Integer Promotions
- C - Character Arithmetic
- C - Type Conversion
- C - Type Casting
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Unary Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Miscellaneous Operators
- Decision Making & Control Statements
- C - Decision Making
- C - if statement
- C - if...else statement
- C - if...else if Ladder
- C - Nested if statements
- C - Switch statement
- C - Nested switch statements
- C - Switch Case Using Range
- Loops in C
- C - Loops
- C - For Loop
- C - While Loop
- C - Do...while Loop
- C - For Loop vs While Loop
- C - Nested Loop
- C - Infinite Loop
- C - Break Statement
- C - Continue Statement
- C - Goto Statement
- Functions in C
- C - Functions
- C - Function Prototype
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- C - Predefined Identifier __func__
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- C - Dynamic Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Character Arrays
- C - Special Characters
- Structures and Unions in C
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Dot (.) Operator
- C - Lookup Tables
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Function Pointers
- C - Array of Function Pointers
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Pointer to an Array
- C - Pointers vs. Multi-dimensional Arrays
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Const Pointers & Pointer to Const
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Restrict Keyword
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Initialization of Pointer Arrays
- Storage Classes and Qualifiers
- C - Storage Classes
- Memory Management in C
- C - Memory Management
- C - Memory Address
- Preprocessors in C
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- File Handling in C
- C - File I/O (File Handling)
- C - Input & Output
- Constants and Literals in C
- C - Macros
- C - Header Files
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
- C Online Compiler
if...else if Ladder in C
The if-else-if ladder in C is an extension of the simple if-else statement that is used to test multiple conditions sequentially. It executes a block of code associated with the first condition that evaluates to true.
Syntax of ifâ¦else if Ladder in C
The syntax of ifâ¦else if ladder in C is as follows −
if (condition1) { // Code to be executed if condition1 is true } else if (condition2) { // Code to be executed if condition1 is false and condition2 is true } else if (condition3) { // Code to be executed if all previous are false, and condition3 is true } // ... else { // Code to be executed if all preceding conditions are false }
In an if...else if ladder, each condition is evaluated sequentially, but only if all the previous conditions are false. As soon as a condition evaluates to true, its corresponding code block executes, and the ladder terminates without checking the remaining conditions.
Example
Letâs understand the ifâ¦else if ladder in C with an example to see how the code executes and how the flow runs step by step.
#include <stdio.h> int main() { int items = 72; if (items >= 90) { printf("Store is full"); } else if (items >= 80) { printf("Need few more items"); } else if (items >= 70) { printf("Store is partially full"); } else if (items >= 50) { printf("Sore is half full"); } else { printf("Store is empty"); } return 0; }
When you run this code, it will produce the following output −
Store is partially full
Explanation − In this example, there are 72 items. The condition "items >= 90" evaluates to false, so the control moves to "items >= 80", which is also false.
Next, it checks the condition "items >= 70", which evaluates to true. Therefore, the statement "Store is partially full" is printed, and the remaining conditions in the ladder are skipped.
Components of ifâ¦else if Ladder
Letâs look at the components of an if...else if ladder in C programming −
If statement
The if statement first statement in the if...else if ladder. The if block contains a condition and a code block, which executes if the condition evaluates to true; otherwise, the control passes to the next statement.
else if statement
The else-if statement always comes after the if statement. An if...else if ladder can have multiple else if statements, each containing a condition to be tested. If a condition evaluates to true, its corresponding code block executes; otherwise, the control moves to the next statement.
else statement
The else statement is the last statement in an ifâ¦else if ladder. The else statement does not contain any condition, however it contains a code block which is executed if all the conditions mentioned in the above if and else if statements evaluate to false.
Working of ifâ¦else if Ladder
The working of the if...else if ladder in C can be understood using the following flowchart −

This is how the control flow moves â
- If condition 1 is true, Statement 1 executes, and the remaining else-if and else conditions are skipped.
- If condition 1 is false, condition 2 is evaluated. If condition 2 is true, Statement 2 executes, and the else block is skipped.
- If condition 2 is also false, the else block executes.
- After the if...else if ladder finishes, the statement immediately following the ladder executes.
Example 1
In this C example, we have used an ifâ¦else if ladder to display the day names based on their designated numbers −
#include <stdio.h> int main() { int day = 5; printf("Day Number: %d\n", day); if (day == 1) { printf("Day Name: Monday\n"); } else if (day == 2) { printf("Day Name: Tuesday\n"); } else if (day == 3) { printf("Day Name: Wednesday\n"); } else if (day == 4) { printf("Day Name: Thursday\n"); } else if (day == 5) { printf("Day Name: Friday\n"); } else if (day == 6) { printf("Day Name: Saturday\n"); } else if (day == 7) { printf("Day Name: Sunday\n"); } else { printf("Invalid day Number\n"); } return 0; }
Run this code and check its output. When you enter the number "5", the program will print "Friday" on the console −
Day Number: 5 Day Name: Friday
Example 2
The following C Program uses an if...else if ladder to check whether a given number is even, odd, even & prime, or odd & prime −
#include <stdio.h> #include <stdbool.h> // Function to check prime number bool isPrime(int num) { if (num <= 1) { return false; } for (int i = 2; i * i <= num; i++) { if (num % i == 0) { return false; } } return true; } int main() { int num = 5; // if...else if ladder if (num % 2 == 0 && isPrime(num)) { printf("%d is Even and Prime.\n", num); } else if (num % 2 != 0 && isPrime(num)) { printf("%d is Odd and Prime.\n", num); } else if (num % 2 == 0) { printf("%d is Even.\n", num); } else if (num % 2 != 0) { printf("%d is Odd.\n", num); } else { printf("Invalid input.\n"); } return 0; }
When you run this code, it will produce the following output −
5 is Odd and Prime.