# Thread: Multiple conditions for while

1. ## Multiple conditions for while

Hi,
i will like to assign multiple conditions for while loop with ||.
however it seems that only the first 2 conditions will be consider and other will be ignore.

So my question is how can i assign many many conditions for while loop with || , like the following does,

while ( guess[0]==guess[1] || guess[0]==guess[2]|| guess[0]==guess[3] || guess [1]==guess [2] || guess[2] == guess[3])

2. Originally Posted by sfff
Hi,
i will like to assign multiple conditions for while loop with ||.
however it seems that only the first 2 conditions will be consider and other will be ignore.

So my question is how can i assign many many conditions for while loop with || , like the following does,

while ( guess[0]==guess[1] || guess[0]==guess[2]|| guess[0]==guess[3] || guess [1]==guess [2] || guess[2] == guess[3])
Perhaps guess[0] equals guess[1] or guess[2]?

Note that logical OR will short-circuit in this case. An example, of what I mean:
Code:
```zac@neux:code (1) \$ gcc ss.c -o ss
zac@neux:code (1) \$ ./ss
loops = 14, foo() = 11
zac@neux:code (1) \$ cat ss.c
#include <stdio.h>

int foo(void)
{
static int x = 0;
return ++x;
}

int main(void)
{
int x = 0,
loops = 0;

while(x < 5 || foo() < 10)
{
x++;
loops++;
}

/* Don't expect foo() to be called "loops" times */
printf("loops = %d, foo() = %d\n", loops, foo());

return 0;
}```
Note that "foo()" wasn't called for every iteration (only once x < 5 became false).

3. sorry maybe i haven't make myself clear,

the while loop function I wrote is suppose to execute when the element in array guess[4] are equal to each other
is that a more simple way to write this in while loop cause i havent learn foo yet~

thanks

4. Originally Posted by sfff
sorry maybe i haven't make myself clear,

the while loop function I wrote is suppose to execute when the element in array guess[4] are equal to each other
is that a more simple way to write this in while loop cause i havent learn foo yet~

thanks
i.e. when any two elements are the same? You're missing a few cases still. i.e. guess[1] == guess[3], guess[2] == guess[1] etc.

I'd generalise that a bit. Perhaps write a function that checks if there are any two elements the same and use that in the while loop.

5. Originally Posted by zacs7
Perhaps write a function that checks if there are any two elements the same and use that in the while loop.
that is what i am trying to do, however, the while loop only consider the first two conditions eg.

while (guess [0] == guess [1] ||guess [0] ==guess [2]), and it simply ignore those conditions afterward, so is there a way to make the while loop consider multiple (more than 2) conditions?

6. Originally Posted by sfff
that is what i am trying to do, however, the while loop only consider the first two conditions eg.

while (guess [0] == guess [1] ||guess [0] ==guess [2]), and it simply ignore those conditions afterward, so is there a way to make the while loop consider multiple (more than 2) conditions?
I think you want
Code:
`guess [0] == guess[1]`
to be true. but you also want
Code:
`guess [0] ==guess [2]`
to be true at the same time

to do that you need to use another logical operator.

there are 3 of these in C.
1) || - this means or. so if the first condition is true or the second condition is true the whole condition will be true
2) && - this means and so if both conditions are true the condition is true, otherwise it is false
3) ! - this means not, this basically turns a true condition into a false condition and vice versa

as an example, if you wanted, all of these to be true at the same time,
Code:
```guess[0]==guess[1]
guess[0]==guess[2]
guess[0]==guess[3]
guess [1]==guess [2]
guess[2] == guess[3]```
you would write
Code:
```while ( guess[0]==guess[1] && guess[0]==guess[2]
&&  guess[0]==guess[3] &&  guess [1]==guess [2] && guess[2] == guess[3])```
is that what you were after?

Popular pages Recent additions