loop question

This is a discussion on loop question within the C Programming forums, part of the General Programming Boards category; can a for loop check two conditions? like for( ; ((x==1)&&(y==1)) ; ) nvm, I just figured out that if ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    34

    loop question

    can a for loop check two conditions?
    like
    for( ; ((x==1)&&(y==1)) ; )


    nvm, I just figured out that if and break will do the job

    btw what's wrong with this
    if((x==1)&&(y == 1 )!!(z != 0))

    I don't know where should I put the parantheses
    Last edited by Trafalgar Law; 09-23-2008 at 05:45 PM.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > can a for loop check two conditions?
    Yes you can check how ever many you want. Although your example would best be a while loop.

    > if((x==1)&&(y == 1 )!!(z != 0))
    The double negation? Not sure if that's even legal. And the missing logic between (y == 1) (z != 0)

    You don't have to use parenthesis in this case, but it does look better (in this case anyway), ie:

    Code:
    if((x == 1) && (y == 1) && (z != 0))

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Maybe you meant || instead of !!?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    34
    Thanks, dude
    I used !! instead of ||

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    in your for loop, above, no need to put parenthesis and thats application dependent. your for continues to work till x and y values remain 1. the moment their value changes the for loop exits.. remember one thing when you use && operator always the first condition gets checked. so an expression which has maximum chances of getting fail should be the first condition, so that if this condition fails then compiler wont go for checking the other condition. similarly in || operator the first condition should always be the one which will be true for most of the time.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > the moment their value changes the for loop exits.
    No it doesn't.

    The loop will exit at the start of the next iteration if the values change such that the condition is no longer satisfied.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    that is also the application dependent... he might not change the values of x and y till some iterations. and after he may change the values of x and y.... at that particular moment the for loop exits.... if i am wrong tell me how?

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    No, it will exit at the start of the next iteration (or the end of the current one), not "at that moment".

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As a side note, !! is perfectly legal, it means "not not", and guarantees that the result is the same boolean value as the C language would interpret it. Say for example we have a pointer, char *ptr, which is either NULL or "valid", then we can do:
    Code:
    int isValid = !!ptr;
    That will be true if the ptr is non-NULL, and false if it's NULL.

    It's one of many "clever tricks" that look really silly. The compiler will most likely make that into a compare and a SETcc instruction.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop question
    By JuzMe in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2009, 08:39 AM
  2. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-29-2008, 11:12 PM
  3. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-06-2006, 11:43 PM
  4. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 12:08 PM
  5. while loop question
    By rayrayj52 in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2004, 05:13 PM

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