 Logical Operator "!" | SoloLearn: Learn to code for FREE!

+1

# Logical Operator "!"

Why is it that,when I write the code,for example,!x%y or !x%y&&!x%c(without parentheses),the statement will return TRUE only when x is 0.Am I missing something?It's so confusing.

+1

Denise Yes, apparently I was too much focusing on the "BOTH" part that I focus only on expressions involving logical AND && operator. The option (c) is also correct because (given x = 3, y = 6) ... ! ( 3 % 3 || 6 % 3) same as ... ! ( 0 || 0) same as ... ! (0) So, did your choice came as true (by the testpaper)?

+1

Ipang sadly i dont know,i couldnt find the answers online.Hopefully there wont be any such exercises at the next transfer exam.

+1

Tricky Denise Well good luck with that exam anyways ...

+1

Try x= 0, y = 0, c = 0 ☕

0

! (Not)Operator returns false of true Statement and vice-versa.

0

codemonkey ah so basically... !x%y = (!x)%y?

0

I guess it's because the logical NOT operator ! has higher precedence over modulo %. I'd recommended to wrap the modulo operands in parentheses in order for them to get evaluated first before the result is passed to logical NOT operator e.g. !(x % y) Or ... (!(x % y) && !(x % c))

0

Ipang the thing is,i found this (as a question,more or less) in a testpaper

0

How was the question like Denise?

0

which one of the expression are true *ONLY* IF BOTH variables (x,y) are divisible by 3.(Therefore if one expression CAN be true with one variable divisible by 3 and the other one not,the expression isnt the answer we're looking for) a) x%3 && y%3 b) !x%3 && !y%3 c) !(x%3 || y%3) (answer chosen by me) d) x%3==0 || y%3==0 and b) is basically making me confused

0

Well, considering the question requires BOTH <x> and <y> to be fully divisible by 3, I guess I'd go with codemonkey here. Option (c) and (d) uses logical OR meaning either one. Option (a) uses logical AND, but it evaluates to true when NEITHER <x> and <y> was fully divisible by three. And still, I'd put parentheses around x % 3 and y % 3. But that's just me :D

0

codemonkey Ipang I used the variables: x=3,y=6 And after running the code with the 4 expressions(separated of course),the output for a) and b) was FALSE,whereas for c) it was TRUE. but if you have a=0,b=0 both b) and c) will have the output TRUE. *cries*

0

Show me the code Denise ...

0

Ipang #include<iostream> using namespace std; int main(){ long long a,b; cin>>a>>b; if(a%3&&b%3){ cout<<"TRUE"<<" "; } else{ cout<<"FALSE"<<" "; } if(!a%3&&!b%3){ cout<<"TRUE"<<" "; } else{ cout<<"FALSE"<<" "; } if(!(a%3||b%3)){ cout<<"TRUE"<<" "; } else{ cout<<"FALSE"<<" "; } if(a%3==0||b%3==0){ cout<<"TRUE"<<" "; } else{ cout<<"FALSE"<<" "; } return 0; } Did i do anything wrong? I suck at boolean expressions T_T

0

In c++ false means 0, and true means another number (example: 1; -1; 3737; -9274 <=> true).

0

Yes it is a logical operator ! (Not)

-1

codemonkey its from a testpaper that's for transferring to a different high school(my friend will most likely have to take it too,and i decided to look over the one from 2016-2017 and then i saw this exercise and i was like:🤔💀=Am I stupid or am I stupid)And i couldnt find the answer online. Maybe you can choose 2 expressions or more ,not only one,but it didnt say anything about that(or if the numbers are >0)

-7

What'sapp me +233239227703