Multiple condition in while
I have this code:
Code:
bool state=true;
qDebug() << "Entering while...";
i=0;
while((state==true)||(i<3))
{
state=false;
qDebug() << "Value of counter: "<<i;
i++;
}
qDebug() << "End of while";
but the first condition seem not to work.
The output is:
Code:
Entering while...
Value of counter: 0
Value of counter: 1
Value of counter: 2
End of while
If I remove the second condition (i<3), I have
Code:
...
while(state==true)
...
Code:
//Output
Entering while...
Value of counter: 0
End of while
It seems that only one condition is allowed in while loop.
Is it possible?
Re: Multiple condition in while
I fail to see what is the problem.
The output you are getting is exactly what it should be with that code.
What is the output you expect?
OR
What do you want your while() condition to express?
Do you want both conditions to apply or any of them?
At the moment any of the conditions apply.
Re: Multiple condition in while
My idea (ok, it's wrong...):
while "state" is true OR i<3 -> cycle the code.
So if "state" is false OR i> or = 3 the {code} would not be executed.
I.E., when state is false while i==2,code isn't executed.
This seems as a && (state is true AND i=3)...
Re: Multiple condition in while
Yes, in the while condition have to be && and not ||. In the case of ||, the first condition state is false but the second condition is still true.
SOLVED -- Re: Multiple condition in while