The if-else statement is a decision-making statement in C. It allows a program to execute one block of code when a condition is true and another block when the condition is false.
if when you want to execute code
for a true condition and else when you want to execute
alternative code for a false condition.
The if-else statement provides two possible paths in a program.
One path executes when the condition is true, and the other executes when
the condition is false.
if (condition)
{
// true block
}
else
{
// false block
}
if (condition)
{
statement1;
}
else
{
statement2;
}
Only one of the two blocks is executed for each evaluation of the
if-else statement.
int age = 16;
if (age >= 18)
{
printf("Adult");
}
else
{
printf("Minor");
}
Since the age is less than 18, the else block executes.
The execution process is:
if block executes.else block executes.int marks = 65;
if (marks >= 40)
{
printf("Pass");
}
else
{
printf("Fail");
}
Because the marks are 65, the condition is true and Pass is
displayed.
int number = 7;
if (number % 2 == 0)
{
printf("Even");
}
else
{
printf("Odd");
}
The remainder of 7 divided by 2 is not zero, so the program prints
Odd.
int number = -10;
if (number >= 0)
{
printf("Positive");
}
else
{
printf("Negative");
}
This example treats zero as non-negative.
int a = 20;
int b = 10;
if (a > b)
{
printf("A is greater");
}
else
{
printf("B is greater or equal");
}
The else block executes whenever a > b is false.
int a = 10;
int b = 10;
if (a == b)
{
printf("Both are equal");
}
else
{
printf("Both are different");
}
The equality operator == is used to compare the two values.
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18)
{
printf("You are an adult.");
}
else
{
printf("You are a minor.");
}
return 0;
}
The result depends on the age entered by the user.
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18)
{
printf("Eligible to vote.");
}
else
{
printf("Not eligible to vote.");
}
The program selects one message based on the age condition.
int age = 25;
int marks = 70;
if (age >= 18 && marks >= 40)
{
printf("Eligible");
}
else
{
printf("Not Eligible");
}
The && operator requires both conditions to be true.
int number = 5;
if (number == 5 || number == 10)
{
printf("Valid number");
}
else
{
printf("Invalid number");
}
The condition is true when at least one of the two comparisons is true.
char grade = 'A';
if (grade == 'A')
{
printf("Excellent");
}
else
{
printf("Keep improving");
}
Characters can also be used in conditions.
float price = 150.50f;
if (price > 100.0f)
{
printf("Expensive");
}
else
{
printf("Affordable");
}
Floating-point values can be used in comparisons.
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number > 0)
{
printf("Positive number");
}
else
{
printf("Zero or negative number");
}
This example has two possible outcomes.
int a = 50;
int b = 30;
if (a > b)
{
printf("A is larger");
}
else
{
printf("B is larger or equal");
}
The condition determines which value is larger.
int a = 20;
int b = 40;
if (a < b)
{
printf("A is smaller");
}
else
{
printf("B is smaller or equal");
}
The < operator is used to compare the two values.
int number = 20;
if (number % 5 == 0)
{
printf("Divisible by 5");
}
else
{
printf("Not divisible by 5");
}
If the remainder is zero, the number is divisible by 5.
int marks = 75;
if (marks >= 40)
{
printf("You passed.\n");
printf("Congratulations!\n");
}
else
{
printf("You failed.\n");
printf("Try again.");
}
Both blocks can contain multiple statements.
Curly braces clearly identify which statements belong to each branch. They are recommended even when a branch currently contains only one statement.
if (number > 10)
{
printf("Greater");
}
else
{
printf("Not greater");
}
When each branch contains exactly one statement, braces can technically be omitted.
if (number > 0)
printf("Positive");
else
printf("Not positive");
Using braces is usually clearer, especially when code is later modified.
Incorrect:
if (number = 10)
{
printf("Number is 10");
}
else
{
printf("Number is not 10");
}
The = operator performs assignment. For comparison, use
==.
Correct:
if (number == 10)
{
printf("Number is 10");
}
else
{
printf("Number is not 10");
}
Avoid:
if (number > 10);
{
printf("Greater than 10");
}
else
{
printf("10 or less");
}
A semicolon immediately after the condition ends the if
statement and causes a syntax problem with the following else.
Correct:
if (number > 10)
{
printf("Greater than 10");
}
else
{
printf("10 or less");
}
In C, zero is treated as false and a nonzero value is treated as true.
int value = 0;
if (value)
{
printf("True");
}
else
{
printf("False");
}
Output:
False
An if statement can also appear inside an else
block.
int number = -5;
if (number > 0)
{
printf("Positive");
}
else
{
if (number == 0)
{
printf("Zero");
}
else
{
printf("Negative");
}
}
More complex alternatives are often easier to manage using an
else-if ladder, which will be covered in the next lesson.
#include <stdio.h>
int main()
{
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
if (marks >= 40)
{
printf("Result: Pass");
}
else
{
printf("Result: Fail");
}
return 0;
}
This is a simple real-world example of using if-else for
decision making.
#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number % 2 == 0)
{
printf("Even number");
}
else
{
printf("Odd number");
}
return 0;
}
The remainder operator % is used to determine whether the
number is divisible by 2.
#include <stdio.h>
int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a > b)
{
printf("%d is larger", a);
}
else
{
printf("%d is larger or equal", b);
}
return 0;
}
This program compares two numbers and selects one of the two output messages.
Practice the following programs:
Practice writing the condition first, then create the true and false blocks.
if-else statement provides two possible execution paths.if block executes when the condition is true.else block executes when the condition is false.if-else statement executes.== for comparison and = for assignment.else block does not have a condition of its own.Question: Which block is executed when the if condition is false?