For loops allow you to do something a certain number of times in their most basic forms;
Now for foreach loops
They take on the form: for(initial_expression; test_expression; change_expression){ do_something; } This is equivalent to a while statement that looks like initial_expression; while(test_expression){ do_something; change_expression; } This for loop will print the numbers 1 through 100 on separate lines; for($i=1; $i<=100; $i++){ print "$i\n"; }First the variable is set to 1, then it is tested to see if it is less than or equal to 100. Then it is printed. Then the change expression is evaluated and $i is incremented by 1. Then the text expression is evaluated again since $i is equal to 2 it is less than or equal to 100 so the loop is repeated until 100 is printed, $i is incremented to 101, $i is no longer less than or equal to 100 so the for loop is finished.
Now for foreach loops
|
---|
Replies are listed 'Best First'. | |
---|---|
RE: for loops
by nop (Hermit) on Nov 01, 2000 at 21:28 UTC | |
by mirey (Initiate) on Nov 02, 2000 at 04:51 UTC | |
by hossman (Prior) on Oct 11, 2004 at 01:09 UTC | |
by apotheon (Deacon) on Oct 11, 2004 at 00:58 UTC | |
RE: for loops
by Falthor (Initiate) on Jun 02, 2000 at 02:47 UTC | |
by davorg (Chancellor) on Jun 29, 2000 at 16:51 UTC | |
by Anonymous Monk on Jun 28, 2000 at 05:20 UTC | |
by Anonymous Monk on Jun 29, 2000 at 15:37 UTC | |
Re: for loops
by Willman023 (Scribe) on Nov 07, 2002 at 16:30 UTC | |
| |
Re: for loops
by xalphabetx (Initiate) on Mar 21, 2002 at 06:49 UTC | |
by amarceluk (Beadle) on May 25, 2002 at 16:54 UTC | |
by apotheon (Deacon) on Oct 11, 2004 at 01:02 UTC |
Back to
Tutorials