I would like my program to execute some code using random variables. Whether this happens is dependant of three conditions:

1) a variable (first) should be bigger than an other (second)
2) a variable (third) should be equal to an other (fourth)
3) it should check these thing maximum 5 times (iteration)

If the first AND second condition are met, the do-while loop should stop and the following code should be executed. If after 5 times the program still hasn't picked the correct values, the do-while loop should also stop.

I'm having trouble using the || and &&.

Now i have something like this:

``````iteration = 0;

do
{
some code picking the values for the integers: first, second, third and fourth;
iteration++;
} while (first > second || third != fourth && iteration < 5);

execute the code with the chosen values;``````

I use the || because the loop should repeat if the first condition is true and the second is false (or vice versa).
I use the && because if the first or second condition is still false, but iteration = 5, I want the loop to stop.

With this placement of || and &&, the loop just keeps on running. ( I have made the code so that the first and second condition can't be met.)

could anyone explain what I'm doing (thinking :)) wrong?

thx

5
Contributors
7
Replies
8
Views
11 Years
Discussion Span
Last Post by WaltP

It has to do with the order of precedence.
The && evaluates before the ||.
It is always evaluating

third != fourth && iteration < 5

before anything else.
You have to use () around the operations.

Assuming that the condition is "The loop should only be executed again (since its a do while loop) when the first is less than second AND the third is not equal to fourth OR when the number of iterations is still less than 5", you should have something like this:

``````// will be executed atleast once
do
{
// something
}
while ((first < second && third != fourth) || (iterations < 5)) ;``````

Correct use of parantheses makes the expression logically correct as well as more readable.

Let's fully parenthesize the comparison, shall we? No sense going half way and this remove all ambiguity:

``````// will be executed atleast once
do
{
// something
}
while (((first < second) && (third != fourth)) || (iterations < 5)) ;``````

thx :)

It never even ocurred to me to use parentheses.

I would like my program to execute some code using random variables. Whether this happens is dependant of three conditions:

1) a variable (first) should be bigger than an other (second)
2) a variable (third) should be equal to an other (fourth)
3) it should check these thing maximum 5 times (iteration)

If the first AND second condition are met, the do-while loop should stop and the following code should be executed. If after 5 times the program still hasn't picked the correct values, the do-while loop should also stop.

I'm having trouble using the || and &&.

Now i have something like this:

``````iteration = 0;

do
{
some code picking the values for the integers: first, second, third and fourth;
iteration++;
} while (first > second || third != fourth && iteration < 5);

execute the code with the chosen values;``````

I use the || because the loop should repeat if the first condition is true and the second is false (or vice versa).
I use the && because if the first or second condition is still false, but iteration = 5, I want the loop to stop.

With this placement of || and &&, the loop just keeps on running. ( I have made the code so that the first and second condition can't be met.)

could anyone explain what I'm doing (thinking :)) wrong?

thx

check this:

``while ((first < second || third != fourth) && iteration < 5);``

check this:
while ((first < second || third != fourth) && iteration < 5);
May be that work:

check this:

``while ((first < second || third != fourth) && iteration < 5);``

Finish parenthesizing for readability...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.