Hi guys, this will probably be a simple question and solution, but I'm not grasping it at the moment.
I'm executing the following code below, with the intent that every number that is divisible by 3 gets replaced with a line of text.
However, the output seems to comment out every number that ISN'T divisible by 3.
If I change it to read:
(iii % 3 == 0)
It works as expected. I just can't work out why, not specifying that % 3 == 0 makes it behave as though I'd put a negative (!) operator before the line of code.
Any help? I know it's really simple, but this surprisingly simple piece of code is really confusing me.
#include <iostream>
int main()
{
usingnamespace std;
for (int iii = 0; iii < 20; iii++)
{
if (iii % 3)
{
cout << "This number is divisible by 3" << endl;
}
else
{
cout << iii << endl;
}
return 0;
Well, I knew I'd feel stupid once the solution was revealed. Thanks for that, not sure how I didn't realise that sooner.
I'm sure I'll be back soon with a new issue. Though I hope not.
so I take it by specifying it equals zero, that is then classed as "True" since it's a specific value that was expected? Any value that is not zero is then classed as false.
(Although I still know zero is always false, any any other number changes to 1 and is classed true)