I am having a problem with my program not reading the OR statements right.

``````do
{

blah blah blah

}while(y!=1||y!=2);``````

I have also used it as this format, ((y!=1)||(y!=2))

The program is marking it as true and keeping me stuck the loop even though y is == 1 or 2.

while(y!=1||y!=2);

Can you imagine any value of y for which at least one side of the OR isn't true?

The answer is no - if y=1, then the right hand side is true, so the whole OR statement is true. If y=2, then the left hand side is true, so the whole OR statement is true. If y= anything else, both sides are true so the whole OR statement is true.

You want it to keep going when: y does not equal one AND y does not equal two.

while(y!=1||y!=2);

Can you imagine any value of y for which at least one side of the OR isn't true?

The answer is no - if y=1, then the right hand side is true, so the whole OR statement is true. If y=2, then the left hand side is true, so the whole OR statement is true. If y= anything else, both sides are true so the whole OR statement is true.

You want it to keep going when: y does not equal one AND y does not equal two.

Thank you so much. Can't believe I didn't notice that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.