Complete the Lesson Summary
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 = ?
Case A: Case B: Case C: Default:
Statements Statements Statements Statements
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 [Link] 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.
2. If none of the case values are matched _________________________
default statement gets
executed.
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.
5. Multi-way decision statement is accomplished using the _______________________
switch
construct.
6. The limitation of a switch statement is that it can evaluate an int or _____________
char
compatible values only.
II. Complete the output for the following program
#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
/* Program to do Arithmetic Manipulation */ Start
#include<stdio.h>
W-5
main()
Input 2 no’s
and option
{
int option, a, b, c ;
printf(“ 1. Addition \n” ); Is
option
printf(“ 2. Subtraction \n” ); ?
printf(“3. Multiplication \n”); case 1 case 2 case 3 default
printf(“ Enter two numbers \n” );
scanf (“ %d %d ”, & a, & b); Invalid
c=a+b c=a-b c=a*b Input
printf(“ Enter your option \ n” );
scanf(“ %d ”,&option);
switch (option) Sum of difference product
of 2 no’s of 2 no’s
{ 2 no’s
case 1 : c = a + b;
printf(“ The Addition of %d and %d is %d”, a, b, c);
break;
case 2: : c = a - b; Stop
printf(“ The Subtraction of %d and %d is %d”, a, b, c);
break;
case 3 : c = a * b;
printf(“ The Multiplication of %d and %d is %d”, a, b, c);
break;
default: : printf(“ Invalid Input”);
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 [Link] = 500 + fees ]
2. Craft [ if ch is 2 then [Link] = 300 + fees ]
3. Carpentry [ if ch is 3 then [Link] = 200 + fees ]
O-3
#include<stdio.h> case 1:printf("Total fee =%d”,fee+500);
main(){ break;
int fee=5000,ch; case 2:printf("Total fee =%d",fee+300);
clrscr(); break;
printf("1. Computer Science \n"); case 3:printf("Total f ee =%d",fees+200);
printf("2. Craft \n"); break;
printf("3. Carpentry \n"); default:printf("Invalid choice");
printf("Enter your choice \n"); }
scanf("%d",&ch); getch();
switch(ch){ }
II. Display a menu as given below and display message accordingly
MENU
W - WRITE A FILE [ DATA IS GETTING PROCESSED …. ]
R - READ A FILE [ READING THE FILE PLEASE WAIT …. ]
#include<stdio.h> case 'W':printf(" Data is getting.. \n");
main() { break;
char opt; case 'R':printf("Reading the file..\n");
clrscr(); break;
printf("Menu\n"); default: printf("Invalid option \n");
printf("W- Writing a file\n"); }
printf("R - Reading a file .pls wait... \n"); getch();
printf("Enter your choice \n");
}
scanf("%c",&opt);
switch(opt ) {
Teacher’s Signature: