Sunday, June 12, 2016

Looping for Meeting 10

Basically "PHP" has 3 kinds of loop, namely, "for" statement, "while" statement, and "do ... while" statement. Below we learn on "for" statement.

The regular expression of "for" statement is like this:
for (initialization; condition(s) or bound(s); increment or decrement) {
// code to be executed
}

The expressions within the parentheses of the "for" statement are separated by semicolons. Usually, the first expression initializes an $i (as a counter) variable, the second expression is the test condition for the loop, and the third expression increments or decrements the counter depending on the condition given. Below we go directly to the example:

<?php
for ($i=1; $i<=10; $i++) {
echo "$i * 10 = ".($i * 10)."<br />";
}
?>

save it as loopfor1.php and open via your browser. The result should be like this:

1 * 10 = 10
2 * 10 = 20
3 * 10 = 30
4 * 10 = 40
5 * 10 = 50
6 * 10 = 60
7 * 10 = 70
8 * 10 = 80
9 * 10 = 90
10 * 10 = 100

One thing to watch in "for" statement is that "dont forget to write the condition(s) expression" to stop looping so that the program will not loop infinitely.