+ 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 ++

3rd Jul 2018, 12:16 AM
Quadier Mackey
Quadier Mackey - avatar
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
3rd Jul 2018, 12:25 AM
Agent
Agent - avatar
+ 1
oooooohhhh i forgot about true or false. thank you so much. it keeps running as long as statement is true. boom. got it.
3rd Jul 2018, 1:35 AM
Quadier Mackey
Quadier Mackey - avatar