Decisions Using Switch with answers
Decisions Using Switch with answers
HGC
9
JV
The process of selecting an alternative among many alternatives is generally referred to as
multiway decision making
_____________________________________________. In a multi-way decision statement the
control is transferred to one of the many possible points. This is accomplished using the
switch
_____________________ statement or construct
LS-5
Syntax Flowchart 1
switch(expression) Switch
{ Expression=?
case 1 :
{ Case 1:
action1; Statements…
break;
}
Case 2:
case 2 :
Statements…
{
action2;
break; Case 3:
} Statements…
::::::::::::::::::::::::::
default : Default:
{ Statements…
action3;
}
}
Flowchart 2
Switch
expression = ?
switch construct,
The value of the expression is matched with the case constants of the _____________
if a match is found then its corresponding statements are executed.
four
Switch uses ______________ reserved words such as switch, case
________________,
break
________________ default
and ________________
integer expression
The keyword switch (______________________________) identifies start of the switch statement.
case
__________ identifies individual values that are compared to the value of the switch expression.
If the expression does not match any of the case, no statement gets executed unless
default
________________ is encountered.
break
________________ statement in a switch takes the control outside the switch.
The limitation of a switch statement is that it can evaluate an int or char compatible values only.
Page 1
Complete the Lesson Summary (Contd …)
HGC
9
JV
/* Program to display the grade */
#include <stdio.h>
Start
main( ) {
char grade; Input LS-5
Grade
printf("Enter your grade :");
scanf("%c", &grade);
switch (grade) {
Is Grade
case 'A': ?
printf("Excellent"); A B C others
break;
case 'B': Excellent V.Good Fair Invalid
data
printf("Very Good");
break;
case 'C':
printf("Fair");
break;
default:
Here we are!!
printf("Invalid Input, enter either A,B or C”); Out of the
switch block!
}
printf (“Here we are !! Out of the switch block !”);
getch(); Stop
}
Program Explanation
▪ The variable grade is declared.
▪ scanf accepts the value for ‘grade’ from the user.
▪ Switch construct checks the given value for a match.
▪ If match is found with any one of the case statements the corresponding message is
displayed.
▪ Finally, the program comes out of the switch construct and displays the message “Here
we are !! Out of the switch block!
Result
Enter your grade: A
Excellent
Here we are !! Out of the switch block !
Teacher’s Signature:
I. Use the correct word from the box to complete each sentence HGC
9
JV
break char multiple action default switch case
case
1. ____________________ is used to identify the individual values that are compared to W-5
the values of the switch expression.
3. _______________________
break statement takes the control outside the switch.
4. Switch is used when a program needs to check for many conditions with _____________
multiple
________________
action to be performed for each condition.
#include<stdio.h>
main() {
int mynumber;
clrscr();
printf ("Please choose any one digit [1, 4 or 5 ] \n");
scanf ("%d", &mynumber);
switch (mynumber) {
case 1: printf ("You chose a perfect square.\n");
break;
case 4: printf ("You chose an even number.\n");
break;
case 5: printf ("You chose a prime number.\n");
break;
default: printf ("Invalid choice.\n");
}
getch(); Output :
}
Please choose any one digit [1, 4 or 5 ]
5
You chose a prime number
Page 1
III. Do the following for the program given below HGC
9
JV
a. Fill in the boxes and the flowchart with correct keywords b. Complete the given flowchart
case 1 : c = a + b;
break;
case 3 : c = a * b;
break;
getch();
}
Teacher’s Signature:
I. Display a menu to Accept the choice from the user and generate the total fee for HGC
9
JV
the first term
1. Computer Science [ if ch is 1 then t.fee = 500 + fees ]
2. Craft [ if ch is 2 then t.fee = 300 + fees ]
3. Carpentry [ if ch is 3 then t.fee = 200 + fees ]
O-3
#include<stdio.h> case 1:printf("Total fee =%d”,fee+500);
main(){ break;
clrscr(); break;
scanf("%d",&ch); getch();
switch(ch){ }
main() { break;
clrscr(); break;
scanf("%c",&opt);
switch(opt ) {
Teacher’s Signature: