Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi
What does this statement in c++ mean:
if (!(someVariable%20))

Is this is the meaning in C#:
if (someVariable % 20 != 0)

Thanks
Posted
Updated 15-May-12 8:31am
v2
Comments
Sandeep Mewara 15-May-12 14:24pm    
Is the first line valid? AFAIK, below line should be valid in C++ too. Lets see what others say.
lewax00 15-May-12 14:54pm    
It is valid, it's just shorthand for checking if something is equal to 0.
Sandeep Mewara 15-May-12 15:09pm    
Ok. Thanks. Good to know these small things while browsing questions.

:thumbsup:
Member 8456258 15-May-12 14:29pm    
Yes, the first line is a functional code and it runs fine.
JackDingler 15-May-12 14:50pm    
And depends on operator precedence in C#.

Could be equivalent to:
if ((someVariable % 20) != 0)
or
if (someVariable % (20 != 0))

I would guess it's the first one, but for such things, I use parenthesis to demonstrate my clear intent.

C and C++ operators[^]
C# operators[^]

In both languages % means modulo[^], so both conditions checks for the remainder of division of one number (variable) by another (20).
 
Share this answer
 
Comments
Member 8456258 15-May-12 14:45pm    
I understand, but are they the same? I mean does (!(someVariable%20)) statement in c++ return true when the division of someVariable to 20 results in 0? by the way someVariable increments. So, if my assumption is correct does it mean that the operation suppose to happen once every 20 times of iteration?
Thanks.
lewax00 15-May-12 15:00pm    
Yes, that assumption is correct.
Sergey Alexandrovich Kryukov 15-May-12 18:30pm    
Right, a 5.
--SA
Maciej Los 16-May-12 0:36am    
Thank you ;)
VJ Reddy 16-May-12 7:21am    
My 5!
Yes it does. You can also express this in C# as

if (! Convert.ToBoolean(i % 20))


C# does not automatically convert 0 to false like C++
 
Share this answer
 
Comments
Member 8456258 15-May-12 15:12pm    
I like this one, thanks!
Maciej Los 15-May-12 16:24pm    
Good answer, my 5!
Sergey Alexandrovich Kryukov 15-May-12 18:34pm    
I would avoid it. Additional call, and what for? Just to reduce readability and add a chance of a mistake?
if (someVariable % 20 == 0) is the best.
--SA
Clifford Nelson 15-May-12 20:14pm    
I was not advocating using it, just showing that it was the same.
No. the two are not the same: the !someVariable%20 does an logical invert on somevariable first, then takes the value modulus 20. If that is non-zero, it's true. Try it:
C#
int someVariable;
for (someVariable = 0; someVariable < 21; someVariable++)
   {
   Console::WriteLine(L"");
   if (!someVariable%20) Console::WriteLine(L"YES");
   else Console::WriteLine(L"NO");
   if (someVariable%20 != 0) Console::WriteLine(L"YES");
   else Console::WriteLine(L"NO");
   }

You get:
YES
NO

NO
YES

NO
YES

NO
YES

NO
YES

NO
YES

NO
YES

NO
YES

NO
YES

NO
YES

NO
YES

NO
YES

NO
YES

NO
YES

NO
YES

NO
YES

NO
YES

NO
YES

NO
YES

NO
YES

NO
NO
 
Share this answer
 
Comments
lewax00 15-May-12 14:55pm    
Except there are parenthesis, so the inversion is applied last.
OriginalGriff 15-May-12 14:57pm    
They weren't there when I looked! I'm sure I copy and pasted it into my app to test it... :confused:
Member 8456258 15-May-12 15:19pm    
You are right I missed the parenthesis at first but later I updated the question. I apologize for that but your answers were very useful, thanks!
OriginalGriff 15-May-12 15:24pm    
:laugh:
Probably my fault for answering the phone in mid typing!
The code checks if someVariable is evenly divisible by 20: someVariable%20 returns the remainder of someVariable/20, if the remainder is 0 it is evenly divisible, and in C++ !0 is true.

For C# if the original C++ line doesn't work, then what you want is
C#
if (someVariable % 20 == 0)
 
Share this answer
 
Comments
Maciej Los 15-May-12 16:24pm    
Good answer, my 5!
Sergey Alexandrovich Kryukov 15-May-12 18:33pm    
This is a correct way. I would advise to avoid implicit Boolean conversion allowed in C++.
My 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900