-
Help me understand For Looping
Someone please help me understand for looping. I don't get it. I'm so lost.
-
Mayor of Awesometown
Example:
int i;
for(i = 0; i < 10;i++)
{
//stuff
}
Program execution gets to the loop, and begins to execute the loop. The first part (i = 0) is initialization of the loop variable (the var that controls how many times the loop is executed). The second part is the condition - the loop will coninue to execute as long as this true. Our loop will execute as long as i's value is less than 10. The last part (i++) is incerementing the loop variable. This allows a way to eventually exit the loop (since incrementing i repeatedly will eventually get it to 10)
1st pass:
i=0, i<10, go into loop
2md pass:
i=1, i<10, go into loop
3rd pass:
i=2, i<10, go into loop
4th pass:
i=3, i<10, go into loop
...
Until
10th pass:
i = 10, i is not <10, skip over loop and continue execution.
-
Popular pages
Recent additions