What's wrong

This is a discussion on What's wrong within the C Programming forums, part of the General Programming Boards category; I have this code: Code: #include <stdio.h> int main() { int x, y, i=3, j=4; for( x=i, y=j; x>=0 || ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    124

    What's wrong

    I have this code:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int x, y, i=3, j=4;
    
    	for( x=i, y=j; x>=0 || y >=0; x--, y-- )
    		printf( "%d %d\n", x, y );
    
    	return 0;
    }
    It is supposed to stop typing when i<0 OR j<0, but it doesn't
    Why?

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>x>=0 || y >=0
    Should be:
    x >= 0 && y >= 0
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    Ok, thanks pal!
    But i don't undertand why. With || when x==-1 it should stop...
    Why &&?
    Loading.....
    ( Trying to be a good C Programmer )

  4. #4
    tocornottoc
    Guest
    test out the logic:

    x && y is only true when both x and y are true.

    x || y is only false when both x and y are false.

    so in your case, the first time x is < 0, y is not, and so your statement evaluates to: True || False which still equals True.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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