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 OverflowWhile 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 .
when you are doing a while loop as (i++ < 15) , it checks the condition and stop the loop if i++ is < 15 , But here when you do a do while loop j++ the loop goes 2 times and when it comes to while (i++ < 15) it increaments the value of i by 1 and stops... so in second loop the value of i increases by one but the function inside the while loop remains the same as it stops when i++ is > than 15
IF you do the following you will get 31
int i = 10;
int j = 0;
do {
j++;
System.out.println("loop:" + j);
while (i < 15) { //line6
i++
i = i + 20;
System.out.println(i);
} ;
} while (i < 2);
System.out.println("i_final:" + i);
How to make for loops in Java increase by increments other than 1 - Stack Overflow
java - Control flow for a while loop with post increment - Stack Overflow
Question about incrementation when looping.
How do I use Increment operators (++) to increase a value more than 1?
Videos
You need an additional variable that stores the amount by which you increment. This variable itself has to be incremented by one in each run of the loop.
Try this :
int counter = 8;
int inc = 1;
while ( counter <= 18 )
{
System.out.print ( " " + counter );
if ( counter >= 14 && counter < 18 )
{
System.out.print ( " hello " );
}
counter = counter + inc;
inc += 1;
}
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!
First iteration :
while (i++ <= 10) { // i++ returns 10, so condition is true, i becomes 11
i++; // i becomes 12
}
Second iteration :
while (i++ <= 10) // i++ returns 12, so condition is false, i becomes 13
Therefore the final value of i is 13.
You are using post increment
while (i++ <= 10) { // i will be incremented after evaluating i and do comparaison with 10
i++;
}
You can use the pre increment instead of post increment
while (++i <= 10) { // i will be incremented before comparaison
i++;
}
And the value of i will be 11.
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.
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 :)
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.
When i is 7, the condition i < 8 is still fulfilled, so there is no reason to exit the loop.
It is not very clear to declare the loop variable before the loop and use it afterwards anyway. Rather, consider declaring the loop variable with the loop statement.
int numIterations = 8;
for(int i = 0; i < numIterations; i++) {
// ...
}
// continue doing something with numIterations, or numIterations-1
If using numIterations-1 really bothers you, you could also instead use int maxCounter = 7 and use i <= maxCounter instead as loop invariant.
When you say
for( i = 0; i < 8; i++ )
the middle part, i < 8, is the loop invariant: as long as it holds, there will be a next iteration. Therefore once you leave the loop, it is because the invariant holds no longer and i cannot possibly be less than 8.