Decision Making Statements in C
Decision-Making Statements
“Decision-making statements are used to execute a particular block of code based on a condition or set of conditions. These conditions can be any expression that evaluates to a Boolean value (true or false).”
These statements are an essential part of programming as they allow developers to take specific steps based on conditions. They are also known as conditional statements, as they are evaluated based on conditions.
What are Decision Making Statements in C?
Decision-making statements decide the flow of the program. In C language, decision-making can be done using the following statements.
- If statement
- If else statement
- Nested if statements
- If else if ladder
- Switch statement
If Statement
If statement is the most basic conditional statement in C. If statements decide whether a particular block of code will be executed or not, based on condition. For example, I will wear a raincoat if it is raining outside. This statement is based on a condition and can be easily converted in a conditional statement in C. E.g.
// If statement demonstration
if (raining = true){
print(“wear a raincoat”);
}
This code snippet demonstrates how to write a condition into a decision-making statement in C with the if statement. The syntax for the if statement is as follows,
//syntax for if statement
if (condition){
// code to be executed
}
The syntax clearly defines the construct of an if statement. Let us see an example of a code based on an if statement.
Code:
#include <stdio.h>
int main() {
int num = 10;
if (num > 0) {
printf("%d is a positive number.", num);
}
return 0;
}
Output:
If else statement
The if else statement is an extension of if statement. In the if statement case, we were only concerned about the actual value of the condition. But we are handling both the values of the condition using the else statement along with the if statement. For example, if it is raining outside, I will wear a raincoat; otherwise, I will wear a hat. Here we are dealing with both possibilities that if the condition is true, do this, else do this. This is the logic behind the if else statement. E.g.
// If else demonstration
if (raining=true){
print(“wear a raincoat”);
}
else{
print(“wear a hat”);
}
This is how we will convert our day-to-day logic into conditional programming statements. The syntax for the if-else statement is as follows,
//syntax for if else statement
if (condition){
// code to be executed
}
else{
// code to be executed
}
The syntax demonstrates that if the condition is true, then the code inside the if block will be executed and if the condition is false, then the code inside the else block will be executed. Here is an example based on the above discussion.
Code:
#include <stdio.h>
int main() {
int num = 10;
if (num < 0) {
printf("%d is a negative number.", num);
} else {
printf("%d is a non-negative number.", num);
}
return 0;
}
Output:
Nested If statement
Nested if means another if statement inside an if statement block. Nested if statements are instrumental in programming and applicable in so many cases. We can understand the concept behind nested if statements by continuing our example of rain. If it is raining outside, check if it is raining heavily, then don't go outside; otherwise, wear a raincoat, and if it is not raining, wear a hat. Let us convert these statements into a programming construct.
//demonstration of nested if
if (raining=true){
if (raining_heavily=true){
print(“Don’t go outside”)
}
else{
print(“wear a raincoat”)
}
}
else{
print(“wear a hat”)
}
The above example explains the idea behind nested if. Let us see a program based on a nested if statement.
Code:
#include <stdio.h>
int main() {
int num = 10;
if (num > 0) {
if (num < 100) {
printf("%d is a positive number less than 100.", num);
} else {
printf("%d is a positive number greater than or equal to 100.", num);
}
} else {
printf("%d is not a positive number.", num);
}
return 0;
}
Output:
If else if ladder
As the name suggests, these statements form a ladder. We can check conditions only once in an if statement. We can achieve this using the if else if ladder if we want to check multiple conditions. Let us see an example of implementing the if else if ladder.
Code:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0) {
printf("Number is negative.\n");
}
else if (num == 0) {
printf("Number is zero.\n");
}
else {
printf("Number is positive.\n");
}
return 0;
}
Output:
Switch Statement
In C, the switch statement is a type of decision-making statement that checks for the equality of a particular value on a list of values. On getting a match, it executes the respective code block. Let us see an example to understand it better.
Code:
#include <stdio.h>
int main() {
int day;
printf("Enter a number between 1 to 7: ");
scanf("%d", &day);
switch(day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid input\n");
}
return 0;
}
Output:
Conclusion
Decision-making or conditional statements are essential pillars of programming in any language. C language provides a variety of statements to achieve decision-making.