Decision making
statements are an important part of C programming because they allow a program
to make choices based on conditions. These statements control the flow of
execution depending on whether a condition is true or false.
In real-life programming, decisions are everywhere—for example, checking if a user is logged in, verifying age, or comparing values. C provides several decision making statements to handle such situations.
In this article, you
will learn about different decision making statements in C programming with
clear explanations and examples.
What Are Decision Making Statements in C?
Decision making
statements are used to execute specific blocks of code based on conditions.
These conditions are usually evaluated using relational and logical operators.
If a condition is true,
one block of code runs; if it is false, another block may run.
Types of Decision Making Statements in C
C programming provides
the following decision making statements:
1.
if statement
2.
if-else statement
3.
nested if statement
4.
if-else-if ladder
5.
switch statement
Let’s explore each one
in detail.
1. if Statement
The if statement is the
simplest decision making statement. It executes a block of code only if the
condition is true.
Syntax:
if (condition)
{
// code to execute if
condition is true
}
Example:
int age = 18;
if (age >= 18)
{
printf("You are
eligible to vote");
}
2. if-else Statement
The if-else statement provides
two paths: one for true and one for false conditions.
Syntax:
if (condition)
{
// code if true
}
else
{
// code if false
}
Example:
int number = 10;
if (number % 2 == 0)
{
printf("Even
number");
}
else
{
printf("Odd
number");
}
3. Nested if Statement
A nested if statement is
an if statement inside another if statement.
Example:
int age = 20;
int citizen = 1;
if (age >= 18)
{
if (citizen == 1)
{
printf("Eligible
to vote");
}
}
4. if-else-if Ladder
This is used when
multiple conditions need to be checked.
Syntax:
if (condition1)
{
// code
}
else if (condition2)
{
// code
}
else
{
// default code
}
Example:
int marks = 75;
if (marks >= 90)
{
printf("Grade
A");
}
else if (marks >= 75)
{
printf("Grade
B");
}
else if (marks >= 50)
{
printf("Grade
C");
}
else
{
printf("Fail");
}
5. switch Statement
The switch statement is used
when you need to compare a variable against multiple values.
Syntax:
switch (expression)
{
case value1:
// code
break;
case value2:
// code
break;
default:
// default code
}
Example:
int day = 3;
switch (day)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
default:
printf("Invalid
day");
}
Difference Between if-else and switch
|
if-else |
switch |
|
Used for conditions |
Used for fixed values |
|
Supports complex logic |
Works with simple comparisons |
|
Slower for many conditions |
Faster for multiple cases |
Real-Life Uses of Decision Making Statements
- Login authentication systems
- Grading systems
- ATM transactions
- Menu-driven programs
- Traffic light systems
Advantages of Decision Making Statements
- Enable logical control in programs
- Make programs interactive
- Allow multiple outcomes
- Improve program flexibility
- Help solve real-world problems
Common Mistakes to Avoid
- Forgetting break in switch cases
- Using assignment = instead of ==
- Missing braces in if statements
- Incorrect logical conditions
Conclusion
Decision making
statements in C programming are essential for controlling the flow of a
program. They allow programs to make choices based on conditions using if,
if-else, nested if, if-else-if ladder, and switch statements.
By mastering these concepts, beginners can build more intelligent and interactive programs that solve real-world problems effectively.
