-2

I'm trying to say "if a variable is equals either one of the values then execute a function",
but I'm stuck on if (a / b == value1 || value).

How can I write this in a right way?

user2736738
  • 30,591
  • 5
  • 42
  • 56
Satori
  • 21
  • 2
  • 3
    [Get a good beginners book](https://stackoverflow.com/a/562377/440558) and learn without copy-pasting. – Some programmer dude Jan 27 '18 at 10:01
  • 1
    what is the type of variable a & b – SAR Jan 27 '18 at 10:01
  • 1
    The question is by no means "off-topic". It is a common novice misconception - incorrectly applying natural language semantics to code semantics. I am not sure that it is helpful to shoot down novice or naive questions quite so brutally. The only legitimate cause for closing such a question would be if it had a duplicate. Moreover voting down a question because it illustrates lack of knowledge or experience is is hardly in the spirit of knowledge dissemination. I ask those that downvoted to consider does it _really_ meet the criteria? https://stackoverflow.com/help/privileges/vote-down – Clifford Jan 27 '18 at 12:44
  • Are the values integers or floating point? If floating point, then `==` check isn't a good idea whether you get the syntax correct or not. – lurker Jan 27 '18 at 13:26

3 Answers3

3

You need to change that into two logically ored comparison expressions.

if ((a / b == value1) || (a / b == value))

Consider having a/b in a variable, which might be more efficient, especially if it has to go into a variable later anyway or already happens to be in one.
If you do:

c = a/b;
if ((c == value1) || (c == value))
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
0
//if the variables are integer..............
int a = 10;
int b = 5;
int value1 = 1;
int value2 = 2;
int c = a/b;

if((c == value1) || (c == value2)){
//do the action
}
SAR
  • 1,765
  • 3
  • 18
  • 42
0

If you are dealing with integers and the values to be tested are literals you can also use a switch:

int a = ...;
int b = ...;

switch (a/b)
{
  case -7: /* value 1 */
  case 42:  /* value 2 */
    /* Perform action here. */
}

or taking care to do something else if no match occurred use:

int a = ...;
int b = ...;

switch (a/b)
{
  case -7: /* value 1 */
  case 42:  /* value 2 */
    /* Perform action here. */

    break;

  default:
    /* Else wise perform other action here. */

    break;/ * not necessary, but good practise. */
}
alk
  • 69,737
  • 10
  • 105
  • 255