Switch case:
We studied earlier in decision control that how and when to
use if, if-else.
They are good enough to select one from the two available
options. When choices are two, we can use if-else. Even if we have multiple
choices we can use multiple if-else, sometimes we use if-else ladder or nested
if-else. You probably experienced that
as the number of available options/choices are large, if-else becomes complex.
So when we have many choices, we can use switch case control
in place of if-else.
In this section we are going to use three key switch, case and default.
Syntax of switch case
main()
{
….
….
switch( expression)
{
case
constant1 :
….
case
constant2 :
….
case
constant3 :
….
…
default:
……
}
…..
}
Switch case control is used when user have multiple choices.
Switch transfers the control to a case written in its body depending on the
value evaluated by expression in switch parenthesis. In switch body each case
is post fixed by a constant. This constant could be integer or character but
not real. It is also worth mentioning here that the constant in each case must
be distinct.
When value of expression does not match with any case
constant then control moves on default segment.
Another important point to notice that once control moves
from switch to appropriate case, it not just execute statement written in that
case but also execute all the statements written in following cases including
default, but not cases written above. If you are concern with only one case
execution, use break keyword as the last statement of every case.
Switch case control is very useful in menu driven program.
The following example illustrates menu driven program. It is also use break to
terminate switch.
main()
{
int a, b, result, ch;
while(1) // condition is always true, thus an
infinite loop
{
clrscr();
printf(“\n1.
Addition”);
printf(“\n2.
Subtraction”);
printf(“\n3.
Multiplication”);
printf(“\n4.
Division”);
printf(“\n5.
Exit”);
printf(“\n\nEnter
your choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“Enter two
numbers”);
scanf(“%d%d”,&a,&b);
result=a+b;
printf(“Sum is %d”,
result);
break; //it is used to move control out of switch
body
case 2:
printf(“Enter two numbers”);
scanf(“%d%d”,&a,&b);
result=a-b;
printf(“Difference is
%d”, result);
break; //it is used to move control out of switch
body
case 3:
printf(“Enter two
numbers”);
scanf(“%d%d”,&a,&b);
result=a*b;
printf(“Product is %d”,
result);
break;
//it is used to move control out of
switch body
case 4:
printf(“Enter two
numbers”);
scanf(“%d%d”,&a,&b);
result=a/b;
printf(“Quotient is %d”,
result);
break; //it is used to move control out of switch
body
case 5:
exit(0);
default:
printf(“Invalid Entry”);
}
getch();
}
}
Explanation:
There are few things to discuss about above program.
1) Notice
while loop which executes infinite times till you select 5 from the menu.
2) In
case 5, we use a predefined function exit. The job of this function is to
terminate program.
3) The
keyword break is used in each case as it transfers the control outside switch
body.
4) Whenever
wrong selection from menu (other than value from 1 to 5) switch moves the
control to default segment.
5) No
need to put keyword break after default statements as it is already at the end
of switch body.
6) Rest
you can understand the flow of program by executing it.
goto control
The goto statement is used for unconditional jump from one
part of the program to another part of the program. It is always suggested not
to use goto statement as this reduces the readability of the program. Using
goto statement is considered as poor programming approach
Example
main()
{
. int cm;
printf(“Enter
length in centimeters”);
scanf(“%d”,&cm);
if(cm<100)
goto
label;
cm=cm%100;
printf(Wrong
input is trimed”);
label:
printf(“cm=%d”,
cm);
}
Explanation:
1) goto
is a keyword that moves control to a location in the program mentioned by
location name ‘label’.
2) If
value of cm is more than or equal to 100, it should be trimmed. For example if
user enters 435, it becomes 35, 4 should be trimmed out.
3) Try
executing this program for different inputs.
More points about
goto
1) goto
can move control in forward as well as backward in the program.
2) goto
works only with in the same function body.
3) Labels
are names but no need to declare them as variables.
No comments:
Post a Comment