SWITCH STATEMENT
The switch statement tests the value of a given variable 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 switch statement is :
Selection process of the switch
statement General form
The expression is an integer expression or characters. Value-1, value-2…….. are constants or constant and
are known as case labels.
Each of these values should be unique within a switch statement block-1, block-2 .... 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 (:).
When the switch is executed, the value of the expression is successfully compared against the values
value-1, value-2.
If a case is found whose value matches with the value of the expression, 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-x following the switch.
The default is an optional case. When present, it will be executed if the value of the expression does not
match with any of the case values.
If not present, no action takes place if all matches fail and the Control goes to the statement-x.
The selection process of switch statement is illustrated in the flow chart shown in above Figure.
RULES FOR SWITCH STATEMENT :
The switch expression must be an integral type.
Case labels must be constants or constant expressions:
Case labels must be unique. No two labels can have the same value.
Case labels must end with colon.
The break statement transfers the control out of the switch statement.
The break statement is optional. That is, two or more case labels may belong to the same
statements.
The default label is optional. If present, it will be executed when the expression does not
find a matching case label.
There can be at most one default label.
The default may be placed anywhere but usually placed at the end.
It is permitted to nest switch statements.
#include<stdio.h>
main()
{
int a;
printf("Please enter a no between 1 and 5: ");
scanf("%d",&a);
switch(a)
{
case 1: printf("You chose One");
break;
case 2: printf("You chose Two");
break;
case 3: printf("You chose Three");
break;
case 4: printf("You chose Four");
break;
case 5: printf("You chose Five.");
break;
default : printf("Invalid Choice. Enter a no between 1 and 5");
break;
}
}
Conditional or Ternary Operator (?:) :
Conditional operator is a combination of ? And : and takes three operands. The general form is :
conditional expression ? Expression1 : expression2
The conditional expression is evaluated first. If the result is non-zero, expression1 is evaluated and is
returned as the value of the conditional expression. Otherwise, expression2 is evaluated and its value is
returned.
For example,
if (x < 0)
flag = 0;
else
flag = 1;
Otherwise it can be written as:
flag = (x <0 ) ? 0 : 1 ;
Program to find largest among two numbers using ternary operator
#include<stdio.h>
Int main()
{
int m = 5, n = 4;
(m < n) ? Printf(“m is greater than n that is %d > %d”, m ,n) :printf(“n is
greater than m that is %d > %d”, n,m);
return 0;
}
Guidelines for Writing Multiway Selection Statements
Complex multiway selection statements require special attention.
Given below are some guidelines that would help improve readability and facilitate maintenance:
Avoid compound negative statements. Use positive statements wherever possible.
• Keep logical expressions simple. We can achieve this using nested if statements
• Try to code the normal/anticipated condition first
• Use the most probable condition first. This will eliminate unnecessary tests, thus improving the
efficiency of the program.
• The choice between the nested if and switch statements is a matter of individual's preference. A
good rule of thumb is to use
the switch when alter-native paths are three to ten.
• Use proper indentations .
• Have the habit of using default clause in switch statements.
• Group the case labels that have similar actions.
GOTO STATEMENT :
The goto statement is a jump statement which is sometimes also referred to as unconditional jump
statement.
The goto statement can be used to jump from anywhere to anywhere within a function.
The goto for unconditional statement to branch unconditionally from one point to another in the
program branching.
Although it may not be essential to use the goto statement in a highly structured language like C,
The goto requires a label in order to identify the place where the branch is to be made.
A label is any valid variable name, and must be followed by a colon.
The 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 in figure.
Program to check if a number is even or not using goto statement
#include<stdio.h>
Void checkEvenOrNot(int num)
{
if(num % 2 == 0)
goto even;
Else
goto odd;
even:
printf(“%d is even”, num);
return;
odd:
printf(“%d is odd”, num);
}
Int main()
{
Int num= 26;
checkEvenOrOdd(num);
return 0;
}