Home > Enterprise >  How do I use this statement more efficiently
How do I use this statement more efficiently

Time:09-12

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]

CodePudding user response:

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;
}

CodePudding user response:

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

CodePudding user response:

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;
 }

CodePudding user response:

If the goal is to get a good looking and explicit source code (i.e. a sort of self explanatory source code), you can write a service macro or function naming it elegantly. You put it respectively in some separate include or service file. For example:

#define is_num_in(num, min, max) (((num) >= (min)) && ((num) <= (max)))

Then, you can write your code using the preceding:

if is_num_in(num, 1, 5)
{
[...]

As pointed out in the comments, a robust version which would survive to code evolutions where the service would become a function for example, parenthesis should be added in the above if statement:

if (is_num_in(num, 1, 5))
  •  Tags:  
  • c
  • Related