Using if statement inside for loops

So i'm confused about this part
1
2
3
4
if ( i % 12 == 0)
		{
			cout << "i = " << i << endl;
		}

i % 12 means get the remainder of i which is zero divided by 12 is equal to zero?
then i increments by 1 and now it's i is 1? divided by 12?
I'm confused now.

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

using namespace std;

int main()
{

	for (int i = 1; i < 1000; i++)
	{
		if ( i % 12 == 0)
		{
			cout << "i = " << i << endl;
		}
	}
}

Why it starts counting at 12? I initialize variable to 1 but it started as 12?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
i = 12
i = 24
i = 36
i = 48
i = 60
i = 72
i = 84
i = 96
i = 108
i = 120
i = 132
i = 144
i = 156
i = 168
i = 180
i = 192
i = 204
i = 216
i = 228
i = 240
i = 252
i = 264
i = 276
i = 288
i = 300
i = 312
i = 324
i = 336
i = 348
i = 360
i = 372
i = 384
i = 396
i = 408
i = 420
i = 432
i = 444
i = 456
i = 468
i = 480
i = 492
i = 504
i = 516
i = 528
i = 540
i = 552
i = 564
i = 576
i = 588
i = 600
i = 612
i = 624
i = 636
i = 648
i = 660
i = 672
i = 684
i = 696
i = 708
i = 720
i = 732
i = 744
i = 756
i = 768
i = 780
i = 792
i = 804
i = 816
i = 828
i = 840
i = 852
i = 864
i = 876
i = 888
i = 900
i = 912
i = 924
i = 936
i = 948
i = 960
i = 972
i = 984
i = 996

Could someone explain this better to me line by line how this program .
I just started to today learning c++ and didn't expected programming is very confusing but it's fun.
Last edited on
i % 12 means get the remainder of i which is zero divided by 12 is equal to zero?

It means the remainder of the division i / 12. It's not always zero. Think about it. When does it happen?
Last edited on
Topic archived. No new replies allowed.