In C, programs can choose which part of the code to execute based on some condition. This ability is called decision making and the statements used for it are called conditional statements. These statements evaluate one or more conditions and make the decision whether to execute a block of code or not.
#include <stdio.h>
int main() {
// Number of people in the audience
int num = 100;
// Conditional code inside decision making statement
if (num > 50) {
printf("Start the show");
}
return 0;
}
Output
Start the show
In the above program, the show only starts when the number of people is greater than 50. It is specified in the if statement (a type of conditional statement) as a condition (num > 50). You can decrease the value of num to less than 50 and try rerunning the code.
Types of Conditional Statements in C
In the above program, we have used if statement, but there are many different types of conditional statements available in C language:

if in C
The if statement is the simplest decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statements is executed otherwise not.
A condition is any expression that evaluates to either a true or false (or values convertible to true or false).
#include <stdio.h>
int main()
{
int age = 20;
// If statement
if (age >= 18)
{
printf("Eligible for vote");
}
return 0;
}
Output
Eligible for vote
The expression inside () parenthesis is the condition and set of statements inside {} braces is its body. If the condition is true, only then the body will be executed.
If there is only a single statement in the body, {} braces can be omitted.
if-else in C
The if statement alone tells us that if a condition is true, it will execute a block of statements and if the condition is false, it won’t. But what if we want to do something else when the condition is false? Here comes the C else statement. We can use the else statement with the if statement to execute a block of code when the condition is false. The if-else statement consists of two blocks, one for false expression and one for true expression.
#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 vote
The block of code following the else statement is executed as the condition present in the if statement is false.
Nested if-else in C
A nested if in C is an if statement that is the target of another if statement. Nested if statements mean an if statement inside another if statement. Yes, C allow us to nested if statements within if statements, i.e, we can place an if statement inside another if statement.
#include <stdio.h>
int main(){
int age = 11;
if (age >= 18) {
if (age >= 60)
printf("Eligible to vote (Senior Citizen)\n");
else
printf("Eligible for vote\n");
}
else {
printf("Not eligible to vote (Under 18)\n");
if (age >= 13)
printf("teenager\n");
else
printf("not a teenager\n");
}
return 0;
}
Output
Not eligible to vote (Under 18) not a teenager
if-else-if Ladder in C
The if else if statements are used when the user has to decide among multiple options. The C if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed. if-else-if ladder is similar to the switch statement.
#include <stdio.h>
int main() {
int i = 20;
// If else ladder with three conditions
if (i == 10)
printf("Not Eligible");
else if (i == 15)
printf("wait for three years");
else if (i == 20)
printf("You can vote");
else
printf("Not a valid age");
return 0;
}
Output
You can vote
switch Statement in C
The switch case statement is an alternative to the if else if ladder that can be used to execute the conditional code based on the value of the variable specified in the switch statement. The switch block consists of cases to be executed based on the value of the switch variable.
#include <stdio.h>
int main() {
// variable to be used in switch statement
int var = 18;
// declaring switch cases
switch (var) {
case 15:
printf("You are a kid");
break;
case 18:
printf("Eligible for vote");
break;
default:
printf("Default Case is executed");
break;
}
return 0;
}
Output
Eligible for vote
Note: The switch expression should evaluate to either integer or character. It cannot evaluate any other data type.
Conditional Operator in C
The conditional operator is used to add conditional code in our program. It is similar to the if-else statement. It is also known as the ternary operator as it works on three operands.
#include <stdio.h>
int main() {
int var;
int flag = 0;
// using conditional operator to assign the value to var
// according to the value of flag
var = flag == 0 ? 25 : -25;
printf("Value of var when flag is 0: %d\n", var);
return 0;
}