View previous topic :: View next topic |
Author |
Message |
nilsener
Joined: 06 Dec 2005 Posts: 59
|
for loop question [DONE] |
Posted: Wed Sep 03, 2008 2:35 am |
|
|
Dear,
i am always unsure if this loop will be executed 3 or 4 times?
Code: |
for (i=1;i<4;i++);
{
Code
} |
Thanks for help
Best Regards
nilsener
Last edited by nilsener on Wed Sep 03, 2008 4:50 am; edited 1 time in total |
|
|
jacqueskleynhans
Joined: 10 Apr 2008 Posts: 109 Location: Cape Town, South Africa
|
|
Posted: Wed Sep 03, 2008 3:05 am |
|
|
this will run for i=1 to i=3
So 3 times.
J _________________ "THE ONLY EASY DAY WAS YESTERDAY" |
|
|
Ttelmah Guest
|
|
Posted: Wed Sep 03, 2008 3:05 am |
|
|
The way 'for' executes is:
First the initialisation (i=1)
Then test - if false exit - don't execute body
Then loop body.
Then increment (i++)
Loop back to the test.
So, where the test applies, on the first pass, i=1, then i=2, then i=3 then i=4 - fail don't execute body.
So the body executes three times for the example as given.
Best Wishes |
|
|
nilsener
Joined: 06 Dec 2005 Posts: 59
|
|
Posted: Wed Sep 03, 2008 4:50 am |
|
|
Thanks you very much for your answers, now I understand it.
Best Regads
nilsener |
|
|
|