
- 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
C - The if-else Statement
The if-else statement is one of the frequently used decision-making statements in C. The if-else statement offers an alternative path when the condition isn't met.
The else keyword helps you to provide an alternative course of action to be taken when the Boolean expression in the if statement turns out to be false. The use of else keyword is optional; it's up to you whether you want to use it or not.
Syntax of if-else Statement
Here is the syntax of if-else clause −
if (Boolean expr){ Expression; . . . } else{ Expression; . . . }
The C compiler evaluates the condition, and executes a statement or a block of statements following the if statement if it is true.
If the programming logic needs the computer to execute some other instructions when the condition is false, they are put as a part of the else clause.
An if statement is followed by an optional else statement, which executes when the Boolean expression is false.
Flowchart of if-else Statement
The following flowchart represents how the if-else clause works in C −

Note that the curly brackets in the if as well as the else clause are necessary if you have more than one statements to be executed. For example, in the following code, we dont need curly brackets.
if (marks<50) printf("Result: Fail\n"); else printf("Result: Pass\n");
However, when there are more than one statements, either in the if or in the else part, you need to tell the compiler that they need to be treated as a compound statement.
C if-else Statement Examples
Example: Tax Calculation Using if-else Statement
In the code given below, the tax on employees income is computed. If the income is below 10000, the tax is applicable at 10%. For the income above 10000, the excess income is charged at 15%.
#include <stdio.h> int main() { int income = 5000; float tax; printf("Income: %d\n", income); if (income<10000){ tax = (float)(income * 10 / 100); printf("tax: %f \n", tax); } else { tax= (float)(1000 + (income-10000) * 15 / 100); printf("tax: %f", tax); } }
Output
Run the code and check its output −
Income: 5000 tax: 500.000000
Set the income variable to 15000, and run the program again.
Income: 15000 tax: 1750.000000
Example: Checking Digit Using if-else Statement
The following program checks if a char variable stores a digit or a non-digit character.
#include <stdio.h> int main() { char ch='7'; if (ch>=48 && ch<=57){ printf("The character is a digit."); } else{ printf("The character is not a digit."); } return 0; }
Output
Run the code and check its output −
The character is a digit.
Assign any other character such as "*" to "ch" and see the result.
The character is not a digit.
Example: if-else Statement Without Curly Braces
Consider the following code. It intends to calculate the discount at 10% if the amount is greater than 100, and no discount otherwise.
#include <stdio.h> int main() { int amount = 50; float discount; printf("Amount: %d\n", amount); if (amount >= 100) discount = amount * 10 / 100; printf("Discount: %f \n", discount); else printf("Discount not applicable\n"); return 0; }
Output
The program shows the following errors during the compilation −
error: 'else' without a previous 'if'
The compiler will execute the first statement after the if clause and assumes that since the next statement is not else (it is optional anyway), the subsequent printf() statement is unconditional. However, the next else is not connected to any if statement, hence the error.
Example: if-else Statement Without Curly Braces
Consider the following code too −
#include <stdio.h> int main() { int amount = 50; float discount, nett; printf("Amount: %d\n", amount); if (amount<100) printf("Discount not applicable\n"); else printf("Discount applicable"); discount = amount*10/100; nett = amount - discount; printf("Discount: %f Net payable: %f", discount, nett); return 0; }
Output
The code doesnt give any compiler error, but gives incorrect output −
Amount: 50 Discount not applicable Discount: 5.000000 Net payable: 45.000000
It produces an incorrect output because the compiler assumes that there is only one statement in the else clause, and the rest of the statements are unconditional.
The above two code examples emphasize the fact that when there are more than one statements in the if or else else, they must be put in curly brackets.
To be safe, it is always better to use curly brackets even for a single statement. In fact, it improves the readability of the code.
The correct solution for the above problem is shown below −
if (amount >= 100){ discount = amount * 10 / 100; printf("Discount: %f \n", discount); } else { printf("Discount not applicable\n"); }
The else-if Statement in C
C also allows you to use else-if in the programs. Let's see where you may have to use an else-if clause.
Let's suppose you have a situation like this. If a condition is true, run the given block that follows. If it isn't, run the next block instead. However, if none of the above is true and all else fails, finally run another block. In such cases, you would use an else-if clause.
Syntax of else-if Statement
Here is the syntax of the else-if clause −
if (condition){ // if the condition is true, // then run this code } else if(another_condition){ // if the above condition was false // and this condition is true, // then run the code in this block } else{ // if both the above conditions are false, // then run this code }
Example of else-if Statement
Take a look at the following example −
#include <stdio.h> int main(void) { int age = 15; if (age < 18) { printf("You need to be over 18 years old to continue\n"); }else if (age < 21) { printf("You need to be over 21\n"); } else { printf("You are over 18 and older than 21 so you can continue \n"); } }
Output
Run the code and check its output −
You need to be over 18 years old to continue
Now, supply a different value for the variable "age" and run the code again. You will get a different output if the supplied value is less than 18.