Conditional Statements Tutorials
Programming
Conditional Statements in C (if, if-else, Nested if, if-else-if)
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.
Why Do We Need Decision Making?
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.
Example of Decision Making
#include <stdio.h>
int main()
{
int num = 100;
if (num > 50)
{
printf("Start the show");
}
return 0;
}Output
Start the showIn the above example, the message is displayed only when the condition num > 50 is true.
Types of Conditional Statements in C
C provides the following decision-making statements:
if Statement
if-else Statement
Nested if-else Statement
if-else-if Ladder
switch Statement
Conditional Operator (?:)
if Statement
The if statement is the simplest decision-making statement in C.
It executes a block of code only when the specified condition is true.
Syntax
if(condition)
{
// code to execute
}Example
#include <stdio.h>
int main()
{
int age = 20;
if (age >= 18)
{
printf("Eligible for vote");
}
return 0;
}Output
Eligible for voteImportant Points
The condition must evaluate to true or false.
If the condition is true, the code inside the
ifblock executes.If the condition is false, the code inside the block is skipped.
Curly braces
{}are optional when there is only one statement.
if-else 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.
Syntax
if(condition)
{
// code if condition is true
}
else
{
// code if condition is false
}Example
#include <stdio.h>
int main()
{
int age = 10;
if (age >= 18)
{
printf("Eligible for vote");
}
else
{
printf("Not Eligible for vote");
}
return 0;
}Output
Not Eligible for voteHow it Works
If the condition is true, the
ifblock executes.If the condition is false, the
elseblock executes.
Nested if-else Statement
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.
Example
#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;
}Output
Not eligible to vote
Not a teenagerHow it Works
First, 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.
if-else-if Ladder
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.
Syntax
if(condition1)
{
// code
}
else if(condition2)
{
// code
}
else if(condition3)
{
// code
}
else
{
// code
}Example
#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;
}Output
You can voteHow it Works
Conditions are checked one by one.
The first true condition executes its block.
Remaining conditions are ignored.
If no condition is true, the
elseblock executes.
switch Statement
The switch statement is an alternative to the if-else-if ladder when checking a variable against multiple values.
Syntax
switch(variable)
{
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}Example
#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;
}Output
Eligible for voteImportant Points
The switch expression should be an integer or character value.
Each case should normally end with a
breakstatement.The
defaultblock executes when no case matches.
Conditional Operator (?:)
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.
Syntax
condition ? expression1 : expression2;Example
#include <stdio.h>
int main()
{
int flag = 0;
int value = (flag == 0) ? 25 : -25;
printf("%d", value);
return 0;
}Output
25How it Works
If the condition is true, the first expression executes.
If the condition is false, the second expression executes.
Difference Between if-else and switch
if-else
Used for checking conditions and expressions.
Can handle relational and logical operators.
Better when conditions are complex.
switch
Used for comparing a variable against fixed values.
Easier to read when there are many choices.
Works only with integer and character values.
Summary
Decision-making statements allow a program to choose different execution paths.
The
ifstatement executes code when a condition is true.The
if-elsestatement provides two execution paths.Nested
ifstatements allow multiple levels of condition checking.The
if-else-ifladder is used for multiple conditions.The
switchstatement is an alternative to the if-else-if ladder.The conditional operator
?:provides a short form of the if-else statement.