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?
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?
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))
//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
}
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. */
}