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

2. > 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. Maybe you meant || instead of !!?

4. Thanks, dude
I used !! instead of ||

5. 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. > 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. 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. No, it will exit at the start of the next iteration (or the end of the current one), not "at that moment".

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