Understanding the C If-Else Statement: Conditional Logic in Action

Learn about the if-else statement in C programming, which enables executing different code blocks based on the evaluation of a condition. Discover how the optional else part provides alternative actions when the condition is false, enhancing your program's decision-making capabilities.



C - The if-else Statement

The if-else statement in C allows you to execute different code based on whether a condition is true or false. The else part is optional and provides an alternative action when the condition is false.

Syntax of if-else Statement

Syntax

if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
    

Flowchart of if-else Statement

The flowchart for the if-else statement shows the decision-making process.

Flowchart of if-else Statement

Example 1: Tax Calculation

This example calculates tax based on income. If income is below $10,000, the tax is 10%. Above $10,000, the tax on the excess is 15%.

Code

#include 

int main() {
int income = 12000;
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\n", tax);
}
return 0;
}
    
Output

Income: 12000
Tax: 2000.000000
    

Example 2: Checking Digit

This example checks if a character is a digit.

Code

#include 

int main() {
char ch = '8';

if (ch >= '0' && ch <= '9') {
printf("The character is a digit.\n");
} else {
printf("The character is not a digit.\n");
}
return 0;
}
    
Output

The character is a digit.
    

Example 3: if-else Without Curly Braces

This example calculates a discount. Without curly braces, the else statement may not work as expected.

Code

#include 

int main() {
int amount = 150;
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

Amount: 150
Discount: 15.000000
    

Example 4: if-else Without Curly Braces - Incorrect Output

This example shows how omitting curly braces can lead to incorrect results. The correct approach is to use curly braces to group statements.

Code

#include 

int main() {
int amount = 150;
float discount, net;
printf("Amount: %d\n", amount);

if (amount < 100)
printf("Discount not applicable\n");
else
printf("Discount applicable");
discount = amount * 10 / 100;
net = amount - discount;
printf("Discount: %f Net payable: %f\n", discount, net);

return 0;
}
    
Output

Amount: 150
Discount applicable
Discount: 15.000000 Net payable: 135.000000
    

The else-if Statement

The else-if statement allows multiple conditions to be checked. It provides additional conditions to test if the previous conditions are false.

Code

#include 

int main(void) {
int age = 25;

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");
}
return 0;
}
    
Output

You are over 18 and older than 21, so you can continue