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.
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!

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
Question about incrementation when looping.
In a for(initialization; condition; increment) loop, the "initialization" code is always executed, before anything else happens. Then the loop begins. The loop is equivalent to a while loop in that there is a "condition", and this condition is tested before every iteration -- including the first iteration. (This means your for or while loop might never execute the body of the loop, which is correct.) At the end of the loop body, or the beginning of every iteration except the first, the "increment" part is executed. Thus, the increment does not run before the first time, but does run every other time. The use of a continue statement to advance to the next iteration through the loop will run the increment part. Simplified, it looks like this: initialization part goto condition top-of-loop: body of loop /* continue stmt */ goto end-of-body /* break stmt */ goto break end-of-body: increment part condition: evaluate condition part if truthy goto top-of-loop break: whatever code is after your for loop Note here that truthy in C has a particular meaning: non-zero. Any "condition" type test will compare the result of a contained expression against zero. A zero is considered false, and any value other than zero is considered true. "1" is true. "1000000" is true. "6.02E+23" is true. "&main" is true. "NaN" is true. "Inf" is true. "-Inf" is true. I honestly do not know if -0 is true or not, on a sign+magnitude CPU. Yes, there is a boolean type. But the boolean type was added much, much, much later. Before there was a boolean type, everything was int, and so the foundation of the language assumes that the truthiness of int value: 0 vs. non-0, drives everything. More on reddit.com
🌐 r/cprogramming
12
11
February 7, 2023
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
🌐
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 - By default, a “for” loop in Java increments by “1” but it can be incremented or decremented by “2” with the help of the addition assignment operator “+=”. This increment or decrement can be achieved in a custom manner with any ...
🌐
Blogger
javahungry.blogspot.com › 2023 › 05 › increment-for-loop-by-2.html
Increment for Loop by 2 in Java with examples | Java Hungry
Just like above we can increment for loop by n i.e. 2,3,4,5,6. We just need to replace the increment or decrement part of the for loop by i+=n as shown below in the example. Simple Java program where we will increment for loop by 5.
🌐
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 < ...
🌐
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.
Find elsewhere
🌐
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 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. In general, if increment increases the value of a variable by 1, and decrement decreases it by 1, then with assignment operators, we can customize any operation. For example,
🌐
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 › Is-it-possible-for-a-for-loop-in-Java-to-go-up-in-different-increments-I-want-to-write-a-loop-that-starts-with-1-but-then-goes-up-to-100-200-all-the-way-until-1000-How-would-you-write-a-code-to-do-that
Is it possible for a 'for loop' in Java to go up in different increments? I want to write a loop that starts with 1, but then goes up to 100, 200, all the way until 1000. How would you write a code to do that? - Quora
Set the counter to 1 before loop ... value, increment it by either 99 (if counter value < 100) or 100 (if counter value is greater than or equal to 100) b) A more general solution: Preload an array with the counter values you are going to want, using a literal assignment. Use a for loop (going from 0 to 10 in your example, and use the for loop index to get the count ... Assuming you want to make use of the above sequence of values in some way, there are 2 basic ...
🌐
YouTube
youtube.com › watch
For loop and increments in Java - Programming tutorial - YouTube
How does the for loop works in Java? learn it with a basic example.__Thank you for watching this video, if you like it please don't forget to like it, or sub...
Published   May 9, 2014
🌐
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.
🌐
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 :)

🌐
Tutorial Gateway
tutorialgateway.org › java-for-loop
Java For loop
March 25, 2025 - Next, i value will incremented by 1 (i++). Please refer to the Increment and Decrement Operators article to understand this ++ notation. Java for loop Second Iteration: Within the first Iteration, the values of i changed as i = 2. Next, it will ...
🌐
Reddit
reddit.com › r/cprogramming › question about incrementation when looping.
r/cprogramming on Reddit: Question about incrementation when looping.
February 7, 2023 -

Hi. As the title state my question is about incrementation when looping. I understand it may sound stupid and for that I apologize.

When you use a nested for loop you write :

for(int i=0; i<5; i++) { for(int j=0; j<5; j++) { Task; } }

My question is when exactly does incrementing happen? Does i get incremented then go into the inner for loop? Does j complete the task then get incremented?

Thank you to anyone who help. I also apologize if I'm not making sense.

Top answer
1 of 5
2
In a for(initialization; condition; increment) loop, the "initialization" code is always executed, before anything else happens. Then the loop begins. The loop is equivalent to a while loop in that there is a "condition", and this condition is tested before every iteration -- including the first iteration. (This means your for or while loop might never execute the body of the loop, which is correct.) At the end of the loop body, or the beginning of every iteration except the first, the "increment" part is executed. Thus, the increment does not run before the first time, but does run every other time. The use of a continue statement to advance to the next iteration through the loop will run the increment part. Simplified, it looks like this: initialization part goto condition top-of-loop: body of loop /* continue stmt */ goto end-of-body /* break stmt */ goto break end-of-body: increment part condition: evaluate condition part if truthy goto top-of-loop break: whatever code is after your for loop Note here that truthy in C has a particular meaning: non-zero. Any "condition" type test will compare the result of a contained expression against zero. A zero is considered false, and any value other than zero is considered true. "1" is true. "1000000" is true. "6.02E+23" is true. "&main" is true. "NaN" is true. "Inf" is true. "-Inf" is true. I honestly do not know if -0 is true or not, on a sign+magnitude CPU. Yes, there is a boolean type. But the boolean type was added much, much, much later. Before there was a boolean type, everything was int, and so the foundation of the language assumes that the truthiness of int value: 0 vs. non-0, drives everything.
2 of 5
1
You could try it, for example, by printing i and j as your task. What should happen is: i=0, j=0, task i=0, j=1, task i=0, j=2, task i=0, j=3, task i=0, j=4, task i=1, j=0, task i=1, j=1, task ... So the incrementing happens before task, if I understand you question correctly.
🌐
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.
🌐
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 - i that will increment from 0 to 9 and j that will decrement from 10 to 1. ... Let’s see how to use For loop with 2 variables in different Programming Languages.
🌐
freeCodeCamp
freecodecamp.org › news › for-loop-in-java-foreach-loop-syntax-example
For Loop in Java + forEach Loop Syntax Example
February 7, 2022 - Incrementing to 6 is impossible ... values of an array. int[] randomNumbers = {2, 5, 4, 7}; for (int i = 0; i < randomNumbers.length; i++) { System.out.println(randomNumbers[i]); } // 2 // 5 // 4 // 7...