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
increment variable in while loop
Darcy Phillips is having issues with: Hi, I am being asked to construct a while loop that iterates over the elements of the "numbers" array, and increments the "counter" variable wit... More on teamtreehouse.com
๐ŸŒ teamtreehouse.com
1
May 4, 2016
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
๐ŸŒ
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!

๐ŸŒ
Dot Net Perls
dotnetperls.com โ€บ while-java
Java - while Loop Examples - Dot Net Perls
November 10, 2023 - With the while-loop, a block of ... loop based on both "i" and "z." This could be expressed with a for-loop, but it might be less clear. And We increment "i" upwards and decrement "z" downwards....
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 โ€ฆโ€ฆ.
๐ŸŒ
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.
๐ŸŒ
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]
๐ŸŒ
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.
๐ŸŒ
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 :)

๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_while_loop.asp
Java While Loop
In the next chapter, you will learn about the do while loop, which always runs the code at least once before checking the condition. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ยท If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com ยท HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
๐ŸŒ
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 ...
๐ŸŒ
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.

๐ŸŒ
Home and Learn
homeandlearn.co.uk โ€บ java โ€บ java_for_loops.html
java for complete beginners - for loops
This is called incrementing the variable. It is so common that the shorthand notation variable++ was invented: ... The value of some_number will be 1 when the code above is executed. It is the short way of saying this: int some_number = 0; some_number = some_number + 1; ... Loop Start value: 0 Keep Looping While: Start value is less than 11 How to advance to the end value: Keep adding 1 to the start value
๐ŸŒ
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, ...