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
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.
> 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))
Thanks, dude
I used !! instead of ||
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.
> 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.
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?
No, it will exit at the start of the next iteration (or the end of the current one), not "at that moment".
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:
That will be true if the ptr is non-NULL, and false if it's NULL.Code:int isValid = !!ptr;
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.