while loop - 2 conditions with OR

hi guyz, i have a problem with a while/do while loop, and i'm not certant is it really a problem or i just dont know the rule about using it.
For instance, is it possible to use 2 or more conditions in while loop using ||(or) operator?

int a = 2;
int b = 2;

while(a != 1 || b != 1)
{
do something
a = 1;
}

it doesn't end, it continuesly loops

any thoughts?

Much abliged

Yeah offcourse...

"or" means that if one condition is still true then the whole condition is true

You need to have it false in both condition

so if you set a to 1 and b is still not 1 then it will still loop
This is because the while condition resolves like this:

if a IS NOT equal to 1
or if b is NOT equal to 1

This means that to terminate that loop, a must equal 1 and b must equal 1. You might want to try using the (and) && operator. For example...

while(a != 1 && b != 1)

Means that a AND b must not equal one for the expression to be true.
the correct syntax for a do-while loop is
do
{
//your codes here
}while(condition);

furthermore, it is possible to use more than one condition in the while loop by using the || operator but you must take care that if any condition gets true the it will continue to loop and in your code it is clearly visible that the condition "b!=1" is always true as there isn't any statement to change the value of 'b'. So it is an infinite or never ending loop.
Thank you guyz so much, that really clears it up for me. Now when i get it i don't see why did i go with OR hehe. cheers
Topic archived. No new replies allowed.