if...else Statement
Introduction to C
Structure of a C Program
Variables and Data Types
Input and Output
Operators in C
Decision Making
If Statement
if...else Statement
Nested if Statement
Nested if...else Statement
Switch Statement
Loops
For Loop
While Loop
Do While Loop
Loop Comparison
Arrays
Strings
Functions
Function Types
Library Functions
User-Defined Functions
Recursion Function
Pointers
Structures
File Handling
Common Mistakes
Ideas
if...else Statement
The if...else Statement
What is an if...else Statement?
The if...else statement is used when you have two possible paths of execution. It allows the program to perform one action if a condition is True and a different action if the condition is False.
- The
ifblock: Executes only if the condition is met. - The
elseblock: Acts as a "catch-all" that executes if the condition is not met.
Syntax
if (condition) {
// Code runs if condition is TRUE
} else {
// Code runs if condition is FALSE
}
// Code runs if condition is TRUE
} else {
// Code runs if condition is FALSE
}
Practical Examples
Example 1: Pass or Fail Logic
int marks = 25; if (marks >= 35) { printf("Result: Passed\n"); } else { printf("Result: Failed\n"); }
In this case, since 25 is less than 35, the "Failed" message will be displayed.
Example 2: Even or Odd Checker
int num = 10; if (num % 2 == 0) { printf("The number is Even.\n"); } else { printf("The number is Odd.\n"); }
Important Note: Only one of the two blocks can ever run. The computer evaluates the condition once and chooses the path immediately—it never executes both!
🏋️ Test Yourself With Exercises
Take our quiz on if...else Statement to test your knowledge.
Browse Quizzes »