0

I'm having trouble finding information on the implementation of "or". I want to use it in the do while loop below.

do {
    cout << "Please enter 1 for random or 0 for pseudorandom." << endl;   //bool, 1 is TRUE, 0 False
    cin >> random;
    } while (random !0||!1);
4
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by sfuo
0

You would have to have your while() as

while(random != 0 || random != 1);

but this wouldnt work as you want it to. You need to use and (&&)

while(random != 0 && random != 1);

Edited by sfuo: n/a

1

Like sfuo said, the || and && operators are for boolean values, so, you need to put two conditions (or expressions that evaluate to a bool value) on the two sides of those operators. In this case while(random != 0 && random != 1) , which reads as "while 'random' is not 0 and 'random' is not 1".

It is also good to know that the || and && operators are also short-cut operators. It means that, for example, if the first condition already evaluates to true in a or-statement, the second condition will not be evaluated at all because it is not needed to get the result of the overall expression. Similary, in a and-statement, if the first condition evaluates to false, then the second condition is not evaluated at all. This can be very useful, for example, to check if a pointer is NULL before dereferencing it:

while (ptr != NULL && *ptr != some_value )

The second condition *ptr != some_value will not be evaluated if the first one is false (meaning that the pointer is NULL, in which case you couldn't dereference it).

Votes + Comments
Good to know about the pointers
-1

sfuo is may be right.The will be....

do {
    cout << "Please enter 1 for random or 0 for pseudorandom." << endl;   //bool, 1 is TRUE, 0 False
    cin >> random;
    } while (random !0||random !1);
0

If you look at my post I gave you the answer.

The statement random !0 should give you an error. What you need is random != 0. Same thing goes for random !1.

This topic has been dead for over six months. 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.