Thread: mutiple conditions in while loops

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242

    mutiple conditions in while loops

    Hi all.

    Why does the while loop evaluate to true, and the program steps into the while loop? Without the second condition, it evaluates to false. What am I missing?

    Thanks in advance.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
         int x = 34; // ascii value of "
    
        while(x != '"' || x != '?')
        {
            printf("x != \"");
            exit(0);
        }
    
        return 0;
    }
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  2. #2
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    It's true that x is not equal to '?'.


  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    The condition

    Code:
    x!= '"' || x != '?'
    is true when one or two of the sub-conditions is true.
    when x is not a quote or a question mark, both sub-conditions are true and the whole condition is true
    when x is a quote (but not a question mark), the first sub-condition is false and the 2nd one is true, making the whole condition true
    when x is a question mark (but not a quote), the first sub-condition is true and the 2nd one is false, making the whole condition true

    There is no way the whole condition can be false

  4. #4
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Ahahaha! *facepalm*

    Used || instead of &&.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    In the while condition you have an OR logical operation.
    This will result to true if at least one of the operants is true. This can be in this case the first , the second or both.
    It will result to false, only if all operants are resulted to false.

    But lets assume that x is the equal operator ( '=' )
    Code:
    x != '"' || x != '?'
    So the first operant will be true, because ' = ' is different form ' " ' . Also the second will be true, likewise . As a result both are true, thus the condition results to true.

    If now x is set to ' " ' , the first operant will be false but the second will be true again.Thus at least one operant of the OR logical operation is true, so at the end the condition will be true again.
    Same situation if x is the question mark . First operant will be false and second true, but again the operation finally will result to true.

    As a result your while condition has no meaning....It would be equivalent if i wrote
    Code:
    while(1)
    which in other words means that it will always be resulted to true.

    EDIT : Maybe i have to type faster next time

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help plz: opening mutiple files in c
    By arpbansal in forum C++ Programming
    Replies: 3
    Last Post: 03-28-2010, 04:15 PM
  2. Mutiple switches
    By quiet_forever in forum C++ Programming
    Replies: 2
    Last Post: 04-26-2007, 03:19 PM
  3. Questions about using Mutiple Files in C++
    By bfach in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2005, 03:46 PM
  4. loops and "complicate / flexible" conditions
    By white in forum C Programming
    Replies: 3
    Last Post: 11-29-2004, 09:41 PM
  5. single *.h in mutiple *.cpp
    By SuperNewbie in forum Windows Programming
    Replies: 3
    Last Post: 06-11-2002, 11:00 PM