The switch case in C is a multi-way decision-making statement which selects one of the several alternatives based on a set of fixed values for a given expression. The switch case is mainly used to replace multiple if-else
statements. The use of numerous if-else
statements causes performance degradation as several conditions need to be evaluated before a particular condition is satisfied. The general form of the switch statement is
switch(expression) { case constant1 : statement(s); [break;] case constant2 : statement{s); [break;] default : statement(s) ; }
The switch case in C starts with a keyword switch followed by the expression enclosed in the parentheses. The expression must evaluate to an integer value of type char
, short
, int
or an enumeration
is constant but not of type long
, float
or double
. constant1
, constant2
, ….. are the constants or constant expression that evaluates to integral constant and are known as case labels. Some examples are 5
, 'A'
, SHIRTSIZE (a named constant of type int). No two constants in the same switch can have identical values.
When the switch statement is executed then the expression in the parentheses following the keyword switch is compared with each of the constants in the order of their appearance in the switch statement. If a match occurs then the statement(s) corresponding to that case are executed. If no match occurs with any of the case then only the statement(s) following the default case are executed. The default part is optional in the switch statement. In that case, if none of the case values match, then no action takes place.
Each case can contain zero, one or more program statements. Unlike other control structures, the block of statements corresponding to a case is not required to be enclosed in curly braces.
A case is normally terminated with a break statement. When a break statement is encountered, the program comes out of the switch statement and continues with the next executable statement following the switch statement. If the break statement is not used then all the statements from the matched case in the sequence will be executed until another break statement is found or end of the switch statement is reached.
Now consider the example of switch-case statement.
#include<stdio.h> #include<conio.h> void main() { int day; printf ("Enter the day of the week (1 to 7) "); scanf ("%d", &day); switch(day) { case 1 : printf ("Today is SUNDAY");break; case 2 : printf ("Today is MONDAY");break; case 3 : printf("Today is TUESDAY");break; case 4 : printf("Today is WEDNESDAY");break; case 5 : printf("Today is THURSDAY");break; case 6 : printf ("Today is FRIDAY");break; case 7 : printf("Today is SATURDAY");break; default : printf("Enter a valid choice(l to 7 only)"); } getch(); }
Explanation: On execution, the user is prompted to enter the day of the week (say 4). When the program encounters the switch statement, the value of the variable named day is 4. IT starts comparing this value with the constants appearing in the case labels starting from the first one. Since the value 4 matches the integer constant 4 in the third case of the switch statement. Therefore, the associated print () statement is executed, which prints the message. Today is WEDNESDAY. A break statement is encountered next that causes the program to exit the switch statement. Finally, exit the program.
We’ll be covering the following topics in this tutorial:
switch v/s else-if ladder
The switch case is similar to the else-if ladder
as it checks several conditions. It checks the first condition, and if that condition is true
, the statements under that condition are executed. The control is shifted to the statement after the switch statement, and if the condition is false, then the next condition is checked.
The switch case differs from the else-if ladder also. The expression used in else-if ladder returns (true (1) or false (0)) whereas the expression used in a switch statement can return an integer or a character constant where the values 1 for true and 0 for false are also included. Moreover, unlike the else-if ladder, the switch statement does not allow the use of && and || logical operators. In a switch statement, cases can be written in any order, but in the else-if ladder, the order is fixed.
To sum up, the else-if ladder is an effective approach when the number of conditions to be checked less. When a large number of conditions are involved, the switch is preferred.
Some key differences between switch and else-if ladder are as follows :
• switch is better because it is a better way of writing programs as compared to else-if and leads to more structured programming. Thus switch is more simple and manageable.
• switch does not allow the use of logical AND (&&)
and logical OR(||)
operators.
• The block of statements corresponding to a case is not required to be enclosed in curly braces. But in else-if ladder use of compound statements require braces.
• In a switch statement, a case is normally terminated with a break statement. It is not there in else if ladder.
The switch has an optional default case. It is not there in the else-if ladder.
The expression in the switch statement can result in constant of type char, int but not of type float and double. It is not in case of the else-if ladder.
Application of Switch Case
The switch statement is convenient to be used if there are a large number of alternative paths. It is particularly used in menu-driven programs. It is efficient and faster than if statement. But it can only be used in case of equality (= =)
operator but not with other relational operators and it cannot be used with multiple variables.
The GOTO Statement
The goto
statement is an unconditional control transfer statement that causes the control to jump to a different location in the program without checking any condition as specified by the goto
. It is normally used to alter the normal sequence of program execution by transferring control to some other part of the program Like switch, break and continue statements, the goto
statement also causes the control to jump over the statements. So it is also called a Control Transfer statement.
It is generally written as :
goto label ;
Here, the label is a valid identifier used to label the target statement to which control will be transferred. Control may be transferred to any other statement within the current function. You cannot jump between functions. The target statement must be followed by a colon (:)
. It is written as,
label: statement
The label can be set anywhere in the program above or below the goto
statement, so the goto
statement causes the control to be shifted either in a forward direction or in a backward direction which is controlled by the position of the label which is called a jump.
There are two types of jumps
(a) Forward Jump (b) Backward Jump
Sometimes it may be necessary to skip a set of statements in a program. To accomplish this, the process we use a goto
statement. When the label is defined after the goto
statement, the process of skipping is called forward jump. Forward jump using goto
statement can also be used for an early exit from a loop.
The backward jump means to shift the control to a statement which is executed before. It is accomplished by a goto
statement in which the label used is defined before the goto
statement. It causes a certain set of statements to be repeated again and again. If this statement is used without any condition, it causes repetition of part of program infinitely, which forms an infinite loop.
The goto
statement when combined with a decision making the statement, i.e. if statement works similar to looping statements.
Consider the following example :
#include<stdio.h> #include<conio.h> main() { int i = 1; up : printf ( "\nHello" ) ; i++; if(i<=5) goto up; getch(); }
This statement causes Hello to be printed 5 times which works similar to a loop. Thus, if statement used with goto
statement forms a loop.
In the previous example, we can see that if the expression (i==3&&j==2)
is satisfied, then the control shifts to the label (out) where the statement is written is executed.
The break statement is preferred over the goto
statement. As we know that C is a structured language and the use of goto
tends to encourage logic skips all over the program which is against the structured feature which allows the statements to be executed in an orderly and sequential manner. Therefore the use of the goto
statement is generally avoided in the case of C. But in case of a break, we can exit from the present loop only thus it avoids multiple jumps.
Occasionally goto statements can also be useful in conditions where there are situations in which it is necessary to jump out of double nested loops. If a certain condition is detected, then two, if break statements, would be needed which would be awkward. It can be made clear by seeing the above example where the goto statement makes a jump from two for loops.
#include<stdio.h> #include<conio.h> void main() { int m,n; printf ("Enter two numbers : ") ; scanf( "%d%d" ,&m,&n); if (m<n) goto output1; else goto output2; output1 : printf("Out of %d and %d, %d is smaller",m,n,m); goto stop; output2 : printf ("Out of %d and %d, %d is smaller" ,m,n,n); stop : getch(); }