Hi
I am trying to get around the use of "||".
In the below code, the "while" line is giving me problems.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
using namespace std;
int main()
{
int input_1 = 0;
do
{
cout << "List 1 \n List 2 \n List 3 \n";
cin >> input_1;
}
while (input_1 != 1 || input_1 != 2 || input_1 != 3);
cout << "Complete \n";
return 0;
}
|
Last edited on
|| returns false iff both operands are false.
If input_1 != 1
is true the whole condition will be true. We don't even have to look at the other two.
If input_1 != 1
is false we can be sure that input_1 != 2
is true so the whole expression will be true here too.
So, the loop condition will always be true.
Take a look at &&. It is probably what you want to use here.
Last edited on
Sometimes it is useful to apply the mathematical logic. Let consider inverse condition
! (input_1 != 1 || input_1 != 2 || input_1 != 3)
It is equivalent to
(input_1 == 1 && input_1 == 2 && input_1 == 3)
It is obvious that this condition is always equal to false that means that the original condition is always equal to true.
I think this is not what you wanted. Maybe you wanted another condition namely
(input_1 != 1 && input_1 != 2 && input_1 != 3)
It can be rewritten as
( input_1 < 1 || input_1 > 3 )
Thanks Peter87 and Vlad from moscow.
I can see I am not using it right.
I will have to mull this over for a while.
Dan