Aims:
for
-loops
for bounded repetition
foreach
for
for
-loopsecho
statement exactly 5 times:
for ( $i = 0; $i < 5; $i++ ) { echo "<p>{$i}</p>"; }
In the example,
$i
is referred to
as the loop variable (or 'loop counter')
$i = 0
initialises
the loop variable
$i < 5
is called
the loop test (or 'loop condition')
echo
statement) is called the
loop body
$i
is initialised to zero: $i = 0
$i < 5
$i++
)
The example is a for
-loop where $i
counts from 0 to 4 inclusive but stops when $i
is 5
(hence the body is executed 5 times)
$i
counts from 0 to 99
inclusive. How many times is the loop body executed?
$j
counts from 0 to 100
inclusive. How many times is the loop body executed?
$num_exams
contains the
number of exams a student must sit. Write a loop that
executes its loop body exactly $num_exams
times
exercise.html
contains this form:
<form action="exercise.php" method="get"> <input type="text" name="number" /> <input type="submit" /> </form>Write
exercise.php
, which outputs
(as an unordered list) a
multiplication table ('times table')
from 1 up to 10
for the number that
the user enters
exercise.html
), write exercise.php
,
which outputs the sum of all the integers from 1 up to
the number the user entered (inclusive)
int factorial( int $n )