||, && operators in C

4.7k views Asked by At

Why this code only work with && operator?

I think it should be ||, but I'm wrong. Choice can not be equal to 2 value at the same time?

I need to ask user's input until choice will be equal to 'a' OR 'd', but why I need to write && ? I don't get it.

do
{
    scanf("%c", &choice);
} while ( choice != 'a' && choice != 'd' );

I wanted to use ||, but it's not working.

6

There are 6 answers

0
Sourav Ghosh On BEST ANSWER

There's nothing wrong with how the operators work, you need to get the logic for the code here.

First, a do...while loop operates as long as the condition in the while is TRUE.

In your case, you want to ask user's input until choice will be equal to 'a' OR 'd'.

  • So, in other way, as long as user input is not equal to a and d, you need to loop.

  • Logically, if the input is not equal to a, it can still be equal to d, so you have to check both the cases there. Only when, none of a or d is the input, you continue the loop.

Remember, you're not checking the equality, you're checking the unequality. Only if both the inequalities are satisfied, then only the while condition evaluates to TRUE_ and you continue looping to ask for new value(s).

In a nutshell, read DeMorgan's laws.

1
Vlad from Moscow On

I need to ask user's input until choice will be equal to 'a' OR 'd'

This condition can be written like

choice == 'a' ||  choice == 'd'

So if you want that the loop would iterate until this condition is true you should write

do
{
    //...
} while ( !( choice == 'a' ||  choice == 'd' ) );

Or if to include header

#include <iso646.h> 

then you can write

do
{
    //...
} while ( not ( choice == 'a' ||  choice == 'd' ) );

or even like

do
{
    //...
} while ( not ( choice == 'a' or  choice == 'd' ) );

The condition

!( choice == 'a' ||  choice == 'd' )

or

not ( choice == 'a' or  choice == 'd' )

is equivalent to

!( choice == 'a' ) &&  !( choice == 'd' )

or

not ( choice == 'a' ) and  not ( choice == 'd' )

that in turn is equivalent to

( choice != 'a' ) &&  ( choice != 'd' )

The parentheses can be omitted Thus you will have

do
{
    //...
} while ( choice != 'a' &&  choice != 'd' );
0
haccks On

You can use ||. You need to change while loop conditional expression as follows

while ( !(choice == 'a' || choice == 'd') ); 

It's just an application of DeMorgan's Law, how a not affect the AND & OR

______   ___  ___
A || B =  A && B
______   ___  ___
A && B =  A || B

Remember it as: "Break the line, change the sign".

INPUT   OUTPUT 1        OUTPUT 2
A   B   NOT (A OR B)    (NOT A) AND (NOT B)
0   0          1                 1
0   1          0                 0
1   0          0                 0
1   1          0                 0

INPUT   OUTPUT 1        OUTPUT 2
A   B   NOT (A AND B)   (NOT A) OR (NOT B)
0   0           1               1
0   1           1               1
1   0           1               1
1   1           0               0
2
Ihor Dobrovolskyi On

You can replace the condition with this one:

while (!(choice == 'a' || choice == 'd'));

Using this condition do-while statement will be executing until choice equals 'a' or 'd'.

0
user2736738 On

hey it won't work because you want it to be not equal to a and b.

not equal to a and not equal to b

so it will be !( eqaul to a or eqaul to b) (De morgan's)

De Morgan's Law says

not( a and b)  = not (a) or not(b)

Here I applied simply that.

0
lmiguelvargasf On

If you wan to use the or (||) operator, you have to apply the De Morgan's law:

Let's say a, and b are booleans, so

a || b <=> !a && !b

a && b <=> !a || !b

This,

while ( choice != 'a' && choice != 'd' );

is the same as

while !( choice == 'a' || choice == 'd' );