&& not working as expected

This is a discussion on && not working as expected within the C Programming forums, part of the General Programming Boards category; Hello, was hoping someone could comment on the use of && in C. I'm writing a program that is parsing ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    4

    && not working as expected

    Hello, was hoping someone could comment on the use of && in C.

    I'm writing a program that is parsing a file with two pointers, stopping when the "contents" at those pointers become certain values, incrementing the pointers in a while loop otherwise.

    My code doesn't seem to work as expected, the problem seems to be that I don't understand the && operator, although I thought I did for the longest time getting results as I expected, although that was using GCC on Linux system.

    I am now trying lcc on windows.

    I've written some simple code that replicates my problem in understanding:

    Code:
    void main(void)
    {
    int a,b;
    
    a = 2;
    b = 3;
    
    while( (a != 5) && (b != 6) )
    	{
    	a = a+1;
    	b = b+2;
    	}
    
    printf("a is %d and b is %d\n", a,b);
    
    
    return ;
    }
    So when I run the code above I would expect the while loop to run infinitly ( not that I want it too) because a== 5 and b==6 never happens at the same time. However, when I run the program it exits the loop when a == 5, as evidenced(tested) with my printf. So it seems to me that when a == 5 the && is acting like an OR operator " || "; that it will leave the loop if only one of the conditions are met.

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Your loop doesn't do what you think it does.

    It will loop WHILE a isn't five AND b isn't 6. They BOTH have to be true for it to continue looping. If any one or both is false (i.e. a == 5) then the && fails and the loop stops.

    Simply put, if a == 5, then it's impossible for a not to be equal to five, so the loop condition is automatically false. What you probably want is:

    Code:
    !((a == 5) && (b == 6))

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by TonyBalony View Post
    Hello, was hoping someone could comment on the use of && in C.

    I'm writing a program that is parsing a file with two pointers, stopping when the "contents" at those pointers become certain values, incrementing the pointers in a while loop otherwise.

    My code doesn't seem to work as expected, the problem seems to be that I don't understand the && operator, although I thought I did for the longest time getting results as I expected, although that was using GCC on Linux system.

    I am now trying lcc on windows.

    I've written some simple code that replicates my problem in understanding:

    Code:
    void main(void)
    {
    int a,b;
    
    a = 2;
    b = 3;
    
    while( (a != 5) && (b != 6) )
    	{
    	a = a+1;
    	b = b+2;
    	}
    
    printf("a is %d and b is %d\n", a,b);
    
    
    return ;
    }
    So when I run the code above I would expect the while loop to run infinitly ( not that I want it too) because a== 5 and b==6 never happens at the same time. However, when I run the program it exits the loop when a == 5, as evidenced(tested) with my printf. So it seems to me that when a == 5 the && is acting like an OR operator " || "; that it will leave the loop if only one of the conditions are met.
    B will never = 6 ... you're starting at 3 and adding 2 each time... 3, 5 ,7 .... so that part of the while loop condition does nothing.

    The loop exits on a = 5 because it is no longer true that a is not 5 AND b is not 6... only one of the two required states is true... the loop exits.
    Last edited by CommonTater; 12-14-2011 at 12:25 PM.

  4. #4
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    107
    Quote Originally Posted by TonyBalony View Post
    Hello, was hoping someone could comment on the use of && in C.

    I'm writing a program that is parsing a file with two pointers, stopping when the "contents" at those pointers become certain values, incrementing the pointers in a while loop otherwise.

    My code doesn't seem to work as expected, the problem seems to be that I don't understand the && operator, although I thought I did for the longest time getting results as I expected, although that was using GCC on Linux system.

    I am now trying lcc on windows.

    I've written some simple code that replicates my problem in understanding:

    Code:
    void main(void)
    {
    int a,b;
    
    a = 2;
    b = 3;
    
    while( (a != 5) && (b != 6) ) )
        {
        a = a+1;
        b = b+2;
        }
    
    printf("a is %d and b is %d\n", a,b);
    
    
    return ;
    }
    So when I run the code above I would expect the while loop to run infinitly ( not that I want it too) because a== 5 and b==6 never happens at the same time. However, when I run the program it exits the loop when a == 5, as evidenced(tested) with my printf. So it seems to me that when a == 5 the && is acting like an OR operator " || "; that it will leave the loop if only one of the conditions are met.
    the condition in the while loop states that while "this" condition is true the loop will continue. and a && operator states that while both the conditions are simultaneously true the while loop continues .
    so, the condition
    Code:
    while( (a != 5) && (b != 6) ) )
    states that while the value of a is not equal to 5 and SIMULTANEOUSLY the value of b is not equal to 6the loop will continue. if both of them becomes false or even if any one of them becomes false the while loop ends.
    so , when the value of a becomes 5 ,(a!=5) becomes false and the while loop ends. and you get the value of a as 5 and b as 9.
    and one more thing the return type of main is not void it's int.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    Last edited by joybanerjee39; 12-14-2011 at 12:31 PM.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    4
    Ok I understand now
    and yes the code you show gives the results I intended.

    thanks ledow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithreading not working as expected
    By shiroaisu in forum Windows Programming
    Replies: 5
    Last Post: 06-18-2011, 03:34 AM
  2. Program not working as expected
    By perrrson in forum C Programming
    Replies: 3
    Last Post: 10-02-2010, 02:49 PM
  3. IntersectRect() not working as expected?
    By dxfoo in forum Windows Programming
    Replies: 1
    Last Post: 09-05-2006, 05:52 PM
  4. cin.get(); doesn't seem to be working as expected
    By Diablo84 in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2005, 07:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21