Decision Statements 101
Table 5.1 Difference between break and continue.
Break Continue
Exits from current block or loop.
Loop takes next iteration.
Control passes to next statement.
Control passes at the beginning of loop.
Terminates the program.
Never terminates the program.
5.8 THE switch STATEMENT
The sw itch statement is a multi-way branch statement. In the program if there is a possibility to
make a choice from a number of options, this structured selection is useful. The sw itch statement
requires only one argument of any data type, which is checked with number of ca s e options. The
switch statement evaluates expression and then looks for its value among the case constants. If the
value matches with cas e constant, this particular ca se statement is executed. If not, d e fa u lt is
executed. Here sw itch , ca se and d e fa u lt are reserved keywords. Every ca se statement
terminates withThe break statement is used to exit from the current ca se structure. The sw itch
() statement is useful for writing menu driven program.
The syntax of the sw itch () case statement is as under.
switch(variable or expression)
{
case constant A :
statement;
break;
case constant B :
statement;
break;
default:
statement;
1
a)The switch () expression In the block the variable or expression can be a character or an integer.
The integer expression following the keyword sw itch () will yield an integer value only. The
integer may be any value 1,2,3 etc. In case of character constant the values may be with alphabets
such as ' x ' , ' y ' , 'z ' etc.
b) The switch () organization The sw itch () expression should neither be terminated with (;)
semi-colon nor with any other symbol. The entire case structure following sw itch () should be
enclosed with curly braces. The keyword case is followed by a constant. Every constant terminates
with a colon (:). Each case statement must contain different constant values. Any number of c ase
statements can be provided. If the case structure contains multiple statements, they need not be
enclosed within curly braces. Here, the keyword ca s e & break performs respectively the job of
opening and closing curly braces.
102 Programming and Data Structures
c) The switch 0 execution When one of the cases satisfies, the statements following it are executed.
In case there is no match, the default case is executed.
The break statement used in switch () passed control outside the switch () block. By
mistake if no break statements are given all the cases following it are executed.
5.20 Write a program to print lines by selecting the choice.
mainQ
{
in t ch;
c l r s c r ();
printf r n [ l ]
........
");
printf ("n[2] ");
printf ("n[3] *****+*+*");
printf ("n[4] =
-------
=-===");
printf ("n[5] EXIT");
printf ("nn ENTER YOUR CHOICE :");
scanf (“%d", &ch);
switch (ch)
1
case 1 :
printf (" n
..............................................
");
break;
c»;e 2:
printf ("n "):
break;
case 3 :
Prinitf(“,(I*********************************************);
break;
II
II
II
II
II
II
II
li
II
II
II
II
II
II
1!
It
II
II
1!
II
II
li
li
1!
II
11
II
II
II
II
II
II
II
II
i;
ii
H
n
n
ii
ii
s
/
V'
I U
-----
);
case 5 :
printf (" Terminated by choice");
exit();
break;
default:
printf (" Invalid Choice");
1
getchO;
Decision Statements 103
OUTPUT:
111
...
f21 f31
---
-
[5] EXIT
ENTER Y O U R CHOICE : 1
Explanation In this program a menu appears with five options and it requests the user to enter
his/her choice. The choice entered by the user is then passed to switch () statement. In the switch
() statement the value is checked with all the case constants. The matched case statement is
executed in which the line is printed according to the user's choice. If the user enters a non-listed
value then no match occurs and default is executed. The default warns the user with a message
" I n v a l i d C h o ic e ".
5.21 Write a program to provide multiple (unctions such as l.addtion 2. Subtraction 3*
Multiplication. 4. Division 5.Remainder Calculation 6. Larger out of two by using switch
() statements.
mainO
{
Int a,b,c,ch;
clrscr();
printf ( =================");
printf r n t M EN U");
prin tf ( " n t =================");
printf C'nt[l] ADDITION");
printf ([2] SUBTRACTION");
printf ( [3] MULTIPLICATION");
printf (" t[4] DIVISION");
printf (" l5] REMAINDER");
printf ("nt[6] LARGER OUTOFTWO");
printf ("ntl0] EXIT");
printf (nt====~===========");
printf ("nn ENTER YOUR CHOICE:");
scanf ("%d", &ch);
if(ch<=6 & ch>0)
I
printf ("Enter Two Numbers:");
scanf ("%d %d",&ca&b);
I
switch (ch)
case 1:
I
104 Programming and Data Structures
c=a+b;
printf (" Addtion: %d"^);
break;
case 2 :
c=a-b;
printf (" Subtraction: %d",c);
break;
case 3 :
c'=a*b;
printf (“ Multiplication: %d'c);
break;
case 4 :
c=a/b;
printf (" Division: %d",c);
break;
case 5 :
c=a%b;
printf (" Remainder: %d"^);
break;
case 6 :
if (a>b)
printf ("nt%d (a) is larger than %d (b),a,b);
else
if(b>a)
printf ("nt%d (b) is larger than %d (a).",b,a);
else
printf (" t %d (a) & %d (b) are same/'ta,b);
break;
case 0 :
printf (" Terminated by choice");
exit();
break;
default:
printf (" Invalid Choice");
I
getchO;
I
OUTPUT:
MENU
[1] ADDITION
[2] SUBTRACTION
[3] MULTIPLICATION
Decision Statements 105
[4] DIVISION
[5] REMAINDER
[6] LARGER OUT OF TWO
[0] EXIT
ENTER YOUR CHOICE: 6
Enter Two Numbers: 8 9
9 (b) is larger than 8 (a).
Explanation In this program also a menu appears with different arithmetic operations. It requests
the user to enter the required choice number. The choice entered by the user is checked with i f
statement. If it is in between 1 and 6 i f block is executed which prompts the user to enter two numbers.
After this the choice entered by the user is passed to sw i tc h () statement and it performs relevant
operation.
5.22 Write a program to convert years into 1. Minutes 2. Hours 3. Days. 4. Months 5. Seconds
using switch () statements.
mainO
{
long ch/minfhrs/ds/mon/yr8/se;
clrscr();
printf ("n[l ] MINUTES");
printf (M [2] HOURS");
printf (" [3] DAYS");
printf C'n[4] MONTHS");
printf ("n[5I SECONDS");
printf (" [0] EXIT');
printf (" Enter Your Choice: “);
scanf ("%ld",&cch);
if(ch>0&& ch<6)
I
printf ("Enter Years:");
scanf ("% ld",&cyrs);
}
mon=yrs*12;
ds=tnon*30;
ds=ds+yrs*5;
hrs=ds*24;
min=hrs*60;
se=min*60;
switch (ch)
I
case 1 :
printf (" Minutes :%ld",min);
break;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.74.25