Mar 20, 2023
Mar 20, 2023
by Sachin Mehta
We continue with the remaining decision making statement, switch, conditional operator & the GOTO statement.
The Switch Statement
We have seen that when of many alternatives is to be selected, we can use the if statement. In all cases programs can be designed using only this statement. However, the complexity of such a program increases dramatically when the number of choices increases. The clarity of the program decreases. Fortunately, C has a built-in multiway decision statement known as a switch. The switch statement tests the value of a given variable (or expression) against a list of case values and when a match is found, a block of statements associated with that case is executed. The general form of the switch statement is as shown below:
The expression is an integer expression or characters. value1, value2, .... are constants or constant expressions and are known as case labels. Each of these values should be unique within a switch statement. block1, block2,...... are statement lists and may contain zero or more statements. There is no need to put braces around these blocks. Note that case labels end with a colon .
On execution of the keyword switch, the value of the expression is successively compared against the values value1, value2, ........If the value of the expression for a particular case matches with a case value , then the block of statements that follows the case are executed.
The break statement at the end of each block signals the end of a particular case and causes an exit from the switch statement, transferring the control to the statement-n following the switch.
The default is an optional case. When present, it will be executed if the expression does not match any of the case values. If not present, no action takes place if all matches fail and the control goes to the statement-n.
Consider a common program design in which a menu is provided by the switch statement.
........../* the value is accepted from the user and given to a identifier */
......... /* the data type of the value should match with that of the identifier */
printf (" ADDITION /n");
printf (" SUBTRACTION /n);
The value of variable n is accepted and then tested using the switch statement. Any value other than 1 or 2 will lead to end of the default block.
The ? : Operator
This operator is popularly known as the conditional operator. The general form of the conditional operator is as follows:
conditional expression ? expression1 : expression2
The conditional expression is evaluated first. If the result is nonzero, expression1 is evaluated and is returned as the value of the conditional expression. Otherwise , expression2 is evaluated and its value is returned.
if ( c == 'y' )
f = 1;
f = 0;
can be written as
f = ( c == 'y' ) ? 1 : 0;
Using of the conditional operator makes the code more concise and perhaps, more efficient. However , the readability is poor. It is better to use if statements when more than a single nesting of conditional operator is required.
The GOTO Statement
In all the statements seen one common thing was that, there execution depended on some condition. However we also have statements in which are unconditional. Like many other languages, C supports the GOTO statement to branch unconditionally from one point to another in the program. Although it may not be essential to use the GOTO statement in a highly structured language like C, there may be occasions when the use of GOTO might be desirable.
The GOTO requires a label in order to identify the place where the branch is to be made. A 'label' is placed immediately before the statement where the control is to be transferred. The general forms of GOTO and label statements are shown below:
GOTO label; label: statement;
label: statement; GOTO label;
This label can sometime generate infinite loops which should be avoided. The labels used for GOTO statement do not require any kind of declaration as necessary in PASCAL.
More by : Sachin Mehta