Larger than and less than in C switch statement
Solution 1
There is no clean way to solve this with switch, as cases need to be integral types. Have a look at if-else if-else.
Solution 2
A switch-less and if-else-less method:
#include <stdio.h>
int main(void)
{
int a=0, i;
struct {
int value;
const char *description;
} list[] = {
{ -999, "hugely negative" },
{ -99, "very negative" },
{ 0, "negative" },
{ 1, "zero" },
{ 100, "positive" },
{ 1000, "very positive" },
{ 1001, "hugely positive" }
};
printf("please enter a number : \n");
scanf("%i",&a);
for (i=0; i<6 && a>=list[i].value; i++) ;
printf ("%s\n", list[i].description);
return 0;
}
The for-loop contains no code (there is just an empty statement ;
) but it still runs over the array with values and exits when the entered value a
is equal to or larger than the value
element in the array. At that point, i
holds the index value for the description
to print.
Solution 3
If you are using gcc, you have "luck" because it supports exactly what you want by using a language extension:
#include <limits.h>
...
switch(a)
{
case 1000 ... INT_MAX: // note: cannot omit the space between 1000 and ...
printf("hugely positive");
break;
case 100 ... 999:
printf("very positive");
break;
...
}
This is non-standard though, and other compilers will not understand your code. It's often mentioned that you should write your programs only using standard features ("portability").
So consider using the "streamlined" if-elseif-else
construct:
if (a >= 1000)
{
printf("hugely positive");
}
else if (a >= 100)
{
printf("very positive");
}
else if ...
...
else // might put a helpful comment here, like "a <= -1000"
{
printf("hugely negative");
}
Solution 4
(a>1000)
evaluates to either 1 [true] or 0 [false].
Compile and you will get the error:
test_15.c:12: error: case label does not reduce to an integer constant
This means, you have to use an integer constant
value for the case
labels. An If-else if-else
loop should work just fine for this case.
Solution 5
Use:
switch (option(a)) {
case (0): ...
case (1): ...
case (2): ...
case (n): ...
Where the option()
function is simply a function with if else
.
It lets you keep the clean look of a switch and the logic part is elsewhere.
Salahuddin
I'm an electronics engineering student , i'm Studying C language and i use Eclipse compiler.
Updated on June 12, 2020Comments
-
Salahuddin almost 4 years
I'm trying to write a code that has a lot of comparison
Write a program in “QUANT.C” which “quantifies” numbers. Read an integer “x” and test it, producing the following output:
x greater than or equal to 1000 print “hugely positive”
x from 999 to 100 (including 100) print “very positive”
x between 100 and 0 print “positive”
x exactly 0 print “zero”
x between 0 and -100 print “negative”
x from -100 to -999 (including -100) print “very negative”
x less than or equal to -1000 print “hugely negative”Thus -10 would print “negative”, -100 “very negative” and 458 “very positive”.
Then I tried to solve it using a switch statement, but it didn't work. Do I have to solve it using an
if
statement or there is a method to solve it using a switch statement?#include <stdio.h> int main(void) { int a=0; printf("please enter a number : \n"); scanf("%i",&a); switch(a) { case (a>1000): printf("hugely positive"); break; case (a>=100 && a<999): printf("very positive"); break; case (a>=0 && a<100): printf("positive"); break; case 0: printf("zero"); break; case (a>-100 && a<0): printf("negative"); break; case (a<-100 && a>-999): printf("very negative"); break; case (a<=-1000): printf("hugely negative"); break; return 0; }