While loop will be executed twice before its break.

while (i++ < 15) {                //line6 
                i = i + 20; 
                System.out.println(i); 
            } ;

First it increment to 11 .

Check with 15. Returns true.

Now it increments to 31. (i = i + 20)

now again while loop .

it increments the value .

Answer from Siva Kumar on Stack Overflow
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ java-while-loop
Java While Loop
March 28, 2025 - We used the Java While loop, and ... given values: Number = 7. Next, we initialized the sum = 0 ... Next, the number will be incremented by 1 (number ++)....
Discussions

How to make for loops in Java increase by increments other than 1 - Stack Overflow
The longhand form of "++j" is "j ... as terse a shorthand as incrementing by one. "j + 3", as you know by now, doesn't actually change j; it's an expression whose evaluation has no effect. ... In this loop you are using shorthand provided by java language which means a postfix ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Control flow for a while loop with post increment - Stack Overflow
I am not able to figure out how the following code snippet prints 13 as an output. As far as I can see, the while condition should keep looping as i is always less than 10 in this case. I tried deb... More on stackoverflow.com
๐ŸŒ stackoverflow.com
March 16, 2015
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
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
๐ŸŒ
Dot Net Perls
dotnetperls.com โ€บ while-java
Java - while Loop Examples - Dot Net Perls
November 10, 2023 - 0; // Use post increment in while-loop expression. while (index++ < ... Loops seem simpleโ€”but we find the most complex algorithms are based mainly on loops. It is important to clearly understand Java loops.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 685214 โ€บ java โ€บ increment-expression-loop
increment expression in for loop [Solved] (Beginning Java forum at Coderanch)
Currently, the only advantage this has is that when you see the ++ before the i, it's easier to read it as "increment i". 5. Put either i++ or ++i as the "increment" expression in the parens for the for statement(); That's where everyone will be looking for it, so that's where it should go. This will help improve code maintainability quite a bit. ... Thanks much for your replies and time. ... Or consider using Java 8 facilities along with the streams.
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!

Find elsewhere
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2015 โ€บ 03 โ€บ while-loop-in-java-with-examples
While loop in Java with examples
First of allโ€ฆ.. The initialization done with i=0 Then goto while loop and check the condition i<4(i=0) It is true goto the loop body execute the looping statement i.e., args[0] Then increment the i value by 1 After incrementing again check the while loop condition โ€ฆโ€ฆ.
๐ŸŒ
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.
๐ŸŒ
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
Answer (1 of 12): Assuming you want to make use of the above sequence of values in some way, there are 2 basic possibilities: a) donโ€™t use a for loop, use a while loop, and increment the counter within the loop. Set the counter to 1 before loop entry, then after you have used the counter value, ...
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ java-do-while-loop
Java Do While Loop
March 25, 2025 - Next, it will enter into the flow. It will execute the group of statements inside it. Next, we have to use Increment & Decrement Operator inside the Java do while loop to increment or decrease the value.
๐ŸŒ
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 :)

๐ŸŒ
Funnel Garden
funnelgarden.com โ€บ java-for-loop
Java For Loop, For-Each Loop, While, Do-While Loop (ULTIMATE GUIDE)
We also increment the local variable inside the loop body. [cc lang="java" escaped="true"] int [] intArray = {1, 3, 5, 7, 9}; int i=0; while(i<intArray.length) { System.out.println(intArray[i++]); } [/cc]
๐ŸŒ
Trinket
books.trinket.io โ€บ thinkjava2 โ€บ chapter6.html
Loops and Strings | Think Java | Trinket
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: ... The loops we have written so far have three parts in common. They start by initializing a variable, they have a condition that depends on that variable, ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ can someone explain increment and decrement operator in a while loop?
r/learnjava on Reddit: Can someone explain increment and decrement operator in a while loop?
January 15, 2020 -

I've been doing the MOOC Introduction to OOP course part 1 for a while, and I've been having a blast. I'm used to python, and no other language has got my attention like this, for some reason.

There is a thing, I don't quite understand. The increment operator in it self, raises the value of your variable by 1. However, in several excercises now, we have been doing something similar to this:

import java.util.Scanner;

public class ManyPrints {
    // NOTE: do not change the method definition, e.g. add parameters to method
    public static void printText() {
        System.out.println("In the beginning there were the swamp, the hoe and Java");
        // Write your code here
    }

    public static void main(String[] args) {
        // ask the user how many times the text should be printed
        // use the while structure to call the printText method several times
        Scanner reader = new Scanner(System.in);
        int num = 0;
        System.out.println("How many? ");
        int times = Integer.parseInt(reader.nextLine());
        
        
        while(num <= times) {
            printText();
            num++;
        }
    }
}

On the first try, I didn't even have the increment operator in my code, and it would just constantly print my code. Then I thought of this, because we have been doing it a lot, and it worked. However, I don't understand why. Why does the increment operator, make the program able to stop after the number in the user input? Why doesn't it just keep increasing the value by 1?
Sure while num is less or equal to times that part of the code runs, but if i remove the increment operator, it would be infinite.

Thank you.

๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_while_loop.asp
Java While Loop
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... Loops can execute a block of code as long as a specified condition is true. Loops are handy because they save time, reduce errors, and they make code more readable. The while loop repeats a block of code as long as the specified condition is true:
๐ŸŒ
CodingBat
codingbat.com โ€บ doc โ€บ java-for-while-loops.html
CodingBat Java For While Loops
Sometimes, some cleanup is required at the end of the body to set things up for the next iteration. In the above example, the line "count = count + 1;" accomplishes the increment. That can also be written as "count++;". Here's a while loop example that uses a loop to see how man times you can ...