# Thread: multiple conditions - if statement

1. ## multiple conditions - if statement

Hi,

In an if statement i want to check multiple conditions (for the same result), but am thinking that there is a quicker way....at the moment ive got it working as follows:

Code:
```if('x' == 0 && 'y' == 0 && 'z' == 0)//and 16 more variables ==0!
{
//do something
}```
as you can see the if statement will go on forever! Any suggestions?

2. Use an array?

3. would this syntax work?

Code:
```int num[4];

if(num[0, 1, 2, 3] == 0)
{
//do something
}```

4. No, it would not do what you intend as it would be effectively:
Code:
```int num[4];

if(num[3] == 0)
{
//do something
}```
due to the comma operator.

5. Sorry if stupid question, but what do you mean due to the comma operator? That syntax works when assigning values to the elements....im confused :/

what would the correct syntax be?

6. Originally Posted by dibble
Sorry if stupid question, but what do you mean due to the comma operator? That syntax works when assigning values to the elements....im confused :/
That is, the expression 0, 1, 2, 3 evaluates to 3. If you want to see this effect in an example, figure out the output of this program, then compile and run this program to see if you guessed correctly:
Code:
```#include <stdio.h>

int main(void)
{
int n = (1, 2);
printf("%d\n", n);
return 0;
}```
Originally Posted by dibble
what would the correct syntax be?
What you did originally, or by using an array with a loop.

7. I can see how that works. Thank you for your help, much appreciated.

8. If you can't (or don't want to ) use an array, you can assign the variables to a pointer array:
Code:
```#include <stdio.h>

int main() {
int a=1, b=4, c=9, *ray[3], i;
ray[0]=&a; ray[1]=&b; ray[2]=&c;
for (i=0;i<3;i++) if (*ray[i]==4) printf("Found.\n");
return 0;
}```
ps. I think you should use brackets around each truth test of this sort:
Code:
`if((x == 0) && (y == 0) && (z == 0))`
You DEFINATELY should not be enclosing the variable names in single quotes('x', 'y','z'); the only reason this compiles and runs is because you are in effect testing the ASCII values (qv.) of a character. That means your condition tests had absolutely nothing to due with any int variables named x, y, or z.

A quick demonstration of this principle:
Code:
```#include <stdio.h>

int main() {
if ('a' == 97) printf("True");
return 0;
}```
Notice there is no variable named a, but the ASCII value of 'a' is 97. Even if you add "int a=5" at the beginning, the value of 'a' is 97, so the condition will still evaluate as true (ie, the character 'a' and a variable, a, are not the same thing).

9. Originally Posted by MK27
If you can't (or don't want to ) use an array, you can assign the variables to a pointer array:
hmm... but if there is only one if statement, that would be excessive work since you might as well just write out the various comparisons directly.

Originally Posted by MK27
ps. I think you should use brackets around each truth test of this sort:
That is optional here. I feel that the extra parentheses are superfluous in such cases, but other people think that it is good style.

Originally Posted by MK27
You DEFINATELY should not be enclosing the variable names in single quotes('x', 'y','z');
Good catch