Thread: Quick and simple Yes/No question

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    16

    Quick and simple Yes/No question

    Hey all really quick question...

    I tried doing this...

    for(i = 2; i >= 1 || i <= 3; i += direction)

    can I do that boolean operation right within the for loop. It compiles ok but I forget if it will actually function correctly.

    PS: I cant test this because I am working on an embedded system and doing skeleton code atm.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    "i >= 1 || i <= 3" is always true, so your for loop will never stop.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    16
    How is it always true when i is being incremented? Maybe I should specify that direction will either be 1 or -1

    so for instance if direction is -1...

    i = 2
    check i >= 1 || i <= 3 YES
    "run through loop"
    i = i - 1 == 1
    check i >= 1 || i <= 3 YES
    "run through loop"
    i = 1 - 1 == 0
    check i >= 1 || i <= 3 NO
    exit loop..

    Unless adding the boolean does something weird?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Toonzaka View Post
    How is it always true when i is being incremented? Maybe I should specify that direction will either be 1 or -1

    so for instance if direction is -1...

    i = 2
    check i >= 1 || i <= 3 YES
    "run through loop"
    i = i - 1 == 1
    check i >= 1 || i <= 3 YES
    "run through loop"
    i = 1 - 1 == 0
    check i >= 1 || i <= 3 NO
    exit loop..

    Unless adding the boolean does something weird?
    If you think 0 <= 3 is false, then we've got bigger problems.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    16
    Alright my bad; maybe next time instead of being snarky with me you should just answer my question and correct me. I am sorry that I did not see my typo. I understand the concept so thank you for just not helping anyone and wasting both of our time really THANK YOU

    Now getting onto the real question instead of || i mean &&.... is that so hard?

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Yes you can have any expression in the for condition as long as it evaluates to some value.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Toonzaka View Post
    Alright my bad; maybe next time instead of being snarky with me you should just answer my question and correct me.
    He wasn't "snarky with you". His first post answered your question: "this will always be true"


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by claudiu View Post
    Yes you can have any expression in the for condition as long as it evaluates to some value.
    "some value" is too vague for my liking.
    If an expression is specified between the semicolons, the expression result needs to at least be implicitly convertible to bool. Preferably the expression is actually a boolean expression.

    Nothing asked on here is ever really just a yes/no question. Just title your posts with what they are about next time. For instance "for-loop question" would have been pretty much ideal.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by iMalc View Post
    "some value" is too vague for my liking.
    If an expression is specified between the semicolons, the expression result needs to at least be implicitly convertible to bool.
    That is/was not true in the 1989 C standard, as that standard did not support any bool type. The actual requirement is that the expression can be compared with zero.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by grumpy View Post
    That is/was not true in the 1989 C standard, as that standard did not support any bool type. The actual requirement is that the expression can be compared with zero.
    Oh my bad, yes of course, I was thinking this was the C++ forum.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question about types...
    By Elysia in forum C++ Programming
    Replies: 32
    Last Post: 12-07-2008, 05:39 AM
  2. quick simple question regarding enums;
    By MegaManZZ in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2008, 03:47 PM
  3. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 06:51 AM
  4. Quick question, should be simple to answer.
    By kabuatama in forum C Programming
    Replies: 4
    Last Post: 01-21-2006, 03:42 PM
  5. Quick simple question
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2003, 08:37 AM