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) { }
Answer from user557219 on Stack Overflow
🌐
Java2Blog
java2blog.com › home › core java › java basics › increment for loop by 2 in java
Increment for Loop by 2 in Java - Java2Blog
September 24, 2022 - Let’s say we want print all even numbers between 1 to 20 in Java. We can write a for loop and start the loop from 2 by using i=2 in initialization part and increment the loop by 2 in each iteration by using i+=2.
🌐
JavaBeat
javabeat.net › home › how to increment a java for loop by 2
How to Increment a Java for Loop by 2
July 23, 2023 - Specify a “for” loop that iterates along the values from “0” till “7” with a step of “2” using the “addition assignment operator (+=)”. It is such that upon each iteration, the iterated values will be incremented by “2”.
Discussions

Java: Increment by 2 the two inputted integer - Stack Overflow
Can someone help me please.. just starting java..:( How can I display all possible values based on the given minimum input value, maximum input value and the incrementing value? for example: min v... More on stackoverflow.com
🌐 stackoverflow.com
How do I use Increment operators (++) to increase a value more than 1?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
32
13
March 6, 2022
java - for-loop, increment by double - Stack Overflow
I want to use the for loop for my problem, not while. Is it possible to do the following?: for(double i = 0; i More on stackoverflow.com
🌐 stackoverflow.com
Which is better (technically) ++i or i++ in a for loop?
++i is strictly better for iteration. It simply increments the value of i. When not optimized, i++ creates a copy of i's value, increments i, then returns the copy it made earlier. Of course, in an optimized build, these two would be exactly equivalent. But index loops can sometimes change to more abstract iterator loops in future code revisions. Because the implementation of those iterators might be overly complicated for the optimizer or because it might be in another translation unit, it is possible that the post-increment version will be slower. Instead of reasoning which cases it is ok to use i++ in, most people just use the always optimal ++i. More on reddit.com
🌐 r/cpp_questions
18
22
October 4, 2018
Top answer
1 of 13
137

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) { }
2 of 13
48

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!

🌐
Blogger
javahungry.blogspot.com › 2023 › 05 › increment-for-loop-by-2.html
Increment for Loop by 2 in Java with examples | Java Hungry
To increment for loop by 2 in Java, we only need to change the increment/decrement part of the for loop.
🌐
Coderanch
coderanch.com › t › 685214 › java › increment-expression-loop
increment expression in for loop [Solved] (Beginning Java forum at Coderanch)
3: for(int i=4; i<10 ; ) { 4: i = i++; 5: System.out.println( i); 6: } Scenario 1: If line 4 is i = i++; runs into infinite loop Scenario 2: If line 4 is replaced by i++; output displayed from 5 to 9 ... as expected Issue: In Scenario 1: first iteration starts with i =4 in line 3, In line 4, i is assigned 4 ---- because of the postunary ++ operator, my expectation was i to be incremented to 5 after assigning to itself.
🌐
Reddit
reddit.com › r/javahelp › how do i use increment operators (++) to increase a value more than 1?
r/javahelp on Reddit: How do I use Increment operators (++) to increase a value more than 1?
March 6, 2022 -

Just to preface, this isnt homework - Im self learning. Ive used Stack overflow but I dont think im searching the right thing, so I cant find what Im looking for

For my study book (Learn Java the Hard Way) , i'm on an exercise where I have to increase

i = 5;

to 10 by only using ++ , i know I could do

i++; on repeated lines but my study drill says I can do it on one. Which I would like to figure out

"Add code below the other Study Drill that resets i’s value to 5, then using only ++, change i’s value to 10 and display it again. You may change the value using several lines of code or with just one line if you can figure it out. "

Perhaps ive read it wrong but I cant find a way of using only ++ to do this in one line.

Ty for the help :)

Find elsewhere
🌐
Sololearn
sololearn.com › en › Discuss › 1492044 › what-to-do-to-increment-by-2-instead-of-1-
What to do to increment by 2 instead of 1 ?
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
How to do in Java
howtodoinjava.com › home › java flow control › for loop
Java For Loop (with Examples) - HowToDoInJava
November 20, 2023 - Value at index 0 is 0 Value at index 1 is 1 Value at index 2 is 2 Value at index 3 is 3 Value at index 4 is 4 ... First, 'int i = 0' is executed, which declares an integer variable i and initializes it to 0. Then, condition-expression (i < array.length) is evaluated. The current value of I is 0 so the expression evaluates to true for the first time. Now, the statement associated with the for-loop statement is executed, which prints the output in the console. Finally i++ is executed that increments the value of i by 1.
🌐
Tutorial Gateway
tutorialgateway.org › java-for-loop
Java For loop
March 25, 2025 - Again it will check the expression after the value is incremented. The statements inside this will execute as long as the condition is true. This program allows the user to enter any integer values. Then it will calculate the sum of natural numbers up to the user’s entered number using the for loop. package Loops; import java.util.Scanner; public class ForLoop { private static Scanner sc; public static void main(String[] args) { int i, number, sum = 1; sc = new Scanner(System.in); System.out.println("\n Please Enter the any integer Value: "); number = sc.nextInt(); for (i = 1; i <= number; i++) { sum = sum * i; } System.out.format(" Sum of the Numbers From the is: %d ", sum); } }
🌐
Trinket
books.trinket.io › thinkjava2 › chapter6.html
Loops and Strings | Think Java | Trinket
Specifically, ++ is the increment operator; it has the same effect as i = i + 1. And -- is the decrement operator; it has the same effect as i = i - 1. If you want to increment or decrement a variable by an amount other than 1, you can use += and -=. For example, i += 2 increments i by 2:
🌐
Quora
quora.com › How-do-you-code-a-for-loop-that-increments-by-two-numbers-at-once
How to code a for loop that increments by two numbers at once - Quora
Each counter was decremented at the end of the loop. ... Write better C++ code with less effort. Boost your efficiency with refactorings, code analysis, unit test support, and an integrated debugger. ... Since 1983 at my twelve. Many languages and keep learning · Author has 1.6K answers and 899.7K answer views · 2y · In C, java, C++, php,…. ... Just state that the control variable is incremented by 2 each time.
🌐
Codefinity
codefinity.com › courses › v2 › 8204075c-f832-4cb9-88b1-4e24e74ebdcb › 3e046a04-a59c-4fc2-95d5-5d34cff8249b › d5d1873b-9640-4d8b-94de-d61ed41c8651
Learn Increment and Decrement | Loops
In the first for loop, the variable i is initialized to 0, incremented by 1 after each iteration, and the loop executes until i is no longer less than 5. This will output the numbers from 0 to 4. In the second for loop, the variable j is initialized to 5, decremented by 1 after each iteration, and the loop executes until j is no longer greater than 0. This will output the numbers from 5 to 1 in descending order. Java also allows you to simplify expressions using assignment operators.
🌐
Codecademy
codecademy.com › forum_questions › 508fe06f900ba10200002d7f
the for loop increment | Codecademy
My code for the Blast Off exercise works, so long as my for loops increment section reads as follows (I include the entire line of the for loop) : ... The browser actually freezes, apparently given the change to the increment section. Interestingly, in section 2 of the “Art of Looping” lesson, the for loop’s increment section read as counter = counter + 1.
🌐
Coderanch
coderanch.com › t › 409871 › java › Increment-operators-loop
Increment operators in a for loop (Beginning Java forum at Coderanch)
March 27, 2008 - When starting out, i = 0, it goes through the loop, the condition is checked (true), and it is then passed into the value of 'i': i = ++0(1) + ++1(2) <------- becomes 2 after 1st increment i = 3 The value becomes 3 and then it has to be incremented (i++, i = 4).
🌐
Medium
gdscnuml.medium.com › for-loop-with-2-variables-in-java-and-c-89b7016269a4
For loop with 2 variables in Java and C++ | by Google Developer Student Clubs - NUML | Medium
February 18, 2022 - We use For loop to repeat a specific block of code a known number of times. In it, we use a variable and we keep on incrementing or decrementing that variable until the condition is matched.
🌐
Home and Learn
homeandlearn.co.uk › java › java_for_loops.html
java for complete beginners - for loops
Another way to interrupt the flow from top to bottom is by using loops. A programming loop is one that forces the programme to go back up again. If it is forced back up again you can execute lines of code repeatedly. As an example, suppose you wanted to add up the numbers 1 to 10. You could do it quite easily in Java like this: int addition = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10;