+ 1
What are the usages for the 3 parts of the for loop and how do you use them?
I'm still not very sure... can someone explain it to me?
2 Respostas
+ 2
Example:
for( i=0;i<10;i++){}
The "i=0" is the initializer of your loop, the "i<10" is the limit of looping, and "i++" represents how your loop will work, increasing (++) or decreasing (--).
+ 2
for (initialization; condition; change)
{
body
}
First the "initialization" is done.
Then the "condition" is checked.
If the condition is true, the "body" of the loop is executed. If not, control passes to the statement (if any) directly after the body of the loop.
After the body is executed, the "change" is done. This can be increment, decrement of the initialized variable(s) or something else.
Now the condition is checked again & the procedure repeats as before.
Observe that this is just like the while loop as shown below.
initialization;
while (condition)
{
body;
change;
}