# Thread: Quick and simple Yes/No question

1. ## 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. "i >= 1 || i <= 3" is always true, so your for loop will never stop.

3. 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. 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.

5. 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. Yes you can have any expression in the for condition as long as it evaluates to some value.

7. 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.

8. 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.

9. 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.

10. 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.

Popular pages Recent additions