Sometimes, we have multiple conditions and take some action according to each condition. For example, we want to print description of the grade of a student. If the student has grade ‘A’ we print ‘Excellent’ and 'Very good', 'good', 'poor' and 'fail' for grades B, C, D, and F respectively. Now we have to see how this multi-condition situation can be applied in a program. We have a tool for decision making i.e. 'if statement'. We can use 'if statement' to decide what description for a grade should be displayed. But in such a situation where we have multiple conditionas the C language provides us a stand-alone construct to handle these instances. This construct is switch structure. The switch structure is a multiple-selection construct that is used in such cases (multi way decisions) to make the code more efficient and easy to read and understand. The syntax of switch statement is as follows:
switch ( variable/expression
In the switch statement, there should be an integer variable (char is also allowed) or an expression which must evaluate to an integer type (whole numbers only, the decimal numbers 2.5, 14.3 etc are not allowed). We can’t use compound conditions (i.e. the conditions that use logical operators && or ||) in switch statement and in case statements. The constants also must be integer constants (which include char). We can’t use a variable name with the case key word. The default statement is optional. If there is no case which matches the value of the switch statement, then the statements of default are executed. The switch statement takes the value of the variable, if there is an expression then it evaluates the expression and after that looks for its value among the case constants. If the value is found among the constants listed in cases, the statements in that statementList are executed. Otherwise, it does nothing. However if there is a default (which is optional), the statements of default are executed. Thus our previous grade example will be written in switch statement as below:
switch ( grade )
We know that C language is 'case sensitive'. In this language, ‘A’ is different from ‘a’. Every character has a numeric value which is stored by the computer.. The numeric value of a character is known as ASCII code of the character. The ASCII code of small letters (a, b, c etc ) are different from ASCII code of capital letters (A, B, C etc). We can use characters in switch statement as the characters are represented as whole numbers inside the computers. Now we will see how the use of ' the letter a' instead of 'A' can affect our program. We want our program to be user- friendly. We don’t want to restrict the user to enter the grade in capital letters only. So we have to handle both small and capital letters in our program. Here comes the limitations of switch statement. We can’t say in our statement like:
case ‘A’ or ‘a’ : statements ;
We have to make two separate cases so we write:
case ‘A” :
In the switch statement, the cases fall through the case which is true. All the statements after that case will be executed right down to the end of the switch statement. This is very important to understand it. Let's suppose that the user enters grade ‘B’. Now the case ‘A’ is skipped. Next case ‘B’ matches and statement cout << “Very Good” ; is executed. After that, all the statements will be executed. So cout << “Good” ; cout << “Poor” ;and cout << “Fail” ; will be executed after one another. We don’t want this to happen. We want that when a case matches, then after executing its statement, the control should jump out of the switch statement leaving the other cases. For this purpose we use a key word break.
The break statement interrupts the flow of control. We have seen in switch statement that when a true case is found, the flow of control goes through every statement downwards. We want that only the statements of true case should be executed and the remaining should be skipped. For this purpose, we use the break statement. We write the break statement after the statements of a case. Thus, when a true case is found and its statements are executed then the break statement interrupts the flow of control and the control jumps out of the switch statement. The break statement is necessary in switch structure, without it the switch structure becomes illogic. As without it all the statement will execute after first match case is found.
The above code does nothing if the grade is other than these five categories (i.e. A, B, C, D and F). To handle all the possibilities of grade input, we write a default statement after the last case. The statement in this default case is executed if no case matches the grade. So in our program, we can write the default statement after the last case as under.
default : cout << “Please enter grade from A to D or F ” ;
The break statement is also used in decision structures other than switch structure. We have seen that in while, do-while and for loops, we have to violate some condition explicitly to terminate the loop before its complete repetitions. In these loops, we can use the break statement to exit a loop. When a break statement is encountered in a loop, the loop terminates immediately. The control exits the inner most loop if there are nested loops. The control passes to the statement after the loop.
/*This program gets a grade
from user and displays a description accordingly*/
Please enter the student’s
grade : b
There is another statement relating to loops. This is the continue statement. Sometimes we have a lot of code in the body of a loop. The early part of this code is common that is to be executed every time (i.e. in every iteration of loop) and the remaining portion is to be executed in certain cases and may not be executed in other cases. But the loop should be continuous. For this purpose, we use the continue statement. Like the break statement, the continue statement is written in a single line. We write it as continue ; The continue forces the immediate next iteration of the loop. So the statements of the loop body after continue are not executed. The loop starts from the next iteration when a continue statement is encountered in the body of a loop.
There is a statement in the computer languages COBOL, FORTRON and C. This statement is goto statement. The goto is an unconditional branch of execution. The goto statement is used to jump the control anywhere (back and forth) in a program.
The goto statement is defined just for the sake of completeness and its
use is not recommended. Because in such programs, where to goto statement
is used, it is very difficult, for a programmer, to keep the track of
execution as the control jumps from one place to the other and from there
to anywhere else. We call this kind of traditional code as spagatti code.
It is very difficult to trace out the way of execution and figure out
what the program is doing. And debugging and modifying such programs is
very difficult. When structured programming was started, it was urged
not to use the goto statement. Though goto is there in C language but
we will not use it in our programs. We will adopt the structured approach.
All of our programs will consist of sequences, decisions and