# Quick and simple Yes/No question

• 04-06-2010
Toonzaka
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.
• 04-06-2010
tabstop
"i >= 1 || i <= 3" is always true, so your for loop will never stop.
• 04-06-2010
Toonzaka
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?
• 04-06-2010
tabstop
Quote:

Originally Posted by Toonzaka
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.
• 04-06-2010
Toonzaka
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?
• 04-06-2010
claudiu
Yes you can have any expression in the for condition as long as it evaluates to some value.
• 04-06-2010
quzah
Quote:

Originally Posted by Toonzaka
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.
• 04-07-2010
iMalc
Quote:

Originally Posted by claudiu
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.
• 04-07-2010
grumpy
Quote:

Originally Posted by iMalc
"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.
• 04-07-2010
iMalc
Quote:

Originally Posted by grumpy
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.