Selection Statements
The selection statements allow to choose the set-of-instructions for execution depending upon an expression's truth value. C++ provides two types of selection statements : if and switch. In addition, in certain circumstances ? operator can be used as an alternative to if statement. The selection statement are also called conditional statements or decision statements.Selection statements |
1. The if Statement of C++
An if statement tests a particular condition; if the condition evaluates to true, a course-of-action is followed i.e., a statement or set-of-statement is executed. Otherwise (if the condition evaluates to false), the course-of-action is ignored. The syntax (general form) of the if statement is as shown below:
if (expression)
statement;
where a statement may consist of a single statement, a compound statement, or nothing (in case of empty statement). The expression must be enclosed, otherwise ignored. For instance, the following code fragment:
if(cg==' ')
spaces++;
checks whether the character variable ch stores a space or not; if it does, the number of spaces are incremented by 1.
Selection statements with if can also specify what happens when the condition is not fulfilled, by using the else keyword to introduce an alternative statement. Its syntax is:
if (condition) statement1 else statement2
where statement1 is executed in case condition is true, and in case it is not, statement2 is executed.
For example:
checks whether the character variable ch stores a space or not; if it does, the number of spaces are incremented by 1.
Selection statements with if can also specify what happens when the condition is not fulfilled, by using the else keyword to introduce an alternative statement. Its syntax is:
if (condition) statement1 else statement2
where statement1 is executed in case condition is true, and in case it is not, statement2 is executed.
For example:
1
2 3 4 |
if (x == 100)
cout
<< "x is 100";
else
cout
<< "x is not
100";
|
This prints x is 100, if indeed x has a value of 100, but if it does not, and only if it does not, it prints x is not 100 instead.
Several if + else structures can be concatenated with the intention of checking a range of values. For example:
1
2 3 4 5 6 |
if (x > 0)
cout
<< "x is
positive";
else if (x < 0)
cout
<< "x is
negative";
else
cout
<< "x is 0";
|
This prints whether x is positive, negative, or zero by concatenating two if-else structures. Again, it would have also been possible to execute more than a single statement per case by grouping them into blocks enclosed in braces: {}.
2. The switch Statement
C++ provides a multiple - branch selection statement known as switch. This selection statement successively tests the value of an expression against a list of integer or character constants. When a match is found, the statements associated with that constant are executed.
The syntax of switch statement is as follows:
switch (expression)
{
case constant1:
group-of-statements-1;
break;
case constant2:
group-of-statements-2;
break;
.
.
.
default:
default-group-of-statements
}
The expression is evaluated and its values are matched against the values of the constants specified in the case statements. When a match is found, the statement sequence associated with that case is executed until the break statement or the end of switch statement is reached. If a case statement does not include a break statement, then the control continues right on the next case statement(s) until either a break is encountered or end of switch is reached. This situation (i.e. missing break in case statement ) is called fall through. The default statement gets executed when no match is found. The default statement gets executed when no match is found. The default statement is optional and, if it is missing, no action takes place if all matches fail.
Note: The ANSI standard specifies that a switch can have up to 257 case statements however, you must always limit the number of case statements to a smaller amount for the sake of efficiency.
A case statement cannot exist by itself, outside of a switch. The break statements used under switch, is one of C++ jump statements. When a break statement is encountered in a switch statement, program execution jumps to the line of code following the switch statement i.e. outside the body of switch statement.
Note: The ANSI standard specifies that a switch can have up to 257 case statements however, you must always limit the number of case statements to a smaller amount for the sake of efficiency.
A case statement cannot exist by itself, outside of a switch. The break statements used under switch, is one of C++ jump statements. When a break statement is encountered in a switch statement, program execution jumps to the line of code following the switch statement i.e. outside the body of switch statement.
The switch v/s if-else
The switch and if-else both are selection statements and they both let you select an alternative out of given many alternatives by testing an expression. However, there are some differences in their operations. These are given below:
- The switch statement differs from the if statement in that switch can only test for equality whereas if can evaluate a relational or logical expression i.e., multiple conditions.
- The switch statement selects its branches by testing the value of same variable ( against a set of constants) whereas the if-else construction lets you use a series of expressions that may involve unrelated variables and complex expressions.
- The if-else is more versatile of the two statements. For instance, if-else can handle ranges whereas switch cannot. Each switch case label must be a single value.
- the if-else statement can handle floating-point tests also apart from handling integer and character tests whereas a switch cannot handle floating point tests. The case labels of switch must be an integer(which includes char also).
- The switch case label value must be a constant. So, if two or more variables are to be compared, use if-else.
You may also like to visit:-
0 Comments