C Programming - Control Instructions - Discussion
Discussion Forum : Control Instructions - Point Out Correct Statements (Q.No. 9)
9.
Which of the following sentences are correct about a switch loop in a C program?
1: | switch is useful when we wish to check the value of variable against a particular set of values. |
2: | switch is useful when we wish to check whether a value falls in different ranges. |
3: | Compiler implements a jump table for cases used in switch. |
4: | It is not necessary to use a break in every switch statement. |
Discussion:
17 comments Page 2 of 2.
Saurabh Pandey said:
1 decade ago
Why option 2 is not correct? explain.
Surender said:
1 decade ago
@jump Table
A jump table is an abstract structure used to transfer control to another location.
#include<stdio.h>
int main(int argc, char **argv)
{
int i;
scanf("%d",&i);
switch(i)
{
case 1:
printf("\nIts %d",i);
break;
case 2:
printf("\nIts %d",i);
break;
case 3:
printf("\nIts %d",i);
break;
case 4:
printf("\nIts %d",i);
break;
case 5:
printf("\nIts %d",i);
break;
default: printf("\nIts not in range of 1-5");
}
}
In this program if 3 is true, then it will directly jump to body of case 3, & it will keep executing all statements until it want gets a break statement.
A jump table is an abstract structure used to transfer control to another location.
#include<stdio.h>
int main(int argc, char **argv)
{
int i;
scanf("%d",&i);
switch(i)
{
case 1:
printf("\nIts %d",i);
break;
case 2:
printf("\nIts %d",i);
break;
case 3:
printf("\nIts %d",i);
break;
case 4:
printf("\nIts %d",i);
break;
case 5:
printf("\nIts %d",i);
break;
default: printf("\nIts not in range of 1-5");
}
}
In this program if 3 is true, then it will directly jump to body of case 3, & it will keep executing all statements until it want gets a break statement.
Himanshu said:
1 decade ago
We can check if a particular number falls in a range using switch. That's why according to me option 2 should be correct.
For example if x=5 & we hav to check if x falls in 2 to 7
switch(x)
{ case 2:
case 3:
case 4:
case 5:
case 6:
case 7:printf("in range");
break;
default:printf("not in range");
}
For example if x=5 & we hav to check if x falls in 2 to 7
switch(x)
{ case 2:
case 3:
case 4:
case 5:
case 6:
case 7:printf("in range");
break;
default:printf("not in range");
}
Meer said:
1 decade ago
Break statement is not neccesary in every case of the switch statement. In some programs we can implement switch cases without using break;.
Meer said:
1 decade ago
In some particular situations in switch statement we can control the execution by using break; and then it terminates the execution of the program in number of cases of the switch.
Vinoth said:
1 decade ago
Jump table means break statement is it correct or not.
Dilli babu.m said:
1 decade ago
What do you meant by jump table in your explanation?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers