+ 1

Ok I'm confused with the concept of the for Loop

13th Apr 2016, 2:41 AM
EJ6612
EJ6612 - avatar
4 ответов
+ 6
Translate the code to a natural language statement to understand it better, consider the following code: int x = 1; for(x > 4) { x++; } can be translated to "Test if x is greater than 4, then add 1 to x until x is greater than 4 then stop." The purpose of a loop is to repeat a particular operation/assignment until its pre-condition is met.
15th May 2016, 9:30 AM
Jarred Naicker
Jarred Naicker - avatar
+ 2
Jarred's answer is the most accurate. You're simply executing the same function x number of times until a particular condition is met. Similar to a while loop, except the for loop and/or for each loop are best used when iterating over a collection ... I find for loops are more useful when iterating over indexes of a collection and I use foreach loops when there is a list of objects whose values I want to do something with.
30th Jul 2016, 8:45 PM
Mike
Mike - avatar
+ 1
A more simple answer. In terms of a game. If you only want your player to have 3 lives before dying, you can use the for loop. He will start at 3. And it will decrease by 1 everytime he dies. When lives reach 0, it would go to game over.
18th Jun 2016, 10:13 PM
Raymond Feliciana
Raymond Feliciana - avatar
0
In simplest form, a for loop is a quick combination of a counter controlled while loop. In the case of the while loop, you would have to declare your control variables outside the loop and increment inside the loop. The for loop provides a more efficient way of writing the same code.
23rd Jun 2016, 6:37 PM
Andy Amaya
Andy Amaya - avatar