is there any easier way to write this:

 

hi 
is there any easier way to write this:

if (A == 1||A==2 ||A==3 ||A==4 ||A == 5 ) {Do Something;}
 
dr.Rez:

hi 
is there any easier way to write this:


Not sure if easier, but I would type it like this.

   switch (A) {
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
         // Do Something;
      break;
      default:
      break;
   }
 
if (A >= 1 &&  A <= 5 ) {Do Something;}
 
Ex Ovo Omnia:


Not sure if easier, but I would type it like this.


Thank you my firend , actually I need to do it faster and shorter.

 
whroeder1:


thank you ,

I didnt mean it .

let change my numbers: for axample 12,83,65 , 15. 

its not about Continuous numbers.

 
There will be very little difference in speed between the methods. Choose your preference.
 
honest_knave:
There will be very little different in speed between the methods. Choose your preference.


Ok Thank you ,

So There is no better way ?

something like : if (A || 12, 83 , 65 , 15) // I know its not working but something like this,


I want a variable to be checked and if it was equal with one of the given amounts the condition become true.

 
dr.Rez:


Ok Thank you ,

So There is no better way ?

something like : if (A || 12, 83 , 65 , 15) // I know its not working but something like this,


I want a variable to be checked and if it was equal with one of the given amounts the condition become true.


'switch' and 'if-else' statements are your normal options.

You could write a function for it, but I don't see much benefit unless there are a lot of numbers and/or you call them in multiple places in your code.

It is just what is more comfortable for you to read and write... then the compiler will do whatever it needs to do.

 
honest_knave:


'switch' and 'if-else' statements are your normal options.

You could write a function for it, but I don't see much benefit unless there are a lot of numbers and/or you call them in multiple places in your code.

It is just what is more comfortable for you to read and write... then the compiler will do whatever it needs to do.


Thank you much.

yes I think I must to writ a function for it.
 

 
dr.Rez: I didnt mean it .


So There is no better way ? something like : if (A || 12, 83 , 65 , 15)

  1. There are no mind readers here.
  2. #define INDEX uint // Zero based.
    #define COUNT uint //  One based.
       template<typename Ti1, typename Ti2>
       COUNT      count(const Ti1& inp[], INDEX iBeg, INDEX iEnd,
                        const Ti2& value){
          COUNT    n  = 0;
          while(iBeg != iEnd)  if(inp[iBeg++] == value)   ++n;
          return n;
       }
    
    int values[] = {12, 83, 65, 15}
    if(count(values, 0, ArraySize(values), x) != 0) Print("X is in there");
 
whroeder1:
  1. There are no mind readers here.


Thank you very much.

There are somethings in your codes I must learn first to understand  how it is work .

So I will use it after I learn the details .

and I did not understand this : 

  1. There are no mind readers here.