What are Decision Making Statements in C?
A Decision Making Statement in C controls the program's flow based on certain conditions. These statements are called conditional statements because they evaluate conditions using boolean expressions (true or false). Different blocks of code are executed depending on whether the condition is evaluated as true or false.
Need for Decision-Making Statements
Conditional statements are essential in programming as they allow decisions based on specific conditions similar to real-life decision-making. These statements help determine which block of code to execute next. For example, an "if" statement checks a condition, and if it’s true, a particular action is taken; otherwise, an "else" block executes a different action. Moreover, multiple conditions can be handled using "else-if" statements, enabling more complex decision-making. This flexibility is crucial for controlling the flow of a program, ensuring it responds appropriately to different inputs or situations.
The decision making statements are required for the following reasons such as:
1. Branching
Branching allows the program to make decisions by taking different execution paths depending on conditions. In many programming languages, these statements evaluate expressions and execute code blocks based on whether the condition is true or false, such as if, else, and elif.
2. Control Flow
Control flow manages the sequence in which different program parts are executed. It determines how statements, functions, and loops are processed. By using conditionals (like if statements) and control structures (like loops and switches), you can dictate the program's behaviour and execution order.
3. Handling Different Cases
Conditional statements enable your program to handle different cases efficiently. For example, checking for multiple conditions with if-elif-else blocks allows you to differentiate between scenarios such as error handling, edge cases, or unique user inputs and then execute the appropriate action for each case.
4. Event-Driven Programming
In event-driven programming, actions are triggered by events (such as user input, button clicks, or data changes). Conditional statements are crucial for defining how the program responds to each event, ensuring the correct logic is applied depending on the event type. This allows for dynamic, responsive behaviours based on user interaction or system changes.
Types of Decision-making Statements in C
Here are the types of decision-making statements in C:
1. if statement
2. switch statement
3. conditional operator statement (? : operator)
4. Jump Statements:
- Break
- Continue
- Return
- goto statement
Decision-making with if statement
The if statement may be implemented in different forms depending on the complexity of
the conditions to be tested. The different forms are,
1. if Statement
The if statement is one of the simplest decision-making in C. It checks a condition and executes a block of code if the condition is true.
Syntax
if (condition) {
// Code executes if the condition is true
}
Flowchart
Example
#include <stdio.h>
int main() {
int x = 10;
if (x > 5) {
printf("x is greater than 5");
}
return 0;
}
Output
a is greater than 5
Explanation
The program declares an integer x with a value of 10. It then checks if a is greater than 5 using an if statement. Since the condition is true, it prints "x is greater than 5" to the screen.
2. if-else Statement
The if-else statement provides an alternative block of code that runs if the condition is false.
Syntax
if (condition) {
// code executes if condition is true
} else {
// code executes if condition is false
}
Flowchart
Example
#include <stdio.h>
int main() {
int x = 2;
if (x > 5) {
printf("x is greater than 5\n");
} else {
printf("x is less than or equal to 5\n");
}
return 0;
}
Output
x is less than or equal to 5
Explanation
- The program checks if x (which is 2) is greater than 5 using the if statement. Since the condition x > 5 is false, it moves to the else block.
- The message "x is less than or equal to 5" is printed, as x is indeed less than or equal to 5.
3. Nested if Statement
An if statement inside another if statement. This is used when multiple conditions need to be checked in sequence.
Syntax
if (condition1) {
if (condition2) {
// code executes if both conditions are true
}
}
Flowchart
Example
#include <stdio.h>
int main() {
int x = 10, y = 5;
if (x > 5) {
if (y < 10) {
printf("Both conditions are true\n");
}
}
return 0;
}
Output
Both conditions are true
Explanation
- The outer if checks if x is greater than 5 (which it is).
- The inner if checks if y is less than 10 (which it is as well).
- Since both conditions are true, the message "Both conditions are true" is printed
Note: You can use one if or else-if statement inside another if or else-if statement(s).
Example
#include <stdio.h>
int main() {
int a = 10, b = 5, c = 7;
if (a > b) { // Check if a is greater than b
if (c > b) { // Nested condition: if c is greater than b
printf("a > b and c > b\n"); // Executes if both conditions are true
} else { // If c is not greater than b
printf("a > b but c <= b\n"); // Executes when a > b and c <= b
}
} else if (a == b) { // Executes if a is equal to b
printf("a is equal to b\n");
} else { // Executes if a < b
printf("a < b\n");
}
return 0;
}
Output
a > b and c > b
Explanation
- The program first checks if a is more greater than b. The condition is true since a = 10 and b = 5, so the program enters the first if block.
- Inside the first if, the program checks if c is greater than b. Since c = 7 and b = 5, this condition is also true, so it prints "a > b and c > b".
- The else if (a == b) and else blocks are skipped because the first if block was executed. Only the nested condition inside the first it was true.
4. if-else-if Ladder
An if-else-if ladder is used to evaluate multiple conditions sequentially. It allows you to check several conditions one by one and execute different blocks of code depending on which condition is true.
Syntax
if (condition1) {
// code executes if condition1 is true
} else if (condition2) {
// code executes if condition2 is true
} else {
// code executes if no conditions are true
}
Flowchart
Example
#include <stdio.h>
int main() {
int x = 15;
if (x > 20) {
printf("x is greater than 20\n");
} else if (x > 10) {
printf("x is greater than 10 but less than or equal to 20\n");
} else {
printf("x is less than or equal to 10\n");
}
return 0;
}
Output
x is greater than 10 but less than or equal to 20
Explanation
In the above program, the first condition is false (x > 20), but the second condition is true (x > 10), so the corresponding message is printed.
Note: The nested if is used for hierarchical conditions (if condition inside another if) and if-else if ladder is for checking multiple conditions.
5. switch Statement
The switch statement allows multi-way branching. It checks a variable against multiple possible values and executes the corresponding code block.
Syntax
switch (variable) {
case value1:
// code executes if variable == value1
break;
case value2:
// code executes if variable == value2
break;
default:
// code executes if variable doesn't match any case
}
Flowchart
Example
#include <stdio.h>
int main() {
int x = 2;
switch (x) {
case 1:
printf("x is 1\n");
break;
case 2:
printf("x is 2\n");
break;
default:
printf("x is something else\n");
break;
}
return 0;
}
Output
x is 2
Explanation
- The switch statement compares x (which is 2) with the case values.
- Since x matches case 2, it prints "x is 2".
- The break statement ensures that the program exits the switch after printing the result.
6. Conditional Operator (? :)
The conditional (ternary) operator is a shorthand for if-else statements, with three operands: condition, true-case expression, and false-case expression.
Syntax
condition ? expression1 : expression2;
Flowchart
Example
#include <stdio.h>
int main() {
int x = 10;
(x > 5) ? printf("x is greater than 5\n") : printf("x is less than or equal to 5\n");
return 0;
}
Output
x is greater than 5
Explanation
- The conditional (ternary) operator? : signifies an if-else statement.
- The condition (x > 5) is true since x = 10. Therefore, the expression printf("x is greater than 5\n") executes.
- If the condition is false, it would have executed the other part.
7. Jump Statements
Jump statements control the flow of execution in loops and functions. The jump statements are further divided into four types such as:
1. Break
Terminates the loop or switch statement.
Syntax
break;
Flowchart
Example
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
if (i == 3) break;
printf("%d ", i);
}
return 0;
}
Output
0 1 2
Explanation
The break exits the loop when i equals 3, so only 0, 1, and 2 are printed.
2. Continue
This code skips the current loop iteration and moves on to the next iteration.
Syntax
continue;
Flowchart
Example
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
if (i == 3) continue;
printf("%d ", i);
}
return 0;
}
Output
0 1 2 4
Explanation
In the above program, the continue skips the iteration when i equals 3, so 3 is not printed.
3. Goto
Transfers control to a labelled statement. (Not recommended due to readability concerns.)
Syntax
goto label;
Flowchart
Example
#include <stdio.h>
int main() {
int i = 0;
start:
if (i < 3) {
printf("%d ", i);
i++;
goto start;
}
return 0;
}
Output
0 1 2
Explanation
The goto sends control back to the start label, creating a loop that prints 0, 1, and 2.
4. Return
Exits from a function, optionally returning a value.
Syntax
return value;
Example
#include <stdio.h>
int main() {
printf("Before return\n");
return 0;
printf("After return\n"); // This line will not execute
}
Output
Before return
Explanation
The return statement exits the primary function, so the second print statement is never executed.
Popular Questions Asked on Decision-Making Statements in C
Candidates can prepare these questions on decision-making statements in C:
Problem 1: Write a C program to check whether a given number is even or odd.
Code
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}
return 0;
}
Output
Enter a number: 7
The number is odd.
Explanation
- The program uses an if statement to check if the number is divisible by 2.
- If num % 2 == 0, it prints "The number is even."
- Otherwise, it prints "The number is odd."
Problem 2: Write a C program to find the largest of three numbers.
Code
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c) {
printf("The largest number is %d\n", a);
} else if (b >= a && b >= c) {
printf("The largest number is %d\n", b);
} else {
printf("The largest number is %d\n", c);
}
return 0;
}
Output
Enter three numbers: 5 8 3
The largest number is 8
Explanation
- This program compares three numbers using if and else if statements.
- It checks which number is greater and prints that number as the largest.
Problem 3: Write a program to check whether a given year is a leap year.
Code
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}
Output
Enter a year: 2024
2024 is a leap year.
Explanation
- The if condition checks whether the year is divisible by 4 but not 100, or divisible by 400.
- This is the rule for determining leap years.
Problem 4: Write a program to input a student's marks and display the grade based on the marks.
Code
#include <stdio.h>
int main() {
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
if (marks >= 90) {
printf("Grade A\n");
} else if (marks >= 70) {
printf("Grade B\n");
} else if (marks >= 50) {
printf("Grade C\n");
} else {
printf("Grade F\n");
}
return 0;
}
Output
Enter marks: 85
Grade B
Explanation
- This program uses multiple else if statements to determine the grade based on the marks.
- If the marks are greater than or equal to 90, the grade is "A", and so on.
Advantages and Disadvantages of Decision Making Statements in C
Here are the advantages and disadvantages of decision-making statements in C programming:
Advantages
- Flexibility: Decision-making statements allow you to modify the program’s behaviour based on different conditions, making the program adaptable to various situations.
- Control Over Program Flow: They give you better control over how the program moves between different blocks of code, leading to a more organised structure.
- Improved Readability: When used correctly, decision-making statements can make the code more transparent and easier to understand, especially with descriptive variable names and proper formatting.
- Error Handling: These statements help manage errors or specific conditions, allowing the program to react appropriately in unexpected scenarios.
- Breaking Down Complex Problems: They help break a complex problem into smaller, more manageable tasks by evaluating specific conditions at each step.
- Adaptability: If the program requirements change, decision-making statements allow you to quickly adjust the logic to accommodate new inputs or conditions.
Disadvantages
- Increased Complexity: Excessive use of decision-making statements, especially nested ones, can make the code difficult to understand and maintain.
- Risk of Bugs: If conditions are not thoroughly tested or set up incorrectly, they can introduce errors or lead to unintended behaviour in the program.
- Maintenance Challenges: Maintaining and updating decision-making logic can become difficult as the codebase grows, especially with complex or nested conditions.
- Potential Performance Issues: Evaluating many conditions can slow down the program, particularly in loops or data-heavy sections.
- Logical Errors: Incorrectly constructed conditions can lead to logical mistakes, which cause the program to behave in ways that were not intended.
- Redundancy: Repeating similar conditions across the code can lead to unnecessary duplication, making the code more complicated to maintain.
Conclusion
In conclusion, decision-making statements in C are crucial for controlling the flow of a program. They help execute specific actions based on certain conditions, improving the flexibility and responsiveness of a program. Mastery of these statements is key to writing efficient, logical, and maintainable C code.
Build Industry-Relevant Skills in College for a Successful Tech Career!
Explore ProgramFrequently Asked Questions
1. What are the 3 types of control structures?
The three main types of control structures in C are:
- Sequential: Executes statements in the order they appear.
- Selection: Makes decisions using conditional statements (if, switch).
- Iteration: Repeats a code block using loops (for, while, do-while).
2. What are the four types of control statements?
The four types of control statements in C are:
- Decision-making Statements: if, if-else, else-if, switch
- Looping Statements: for, while, do-while
- Jump Statements: break, continue, goto, return
- Sequential Statements: (Default flow, no specific keywords needed, executes statements one by one)
Practice Questions on Decision Making Statements in C
1. Which is the correct syntax for an if statement in C?
A) if (condition) { statement; }
B) if condition { statement; }
C) if (condition) statement;
D) if condition then statement;
Answer: A) if (condition) { statement; }
2. Which of the following C statements will execute only when the condition is false?
A) if
B) else
C) switch
D) continue
Answer: B) else
3. Which of the following statements is used to exit a switch case in C?
A) exit;
B) continue;
C) break;
D) stop;
Answer: C) break;
4. Which of the following is NOT a valid decision-making statement in C?
A) if
B) else if
C) case
D) while
Answer: D) while (It’s a loop, not a decision-making statement)
5. Which of the following C constructs will allow a block of code to be executed if no condition is true in a switch statement?
A) default
B) case
C) exit
D) break
Answer: A) default