+ 1
Increments random numbers
In the for loop when creating random numbes, why do I have to place the increment at the end of the code. “; x++” And why does the for loop keep running when I just put “X” without ++
2 Réponses
+ 4
Hi Quadier Mackey
Code:
for (int x = 1; x <= 10; x++)
First you declare x and it is equal to 1 which is initialised
Then you have the condition which is if x is less than ten
Lastly, you have the increment of x
if “x++” was not put at the end as it is the sytax as shown:
for ( init; condition; increment ) {
statement(s);
}
Your loop keeps running when you just put “x” becuase the statements in the loop will be true forever
X will always be 1 and less or equal to 10 so the cose will run forever
+ 1
oooooohhhh i forgot about true or false. thank you so much. it keeps running as long as statement is true. boom. got it.