Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Larger than and less than in C switch statement

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; } 
like image 779
Salahuddin Avatar asked Jan 07 '14 12:01

Salahuddin


People also ask

Can you use greater than in a switch statement?

You can use a JavaScript switch greater than the expression same as using in an if-else statement.

Can you use less than in switch 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.

Can switch-case have multiple conditions in C?

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.

Can you do a comparison in a switch statement?

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.


2 Answers

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.

like image 136
Eric Fortin Avatar answered Sep 19 '22 23:09

Eric Fortin


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.

like image 33
Jongware Avatar answered Sep 20 '22 23:09

Jongware