+ 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
4 Respuestas
+ 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
+ 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.
0
Be more clear
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.