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; }
You can use a JavaScript switch greater than the expression same as using in an if-else statement.
Like for example if a variable is less than a specific number, let's say less than 4. The program will print out cases 1 to 3. switch works in two ways, one way is to match an expression to a value, the other is to match a value to an expression.
A switch statement—or simply a case statement—is a control flow mechanism that determines the execution of a program based on the value of a variable or an expression. Using a switch statement allows you to test multiple conditions and only execute a specific block if the condition is true.
The switch statement compares the String object in its expression with the expressions associated with each case label as if it were using the String. equals method; consequently, the comparison of String objects in switch statements is case sensitive.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With