Decision-making statements are crucial in C and C++ programming, and many developers write inefficient code without knowing it. By using these statements, you can control program flow with if-else, switch, and the ternary operator. But are you using the best one? Improper use of nested if statements, redundant conditions, or handling false in switch cases may slow down your code. Are you making these common mistakes? This article breaks down the decision-making statements in C/C++, with syntax, examples, and best practices to help you write cleaner and efficient code.
Table of Contents:
What are Decision-Making Statements in C/C++?
Decision-making is an integral part of the code. Based on the specific conditions, it allows the execution of different blocks of code. We use decision-making statements in the program to control the flow based on logical conditions. Decision-making statements, also known as conditional statements, are based on the conditions that control the flow of the program. Mostly, the conditions are typically in Boolean expressions, and the code gets executed only if the condition is true; Otherwise, it does not.
For example, if we want to check if a Number is Positive, Negative, or zero. For this type of scenario, we need to use the decision-making statements in C/C++.
Use of Decision-Making Statements in C/C++
Here are the real-life scenarios where conditional statements are used in C/C++:
- Loop Control: Conditional statements are widely used within loops to control the execution.
- Error Handling: Conditional statements help in handling errors or unexpected conditions.
- Authentication: The conditional statements are widely used in authentication systems to compare the user’s input against the stored credentials, to determine whether to grant or deny access.
- Control Flow: By using decision-making statements, we can easily control the flow of execution in a program.
Types of Decision-Making Statements in C/C++
Both C and C++ provide several types of decision-making statements. Let’s discuss them in a detailed way:
- if Statement
- if-else Statement
- if-else-if Statement
- Nested if Statement
- Switch Statement
- Conditional (Ternary) Operator
- Jump Statements in C/C++
1. if Statement in C/C++
In C and C++, the if statement is a fundamental decision-making statement that allows the program to execute a block of code only if the specified condition is true. And it skips the code if the condition is false.
Syntax:
if (condition) {
// Code to execute if the condition is true
}
If statement works as follows:
- If the condition is true, then the statement inside the {} block is executed.
- If the condition is false, then the statement inside the {} block is avoided.
Example of an if statement in C:
Output:
In C, the if statement checks if a condition is true or not; if it is true, it executes the block of code, otherwise, it skips the code. It takes the input using scanf(), and using an if statement, it checks whether the number is greater than zero. If it is true, it prints “The number is positive: using printf(). If the condition is false, the statement skips the blocks and continues the next execution.
Example of an if statement in C++:
Output:
In C++, the if statement works the same way as in C, using cin and cout. It takes the inputs and outputs. It first checks the condition. If it is true, it executes the block and prints the output. If the statement is false, it skips the block and executes the next statement.
2. if-else Statement in C/C++
When you need to execute one block of code, the if-else statement is used :
- If the condition is true, then the program executes the first block.
- If the condition is false, the first block gets skipped, and the else block is executed.
Syntax:
if (condition) {
// Code executes if a condition is true
} else {
// Code executes if a condition is false
}
- If the statement is true, it executes the if block.
- If the statement is false, it executes the else block.
Example of an if-else statement in C:
Output:
In C, the if-else statement checks whether the number is positive or negative. If the value is positive, it executes the if statement. If the value is negative, it executes the else statement.
Example of an if-else statement in C++:
Output:
In C++, the if-else statement checks whether the number is positive or negative; if the value is positive, it executes the if statement. Otherwise, it executes the else statement. Later, it prints the statements, using cout.
3. if-else-if Statement in C/C++
You can check multiple conditions sequentially by using an if-else-if statement. The first if condition is the beginning of the evaluation. If the condition is found to be True, the statement is executed, and the rest of the statements are skipped. If the condition is false, it moves to the next else-if condition and keeps checking until it gets a true condition. The last else block is used as the default if none of the conditions are true. This method works efficiently with anything that has binary possibilities, like grading systems, voting systems, etc.
Syntax:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else if (condition3) {
// Code to execute if condition3 is true
} else {
// Code to execute if none of the conditions are true
}
- If condition1 is true, it executes the block and skips the rest.
- If the condition is false, it immediately checks the condition2
- If no conditions are true, by default, it executes the else block.
Example of an if-else-if statement in C:
Output:
In C, the above program uses scanf() for input and checks the range using an if-else-if statement. If the marks are above 90, it prints “Grade A’, if between 80-89 it prints “Grade B”. And if the marks are below 60, it prints “Grade F”. The else block works as a default if the marks are less than 60.
Example of an if-else-if statement in C++:
Output:
The above programs work the same as C but use cin and cout for input and output. It follows the same method as checking the conditions sequentially and printing the grade.
4. Nested if Statement in C/C++
An if statement inside another if statement is called a nested if statement, and it is used for checking multiple related conditions. First, the outer if condition is checked, and in case of false, the inner if will be skipped. If the outer condition is true, check the inner if condition, and if it is true, execute the inner if block. This is useful in structures like user authentication (where you first check the username, and then you check the password) or eligibility checks (where you first check whether the person is of age, and then check all others).
Syntax:
if (condition1) {
// Code executes if condition1 is true
if (condition2) {
// Code executes if both condition1 and condition2 are true
}
}
- It first checks the condition1.
- If condition1 is true, it checks condition2.
- If condition1 is false, it automatically skips the inner if.
Example of a nested-if statement in C:
Output:
First, the program requests user input for both age and citizenship. In the outer ‘if’, we check whether the user is 18 or older. Otherwise, it prints, “You must be at least 18 years old to vote,” and exits. Inside the first if, if the age condition matches, it checks whether the user is a citizen (Y or y). If so, it prints “You are eligible to vote, else it prints “You must be a citizen to vote”.
Example of a nested-if statement in C++:
Output:
The C++ program uses cin and cout for input and output. Inside the first if, if the age condition matches, it checks whether the user is a citizen (Y or y). If so, it prints “You are eligible to vote, else it prints “You must be a citizen to vote.”
5. Switch Statement in C/C++
The switch statement checks a value and executes the matching case by avoiding multiple if-else statements. Here, each case runs a particular code, and a break has been used so that the program execution is not transmitted to the next statement. If none of the conditions match, then the default block will be executed.
Syntax:
switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
case value3:
// Code to execute if expression == value3
break;
default;
// Code to execute if no cases match
}
- The expression is evaluated only if the matching case executes.
- The break statement prevents the execution from falling into the next case.
- If no matches are found, then the default case is executed.
Example of a switch statement in C:
Output:
Depending on the operator (+, -, *, /), the switch statement operates. In the default case, an error is outputted if an invalid operator is given. For division, the program checks that the divisor is zero before attempting the operation to avoid errors. The break statement ensures that only the matching case executes.
Example of a switch statement in C++:
Output:
The C++ program executed the program same way as the previous one, but uses cin and cout for input and output. Based on the user input, the switch statement selects the correct arithmetic operation. If an invalid operator is entered, the default case manages it.
6. Conditional (Ternary) Operator in C/C++
The conditional(ternary) operator is similar to the if-else statement. Is it written as a condition? value_if_true : value_if_false. Depending on the single line, these conditional operators are used for executing the different lines of code, they return two values based on condition is true or false.
Syntax:
variable = (condition)? value_if_true : value_if_false;
- The condition evaluates the expression.
- Value_if_true assigns the values if the condition is true
- Value_if_true assigns the values if the condition is false.
Example of a conditional statement in C:
Output:
The above program takes the two numbers, using the ternary operator, and checks if num1 is greater than num2. If it is greater than num1 is assigned to max; otherwise, num2 is assigned. By using the printf(), the largest number is printed.
Example of a conditional statement in C++:
Output:
The above works the same as the previous; The program takes the two numbers, using the ternary operator, and checks if num1 is greater than num2. If it is greater than num1, it is assigned to max; Otherwise, num2 is assigned. By using the cout, the largest number is printed.
7. Jump Statements in C/C++
Jump statements are used to change the flow of execution in the code by transferring control from one part of the code to another. We can skip iterations, exit the loops, or jump to certain points using these statements. The jump statements, supported by C and C++, are:
- break: Exits from the nearest loop/switch statement immediately.
- continue: Skips the current iteration of a loop and goes to the next iteration.
- goto: Transfers control to a labeled statement elsewhere in the program.
- return: Exit a function and optionally return a value (in functions).
What is Semantic Equivalence?
Semantic equivalence means that the same output or the same operation is performed by two programs. This approach is highly effective when it comes to code refactoring, code optimizations, and teaching programming concepts.
The most common control statements that can be interchanged are:
- if-else and switch
- for and while, and do-while
- loop and recursion
Using if-else
The code below represents the usage of if-else:
Example:
Output:
This is a program that checks the day and prints the weekday with if-else-if, where day = 2 means Tuesday.
Using switch
The code below represents the usage of the switch:
Example:
Output:
This is a program that uses a switch to check the day and prints the weekday, where day = 2 means Tuesday.
Conclusion:
In C and C++, based on the conditions, decision-making statements are very important for controlling the flow of a program. These statements include if, if-else, switch, and ternary operators, which help to execute a specific block of code in a program. Also, the jump statements are essential while altering the execution flow. By mastering the concept of decision-making statements, programmers can easily implement logical statements like loops and error-handling. These decision-making statements enhance readability and efficiency.
You can learn more about C++ in the C++ article, and also explore C++ Interview Questions prepared by industry experts.
Decision-Making Statements in C/C++ – FAQs