What are Decision Making Statements in C?
At times, it so happens that the computer may not execute all statements while solving problems. Some statements may be executed in a situation, while they may not be executed in another situation. The computer has to take the required decision in this respect.
For this, we have to provide appropriate conditions which will be evaluated by the computer. It will then make a decision on the basis of the result. The decision will be in the form of selecting a particular statement for execution or skipping some statement from being executed.
The statements provided by C++ for the selected execution are called decision-making statements or selection statements. if and switch is the two types of selection statements in C++.
Types of Decision Making Statements in C
These are the types of decision making statements in c which given below:
- If Statement in C
- If Else Statement in C
- Nested If in C
- The Else If Ladder
- Switch Statement in C
- Conditional Operator in C
If Statement in C
The if statement is used to select a set of statements for execution based on a condition. In C++, conditions (otherwise known as test expressions) are provided by relational or logical expressions. The syntax (general form) of the if statement is as follows:
Here the test expression represents a condition that is either a relational expression or logical expression. If the test expression evaluates to True (non-zero value), a statement or a block of statements associated with if is executed. Otherwise, the control moves to the statement following the if construct.
The figure shows the mode of execution of if statement. While using if, certain points are to be remembered.
The following is a sample output of program 7.1:
Enter your score: 25
You have passed
In Program 7.1, the score of a student is entered and stored in the variable score. The test expression compares the value of score with 18. The body of if will be executed only if the test expression evaluates to True.
That means, when the score is greater than or equal to 18, the output You have Passed will be displayed on the screen. Otherwise, there will be no output.
Note that the statement block associated with if is written after a tab space. We call it indentation. This is a style of coding which enhances the readability of the source code. Indentation helps the debugging process greatly. But it has no impact on the execution of the program.
Consider the following C++ program segment. It checks whether a given character is an alphabet or a digit.
If Else Statement in C
Consider the if statement in Program 7.1:
if (score >= 18)
cout << “You have passed”;
Here, the output is obtained only if the score is greater than or equal to 18. What will happen if the score entered is less than 18? It is clear that there will be no output. Actually, we don’t have the option of selecting another set of statements if the test expression evaluates to False.
If we want to execute some actions when the condition becomes False, we introduce another form of if statement, If Else Statement in C The syntax is:
If the test expression evaluates to True, only the statement block 1 is executed. If the test expression evaluates to False statement block 2 is executed. The flowchart shown in Figure explains the execution of if else statement in c.
The following code segment illustrates the working of if…else statement.
Let us write a program to input the heights of two students and find the taller.
Nested If in C
In some situations there may arise the need to take a decision within if block. When we write an if statement inside another if block, it is called nesting. Nested means one inside another. Consider the following program segment:
In this code fragment, if the value of score is greater than or equal to 60, the flow of control enters the statement block of outer if. Then the test expression of the inner if is evaluated (i.e. whether the value of age is greater than or equal to 18).
If it is evaluated to True, the code displays the message, “You are selected for the course!”. Then the program continues to execute the statement following the outer if statement. An if statement, inside another if statement is termed as a nested if statement.
The following is an expanded form of nested if.
The important point to remember about nested if is that an else statement always refers to the nearest if statement within the same block. Let us discuss this case with an example. Consider the following program segment:
We know that this is logically not correct. Though the indentation of the code is proper, that doesn’t matter in execution. The second if statement will not be considered as nested if, rather it is counted as an independent if with an else block.
So, when the first if statement is executed, the if block is selected for execution since the test expression is evaluated to True.
It causes the first line in the output. After that, while considering the second if statement, the test expression is evaluated to False and hence the second line in the output is obtained. So to get the correct output, the code should be modified as follows:
If we input the same value 45 as in the case of previous example, the output will be as follows:
You have passed
Program 7.4 uses nested if to find the largest among three given numbers. In this program, if statement is used in both the if block and else block.
The Else If Ladder
There are situations where an if statement is used within an else block. It is used in programs when multiple branching is required. Different conditions will be given and each condition will decide which statement is to be selected for execution.
Common programming construct based on if statement is the else if ladder, also referred to as the else if staircase because of its appearance. It is also known as if…else if statement. The general form of else if ladder is:
At first, test expression 1 is evaluated and if it is true, statement block 1 is executed and the control comes out of the ladder. That means the rest of the ladder is bypassed. If test expression 1 evaluates to False, then test expression 2 is evaluated, and so on.
If any one of the test expressions evaluates to True, the corresponding statement block is executed and control comes out of the ladder. If all the test expressions are evaluated to False, the statement block n after the final else is executed.
Observe the indentation provided in the syntax and follow this style to use else if ladder. Let us illustrate the working of the else if ladder by using a program to find the grade of a student in a subject when the score out of 100 is given. The grade is found out by the criteria given in the following table:
Switch Statement in C
We have seen the concept of multiple branching with the help of else if ladder. Some of these programs can be written using another construct of C++ known as switch statement. This selection statement successively tests the value of a variable or an expression against a list of integers or character constants.
The syntax of the switch statement is as follows:
In the syntax switch, case, break and default are keywords. The expression is evaluated to get an integer or character constant and it is matched against the constants specified in the case statements. When a match is found, the statement block associated with that case is executed until the break statement or the end of switch statement is reached.
If no match is found, the statements in the default block get executed. The default statement is optional and if it is missing, no action takes place when all matches fail. The break statement, used inside switch, is one of the jump statements in C++.
When a break statement is encountered, the program control goes to the statements following the switch statement. We will discuss the break statement in detail in Section 7.3.2. Program 7.7 can be written using switch statement. It enhances the readability and effectiveness of the code. Observe the modification in Program 7.8.
Conditional Operator in C
As we mentioned in Chapter 6, C++ has a ternary operator. It is the conditional operator (?:) consisting of the symbols ? and : (a question mark and a colon). It requires three operands to operate upon. It can be used as an alternative to if…else statement. Its general form is:
Test expression ? True_case code : False_case code;
Test expression can be any relational or logical expression and True_case code and False_case code can be constants, variables, expressions or statement. The operation performed by this operator is shown below with the help of an if statement.
The conditional operator works in the same way as if…else works. It evaluates the test expression and if it is true, the True_case code is executed. Otherwise, False_case code is executed. Program 7.10 illustrates the working of conditional operator.