Decision making in C
Decision making in C refers to the process of using conditional statements to control the flow of execution. These conditions are usually based on relational or logical expressions. When a condition is satisfied, a specific block of code is executed; otherwise, another block runs or the program moves ahead.
Types of Decision Making Statements in C

There are several statements used for decision making in C, each serving a specific purpose:
1. 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");
}
}
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.
2. 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.
3. 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
4. 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
5. 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
