II-88 Programming Concepts
Below  given  program  is  the  same  program  as  of  the  above  program  using  switch statement 
instead of multiple if-else statement.
scanf(" %c",&x);
if(x=='a')
printf(" You have pressed Alphabet 'a'");
else if(x=='b')
printf(" You have pressed Alphabet 'b'");
else if(x=='c')
printf(" Ypu have pressed Alphabet 'c'");
else
printf(" Sorry You have pressed Alphabet other than
(a,b,c):");
getch();
}
OUTPUT:
Enter any alphabet from (a,b,c):b
You have pressed Alphabet 'b'
#include <stdio.h>
#include <conio.h>
void main()
{
char x;
clrscr();
printf(" Enter any alhabet from (a,b,c)");
scanf(" %c",&x);
switch(x)
{
case 'a':printf(" You have pressed Alphabet 'a'");
break;
case 'b':printf(" You have pressed Alphabet 'b'");
break;
case 'c':printf(" Ypu have pressed Alphabet 'c'");
break;
default:printf(" Sorry You have pressed Alphabet other than
(a,b,c)");
}
getch();
}
OUTPUT:
Enter any alphabet from (a,b,c)b
You have pressed alphabet 'b'
M05_ITL-ESL4791_02_SE_C05.indd 88 12/22/2012 4:58:55 PM
Decision Statements II-89
39. Is it necessary to use default case in switch statement? Justify your answer by using 
suitable program.
Ans: No, it is not necessary to use default case in switch statement. The default label is
optional. Therefore, we can skip the default case in the switch statement. If there is no default
label in your program and the expression of the switch statement does not match with any one of the
case constant, then none of the statements within the body of switch statements is executed. In this
case, the flow of program continues within the body of switch. However, using the default case
in switch statement makes the programmer better to understand. The use of default case in the
switch statement also helps the programmer for logic or error purpose.
Consider the successful execution of following program without making use of default case 
in switch statement.
#include <stdio.h>
#include <conio.h>
void main()
{
char x;
clrscr();
printf("Enter one of the Alphabet from (a b c):");
scanf(" %c",&x);
switch(x)
{
case 'a':printf(" User has pressed a");
break;
case 'b':printf(" User has pressed b");
break;
case 'c':printf(" User has pressed c");
break;
}
getch();
}
OUTPUT:
Enter one of the alphabet from (a b c):g
Output: In the above program, there is no default case used in the program. The program does not
print anything if programmer presses character other than (a, b, c).
Multiple-choice Questions
1. The statement if(0) is .
(a) False (b) True (c) Invalid
2. The statement if(-1) is .
(a) False (b) True (c) Invalid
M05_ITL-ESL4791_02_SE_C05.indd 89 12/22/2012 4:58:55 PM
II-90 Programming Concepts
3. The test condition given within the if, i.e., if(condition) is true, then .
(a) if block executed
(b) else block executed
(c) Both blocks are executed
4. The scope of if() can be defined with .
(a) { } (b) [] (c) ()
5. The keyword else is used with .
(a) ifstatement (b) switch statement
(c) do..while() statement (d) None of the above
6. The following keyword/statement is used to terminate from the innermost loop .
(a) default (b) break (c) case
7. The following keyword is used for unconditional jump to a labelled statement .
(a) Go (b) goto (c) jump
8. The value specified in the parentheses followed to switch is .
(a) Real (b) Double floating-point number (c) Integer
9. Which warning message will be flagged by the compiler for the following program?
void main() { if(0) printf("1");}
(a) Unreachable code in function main
(a) Constant expression required
(b) Statement missing
10. The if-else and switch statements belong to .
(a) Repetitive structure (b) Sequential structure
(c) Selective structure (d) None of the above
11. The default is a .
(a) Keyword in c (b) Function
(c) Pointer (d) None of the above
12. The keyword switch is followed by .
(a) Switch variable (b) Constant
(c) Float variable (d) goto
13. The default statement can appear .
(a) Anywhere within the switch
(b) At the end of each case
(c) At the end of switchcase
14. By mistake if no break statements are given in each case statement, then .
(a) All the cases following it are executed
(b) Statements following to one case executes
(c) None of the above
15. The switch statement is used to .
(a) Switch between functions in a program
(b) Switch from one variable to another variable
M05_ITL-ESL4791_02_SE_C05.indd 90 12/22/2012 4:58:55 PM
Decision Statements II-91
(c) Choose from multiple possibilities that may arise due to different values of a single variable
(d) Use switching variables
16. The default statement is executed when .
(a) All the case statements are false (b) One of the case is true
(c) One of the case is false (d) None of the above
17. Use of default in program is .
(a) Optional (b) Compulsory
(c) Both a and b (d) None of the above
18. exit() requires which of the following header file?
(a) process.h (b) dos.h
(c) system.h (d) grahpics.h
19. Each case statement in switch is separated by .
(a) break (b) continue
(c) exit() (d) goto
20. What will be the output of the following program?
#include <stdio.h>
#include <conio.h>
void main()
{
char x='H';
clrscr();
switch(x)
{
case 'H': printf("%c",'H');
case 'E': printf("%c",'E');
case 'L': printf("%c",'L');
case 'l': printf("%c",'L');
case 'O': printf("%c",'O');
}
(a) HELLO (b) HE
(c) H (d) None of the above
21. The case constant cannot be .
(a) int (b) float
(c) char (d) All of the above
22. What will be the output of the following program?
void main()
{
char x='G';
switch(x)
{
M05_ITL-ESL4791_02_SE_C05.indd 91 12/22/2012 4:58:55 PM
II-92 Programming Concepts
(a) Good Boy (b) Bad Boy
(c) Boy (d) None of the above
23. What will be the output of the following program?
if( x=='B')
{
case 'd': printf("%",'o');
case 'B': printf("%s","Bad");
}
else
case 'G': printf("%s","Good");
default : printf("%s", " Boy");
}
}
void main()
{
char x='d';
clrscr();
switch(x)
{
case 'b':
puts( "0 1 001");
break;
default :
puts("1 2 3");
break;
case 'R' :
puts("I II III");
}
}
(a) 1 2 3 (b) 0 1 001
(c) I II III (d) None of the above
24. What will be the output of the following program?
void main()
{
char y='p';
clrscr();
switch(y)
{
if( y=='p')
{
M05_ITL-ESL4791_02_SE_C05.indd 92 12/22/2012 4:58:55 PM
..................Content has been hidden....................

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