Lock1618
Lock1618

Reputation: 191

java loop adding increments of 100

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

Answers (4)

joao figueiredo
joao figueiredo

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

user1766888
user1766888

Reputation: 409

c+=100 would work too. Same as writing c = c + 100

Upvotes: 0

duffymo
duffymo

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

Matt Ball
Matt Ball

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

Related Questions