C Programming - Control Instructions - Discussion

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.

[A]. 1,2
[B]. 1,3,4
[C]. 2,4
[D]. 2

Answer: Option B

Explanation:

No answer description available for this question.

Dilli Babu.m said: (Dec 29, 2010)  
What do you meant by jump table in your explanation?

Vinoth said: (Mar 14, 2011)  
Jump table means break statement is it correct or not.

Meer said: (Sep 21, 2011)  
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.

Meer said: (Sep 21, 2011)  
Break statement is not neccesary in every case of the switch statement. In some programs we can implement switch cases without using break;.

Himanshu said: (Oct 18, 2011)  
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");
}

Surender said: (Jan 10, 2012)  
@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.

Saurabh Pandey said: (Jan 21, 2012)  
Why option 2 is not correct? explain.

Shoeb said: (Feb 22, 2012)  
Hello
If v don't knw the ans...
by reading 4th option v say its 100% rit.
then check out of A B C D wch hav 4, so v got only B & C hav 4
so discard A&D now B hav 1 3 & C hav 2 Extra
Then read only 2nd option
if 2 is right then surely ans is C else
ans is 100% B.
Such typ Of technique is used for Finding the exactly correct ans in very short time.
thnx hav a nic day

Sachin Kumar said: (Jul 16, 2013)  
But the question is that why the 2nd option is incorrect even we can use switch to find range?

Aswini said: (Aug 6, 2013)  
Yes, we can use switch for checking whether the given value falls under a range or not. So how can it be incorrect?

Mrunali said: (Jul 31, 2014)  
We can use it but it is not efficient and hence not preferred. Instead we can use an if statement to check for a range of values.

Eg. to check whether value of variable x lies between 0 and 100.

1.switch-case

switch(x)
{
case 0:
case 1:
case 2:
.
.
.
.
case 99:
case 100:printf("True");break;
default: printf("False");break;
}

//This is very long and tedious and hence not useful. Instead we can Accomplish this in a few lines only using if statement.

2.if-else

if(x>=0 && x<=100)
printf("True");
else printf("False");

Ramyaa said: (Aug 11, 2014)  
Each and every case should have break condition. Whether it is mandatory?

Ravi said: (Oct 11, 2014)  
It is not necessary to use a break in every switch statement.

We use break in case statement not in switch.

Mounika said: (Aug 13, 2016)  
We can't check the ranges of non-integer values through switch statement, and so switch is not useful in checking the ranges of a number.

Mounika said: (Aug 13, 2016)  
We can't check the ranges of non-integer values through switch statement, and so switch is not useful in checking the ranges of a number.

Venkata said: (Dec 14, 2016)  
Can we wrote switch statement without cases? Please anyone tell me.

Kartik Agarwal said: (Sep 20, 2017)  
Why statement 2 is wrong?

We can check for ranges.

/*example*/
switch(a)
{
case 2:
case 3:
case 4:
/* some statements */
break;
case 5:
case 6:
case 7:
/* some statements */
break;
}

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.