+ 2

What that means

say this for an instance we have a while loop in java program now at the end we did something like this x = x + 1; or x = x - 1; I know that's related to the iteration point but how we pronounce both lines to understand

21st May 2018, 11:56 AM
AppInventor
AppInventor - avatar
4 Respostas
+ 2
Janet Jane Jhon well it's inside the loop int x = 4; while ( x > 0 ){ System.out.println("This is a loop"); x = x + 1; // I m talking about this line
22nd May 2018, 10:56 AM
AppInventor
AppInventor - avatar
+ 1
What do u mean? is x= Inside the while loop statement or outside? If outside it will execute regardless. If inside it will execute as long as thr while boolean is true.
22nd May 2018, 2:11 AM
Apple Blossom
Apple Blossom - avatar
0
Be more clear
21st May 2018, 1:52 PM
Jhon
Jhon - avatar
0
In your example, that loop will run forever because the condition x > 0 will always be true. To avoid that you should replace x = x + 1 with x = x -1 or in short notation x--; Now, if you declare x outside or inside the loop, the performance will no be affected but, if it is declared outside the while loop, then you could change its value outside that loop. I suggest declaring that variable inside the loop if you are only going to use it for the purpose of controlling the loop.
22nd May 2018, 11:59 AM
Jhon
Jhon - avatar