Thread: Multiple conditions for while

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    17

    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. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by sfff View Post
    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. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    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. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by sfff View Post
    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. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    Quote Originally Posted by zacs7 View Post
    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. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    Quote Originally Posted by sfff View Post
    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 subscribe to a feed

Similar Threads

  1. multiple conditions - if statement
    By dibble in forum C Programming
    Replies: 8
    Last Post: 03-28-2009, 12:41 PM
  2. checking inputs meets multiple conditions
    By 60beetle60 in forum C Programming
    Replies: 5
    Last Post: 04-19-2008, 08:25 AM
  3. Multiple conditions for the while loop?
    By Olidivera in forum C++ Programming
    Replies: 6
    Last Post: 04-24-2005, 03:47 AM
  4. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  5. Testing Multiple Conditions
    By awilmut in forum C++ Programming
    Replies: 16
    Last Post: 10-20-2002, 10:05 PM