Misunderstanding the code I've written and it's generated output

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.

Thanks in advance.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>


int main()
{
	using namespace 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;
If iii is divisible by 3, then iii%3 gives 0 ,which means false, then it runs code under else.
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)
Last edited on
yup
0==0 -> true
1==0 -> false
2==0 -> false
Last edited on
Topic archived. No new replies allowed.