1

Is there a way for me to write below code in lesser characters?

if (num == 1 || num == 2 || num == 3 || num == 4|| num == 5)
{    
    printf("Enter number you want to convert:\n");
    scanf("%d", &a);
}
else 
{
    goto quit;
}

Like in SQL we use:

if (num in 1,2,3,4,5)

or like in some imaginary language if we could use something like:

if num in [1-5]
Rachid K.
  • 4,490
  • 3
  • 11
  • 30
Shamvil Kazmi
  • 443
  • 3
  • 12
  • 4
    Abra is right. My preference would be `if( 1 <= num && num <= 5 )` It's the same thing, but I don't have to think about "what could that range be?" PS: the curly braces you've shown aren't quite right... Presuming that is just a typo... – Fe2O3 Sep 12 '22 at 05:08
  • Just a word of warning here. A *set* is not necessarily a *range*. I disagree that in SQL you would use `IN` for a range. Even in languages that support something like `range(1, 5)` you would be better off to compare to lowest and highest value rather than use the `range` concept. – Cheatah Sep 12 '22 at 05:32
  • Guys, be careful: nobody says that `num` is an integer: replacing the series of equalities by a pair of inequalities is ok with integer numbers, but in case of floating point numbers, it's flat wrong. (Obviously, using `num==1` for checking a floating point number is flat wrong too :-) ) – Dominique Sep 12 '22 at 06:34
  • @Cheatah, "_Even in languages that support something like `range(1, 5)` you would be better off to compare to lowest and highest value rather than use the range concept._" Are you sure about that? Why? For example, in Python `range` is very efficient (see [this question](https://stackoverflow.com/q/30081275/10669875)). – wovano Mar 11 '23 at 08:48

3 Answers3

2

If the num can be compared in a range like 1 to 5 can simply write

if (num >= 1 && num <= 5)
{    
    ///.....
}

This is mentioned in other comments and answers. But if the numbers you need to compare are not in a range then the code can be complicated. For me, the simpler way is :

if (num == 1 || num == -2 || num == 3 || num == 10 || num == 5)
{    
    //......
}

But there is another way to write code like SQL(mentioned in the question). Using variable argument can implement a function : 

is_equal(total number of arguments with num variable, num , list of numbers need to compare ..))

This can provide similar code to if (num in 1,2,3,4,5)

#include <stdio.h>
#include <stdarg.h>

int is_equal(int count,...) {

   va_list valist;
   int ret = 0 ; 
   int num ; 

   /* initialize valist for num number of arguments */
   va_start(valist, count);

   /* access all the arguments assigned to valist */
   for (int i = 0; i < count; i++) {
      if(i == 0) num  = va_arg(valist, int) ; 
      else{
        if(num == va_arg(valist, int))
            ret = 1 ; 
      }
   }
    
   /* clean memory reserved for valist */
   va_end(valist);
   return ret;
}

int main() {
    int num = 12 ; 
    printf("%d\n" ,  is_equal(5 ,num , 1, 2, 3, 4)) ; 
    printf("%d\n" ,  is_equal(4 ,num, 1, 2, 3)) ; 
    printf("%d\n" ,  is_equal(6 ,num, 1, 2, 3, 4, 12)) ; 
    printf("%d\n" ,  is_equal(8 ,num, 1, 2, 3, 4, 7, -14, 12)) ; 
    printf("%d\n" ,  is_equal(9 ,num, 1, 2, 3, 4, 7, -14, -12 , 120)) ; 
}

Output :

0
0
1
1
0
1

Use relational comparison operators to check if a number is within the lower and upper bounds of a range:

if (num >= 1 && num <= 5)
{    
    printf("Enter number you want to convert:\n");
    scanf("%d", &a);
}
else 
{
    goto quit;
}
user16217248
  • 3,119
  • 19
  • 19
  • 37
0

or use switch:

switch (num)
{
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
          printf("Enter number you want to convert:\n");
          scanf("%d", &a);
          break;

    default:
          goto quit;
 }
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
Mike
  • 4,041
  • 6
  • 20
  • 37
  • 1
    Are you sure that this solution has less number of characters than what OP posted? (But, of course, this could be an alternate solution). – H.S. Sep 12 '22 at 07:10
  • `switch` is the most useful if the test cases are arbitrary and not consecutive. – user16217248 Sep 12 '22 at 16:01