Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make for loops in Java increase by increments other than 1

People also ask

Can I increment by 2 in a for loop in Java?

In the next line, for loop is declared that starts from 1 and ends when the value of i is less than n and inside this loop the index value of variable i is increment by 2. Inside the loop, the print function is used, which uses variable i to prints its increment value.

Can you increment by 2 in a for loop?

A for loop doesn't increment anything. Your code used in the for statement does. It's entirely up to you how/if/where/when you want to modify i or any other variable for that matter.


That’s because j+3 doesn’t change the value of j. You need to replace that with j = j + 3 or j += 3 so that the value of j is increased by 3:

for (j = 0; j <= 90; j += 3) { }

Since nobody else has actually tackled Could someone please explain this to me? I believe I will:

j++ is shorthand, it's not an actual operation (ok it really IS, but bear with me for the explanation)

j++ is really equal to the operation j = j + 1; except it's not a macro or something that does inline replacement. There are a lot of discussions on here about the operations of i+++++i and what that means (because it could be intepreted as i++ + ++i OR (i++)++ + i

Which brings us to: i++ versus ++i. They are called the post-increment and pre-increment operators. Can you guess why they are so named? The important part is how they're used in assignments. For instance, you could do: j=i++; or j=++i; We shall now do an example experiment:

// declare them all with the same value, for clarity and debug flow purposes ;)
int i = 0;
int j = 0;
int k = 0;

// yes we could have already set the value to 5 before, but I chose not to.
i = 5;

j = i++;
k = ++i;

print(i, j, k); 
//pretend this command prints them out nicely 
//to the console screen or something, it's an example

What are the values of i, j, and k?

I'll give you the answers and let you work it out ;)

i = 7, j = 5, k = 7; That's the power of the pre and post increment operators, and the hazards of using them wrong. But here's the alternate way of writing that same order of operations:

// declare them all with the same value, for clarity and debug flow purposes ;)
int i = 0;
int j = 0;
int k = 0;

// yes we could have already set the value to 5 before, but I chose not to.
i = 5;

j = i;
i = i + 1; //post-increment

i = i + 1; //pre-increment
k = i;

print(i, j, k); 
//pretend this command prints them out nicely 
//to the console screen or something, it's an example

Ok, now that I've shown you how the ++ operator works, let's examine why it doesn't work for j+3 ... Remember how I called it a "shorthand" earlier? That's just it, see the second example, because that's effectively what the compiler does before using the command (it's more complicated than that, but that's not for first explanations). So you'll see that the "expanded shorthand" has i = AND i + 1 which is all that your request has.

This goes back to math. A function is defined where f(x) = mx + b or an equation y = mx + b so what do we call mx + b ... it's certainly not a function or equation. At most it is an expression. Which is all j+3 is, an expression. An expression without assignment does us no good, but it does take up CPU time (assuming the compiler doesn't optimize it out).


I hope that clarifies things for you and gives you some room to ask new questions. Cheers!


In your example, j+=3 increments by 3.

(Not much else to say here, if it's syntax related I'd suggest Googling first, but I'm new here so I could be wrong.)


for(j = 0; j<=90; j = j+3)
{

}

j+3 will not assign the new value to j, add j=j+3 will assign the new value to j and the loop will move up by 3.

j++ is like saying j = j+1, so in that case your assigning the new value to j just like the one above.


Change

for(j = 0; j<=90; j+3)

to

for(j = 0; j<=90; j=j+3)

It should be like this

for(int j = 0; j<=90; j += 3) 

but watch out for

for(int j = 0; j<=90; j =+ 3) 

or

for(int j = 0; j<=90; j = j + 3)

Simply try this

for(int i=0; i<5; i=i+2){//value increased by 2
//body
}

OR

for(int i=0; i<5; i+=2){//value increased by 2
//body
}