Reputation: 191
This might be a dumb question but I cannot seem to figure it out.
If I am using a loop and start a int such as c at 1 and I want it to display number 1-2000 in increments of 100. ex.
1
100
200
300
400
500
etc.
What would I write for the c=c+?
Upvotes: 0
Views: 11273
Reputation: 145
If you had initial value for c of 0, i would suggest just to simply c=c+100, until c==2000. Since you want a different increment for the first one, I would try to do this bit of pseudo-code:
c=1
while (c!=2000)
{
// check remainder from integer division by 100
int remainder = c%100;
c = c + (100-remainder);
// your cool piece of code
}
Sorry for bad indentation, its 3 AM here ;).
So that code gets your current index, eg. c=1, calculates the remainder of integer division by 100 remainder (99), and adds it no next value of c (1+99 = 100). next iteration will work since the remainder will be 0, and thus it will increment another 100!
Happy coding
Upvotes: 0
Reputation: 308948
You have one problem - you can't start with 1 and then go 100, 200, ... without a special case.
for (int c = 100; c < 2001; c += 100) {
}
Upvotes: 5
Reputation: 359966
c++
is the same thing as c = c + 1
. The increment there is 1
. So, quite simply:
c = c + 100
Note, changing 1
to 100
is not an increment of 100
.
Upvotes: 1