Loading content...
Loading content...
In C programming, decision-making statements allow a program to choose different paths of execution based on certain conditions. These statements evaluate conditions and decide which block of code should be executed.
Decision-making statements are also known as conditional statements.
In real-world applications, a program often needs to make decisions.
For example:
Check whether a user is eligible to vote.
Verify login credentials.
Determine whether a student has passed or failed.
Display different messages based on user input.
Decision-making statements make such tasks possible.
#include <stdio.h>
int main()
{
int num = 100;
if (num > 50)
{
printf("Start the show");
}
return 0;
}Start the showIn the above example, the message is displayed only when the condition num > 50 is true.
C provides the following decision-making statements:
if Statement
if-else Statement
Nested if-else Statement
if-else-if Ladder
switch Statement
Conditional Operator (?:)
The if statement is the simplest decision-making statement in C.
It executes a block of code only when the specified condition is true.
if(condition)
{
// code to execute
}#include <stdio.h>
int main()
{
int age = 20;
if (age >= 18)
{
printf("Eligible for vote");
}
return 0;
}Eligible for voteThe condition must evaluate to true or false.
If the condition is true, the code inside the if block executes.
If the condition is false, the code inside the block is skipped.
Curly braces {} are optional when there is only one statement.
The if-else statement is used when we want to execute one block of code when the condition is true and another block when the condition is false.
if(condition)
{
// code if condition is true
}
else
{
// code if condition is false
}#include <stdio.h>
int main()
{
int age = 10;
if (age >= 18)
{
printf("Eligible for vote");
}
else
{
printf("Not Eligible for vote");
}
return 0;
}Not Eligible for voteIf the condition is true, the if block executes.
If the condition is false, the else block executes.
A Nested if statement means placing one if statement inside another if or else block.
It is useful when multiple conditions need to be checked.
#include <stdio.h>
int main()
{
int age = 11;
if (age >= 18)
{
if (age >= 60)
printf("Eligible to vote (Senior Citizen)");
else
printf("Eligible for vote");
}
else
{
printf("Not eligible to vote\n");
if (age >= 13)
printf("Teenager");
else
printf("Not a teenager");
}
return 0;
}Not eligible to vote
Not a teenagerFirst, the outer condition is checked.
If it is true, the inner condition is evaluated.
If it is false, the control moves to the outer else block.
The if-else-if ladder is used when multiple conditions need to be tested one after another.
The conditions are checked from top to bottom.
As soon as one condition becomes true, its corresponding block is executed and the remaining conditions are skipped.
if(condition1)
{
// code
}
else if(condition2)
{
// code
}
else if(condition3)
{
// code
}
else
{
// code
}#include <stdio.h>
int main()
{
int age = 20;
if (age == 10)
{
printf("Not Eligible");
}
else if (age == 15)
{
printf("Wait for three years");
}
else if (age == 20)
{
printf("You can vote");
}
else
{
printf("Invalid Age");
}
return 0;
}You can voteConditions are checked one by one.
The first true condition executes its block.
Remaining conditions are ignored.
If no condition is true, the else block executes.
The switch statement is an alternative to the if-else-if ladder when checking a variable against multiple values.
switch(variable)
{
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}#include <stdio.h>
int main()
{
int age = 18;
switch(age)
{
case 15:
printf("You are a kid");
break;
case 18:
printf("Eligible for vote");
break;
default:
printf("Default Case");
}
return 0;
}Eligible for voteThe switch expression should be an integer or character value.
Each case should normally end with a break statement.
The default block executes when no case matches.
The Conditional Operator is also known as the Ternary Operator because it works with three operands.
It provides a shorter way to write simple if-else statements.
condition ? expression1 : expression2;#include <stdio.h>
int main()
{
int flag = 0;
int value = (flag == 0) ? 25 : -25;
printf("%d", value);
return 0;
}25If the condition is true, the first expression executes.
If the condition is false, the second expression executes.
Used for checking conditions and expressions.
Can handle relational and logical operators.
Better when conditions are complex.
Used for comparing a variable against fixed values.
Easier to read when there are many choices.
Works only with integer and character values.
Decision-making statements allow a program to choose different execution paths.
The if statement executes code when a condition is true.
The if-else statement provides two execution paths.
Nested if statements allow multiple levels of condition checking.
The if-else-if ladder is used for multiple conditions.
The switch statement is an alternative to the if-else-if ladder.
The conditional operator ?: provides a short form of the if-else statement.