# multiple ifs...

• 01-16-2008
halfpint101
multiple ifs...
i have a an if statement is there a way to make multiple fuctions equal to one value? for example.

Code:

```if ( x == " yes "," maybe"){         a = 10; } or... if ( x == 1-10){         b = "yes"; }```
or something of that syntax im not sure what but is there any way to do this? or would i have to do each individual one seperatly?

thanks for the help.
• 01-16-2008
Drac
you would either use && or ||, and or, or respectively. in your example it would look like.

Code:

```if ( x == " yes " ||  x ==  " maybe"){         a = 10; } or... if ( x >= 1 && x <= 10){         b = "yes"; }```
• 01-17-2008
matsp
Note that BOTH of the original code-snippets are actually valid C code. They just don't do quite what you expect. The first one is "always true" [at least if we assume the compiler doesn't place strings that have the address of "NULL" - which is true for all compilers I've ever seen].

This is because of the fairly rare "comma operator", where you can write
Code:

`lvalue = expr1, expr2, expr3`
lvalue will get the value of expr3, so in the case of the first code-snippet, the if-statement will be based on the " maybe" string - which is "true" by the C standard.

The second expression compares x with -9, since 1-10 is -9.

--
Mats
• 01-17-2008
halfpint101
Quote:

Originally Posted by matsp
The second expression compares x with -9, since 1-10 is -9.
Mats

ok i was wondering if there was a way to do like:
1 through 5 = something
6 through 10 = something else
etc.

but i guess dracs way works also, well i havent tried yet but thanks for the help.
• 01-17-2008
anon
Code:

```if (x >= 1 && x <= 5) {     something(); } else if (x >= 6 && x <= 10) {     something_else(); }```