Conditional Loops

Looping for Counting

Introduction

 Looping consists of going through a list of items and doing something about each element of the list. To perform this action, you must:
• Have/Indicate a starting point
• Indicate how to move to the next item
• Specify how/where to stop

There are various types of loops you can use. The most fundamental consists of counting numbers from one starting point to an ending value while incrementing the number. The formula to perform this opeation is:

for(\$variable-name = start-value; condition; \$variable-name++ | \$variable-name--)
statement

The for keyword is required and it has parentheses. In the parentheses, declare a variable and initialize with a value by which to start. After its required semicolon, create a Boolean expression that specify a condition by which the coundting will stop. After its required semicolon, specify a way to move to the next value. This can be an incrementing operation. In the body of the condition, create the desired statement. Here is an example:

<?php
for(\$counter = 1; \$counter <= 10; \$counter++)
echo "Number: " . \$counter . "<br>"
?>

This would produce:

The above example counted the numbers up. If you want to count down, decrement the value of the variable in the parentheses. In this case, the starting point must be high and the condition should include a comparison for higher value. Here is an example:

<?php
for(\$counter = 48; \$counter >= 38; \$counter--)
echo "Number: " . \$counter . "<br>";
?>

This would produce:

The Body of a Loop

The body of a loop is the section from its closing parentheses to the end of its statement. That body can be delimited with curly brackets. In this case, the statement must end with a semicolon. Here is an example:

<?php
for(\$counter = 1; \$counter <= 10; \$counter++) {
echo "Number: " . \$counter . "<br>"
}
?>

You can include any HTML code anywhere in the loop as long as you delimit the loop code with its own PHP delimitiers. Here is an example:

<?php
for(\$counter = 1; \$counter <= 5; \$counter++) {
echo "Number: " . \$counter . "<br>"; ?>

<p>Something here!</p>

<?php } ?>

This would produce:

The delimiting is required if the body of the loop is made of various lines of code.

As an alternative, you can enf the line with the for code with a colon. To indicate the end of the loop, use the endfor keyword. Also, the statement(s) must end with a semicolon. Here is an example:

<?php
for(\$counter = 1; \$counter <= 10; \$counter++) :
echo "Number: " . \$counter . "<br>";
endfor
?>

Managing the Factors of a For Loop

As seen above the parentheses of a for loop are made in three sections. If you already have the starting point before the for loop is accessed, you can omit that starting point but its semicolon must be represented. Here is an example:

<?php
\$counter = 1;

for(; \$counter <= 10; \$counter++)
echo "Number: " . \$counter . "<br>";
?>

If you have a way to increment the variable in the body of the loop, you can omit it in the parentheses of a for loop. Here is an example:

<?php
\$counter = 1;

for(; \$counter <= 10; )
{
echo "Number: " . \$counter . "<br>";
\$counter++;
}
?>

In fact, you can create a loop whose three factors are defined outside the parentheses. The parentheses must still include their two required semicolons. This would be done as follows:

for(;;)
statement

Looping While a Condition is Verified

Introduction

PHP provide a means to perform an action as long as a certain condition is true. This can be done using the while keyword. The formula to follow is:

while(Condition)
statement(s)

Before stating the condition, you should have a primary expression against which the condition would be checked. For example, if you are planning to count some numbers, you can specify by what value to start. This can be done as follows:

<?php
\$counter = 1
?>

In the parentheses of while, include a Boolean expression that involves the variable and can be assessed as being true or false. In the body of the condition, create a statement. That statement will execute as long as the parenthesed condition is true. Here is an example (don't run the following program):

<?php
\$counter = 1;

while(\$counter <= 10)
echo "<br>Number: " . \$counter
?>

This would produce:

Such a statement would run forever (or until the computer memory or the processor cannot take it anymore). This means that you must specify a condition by which the loop should stop. If you are counting some numbers as in our example, you can ask the parser to keep incrementing the number until a new value reaches a threshold. Here is an example:

<?php
\$counter = 1;

while(\$counter <= 10)
echo "<br>Number: " . \$counter++
?>

The Body While a Condition is True

As seen with other conditional statements, a while loop may include many statements in its body. For this reason, its body can or must be delimited. You have various options. You can delimit the body with curly brackets. Here is an example:

<?php
\$counter = 1;

while(\$counter <= 10) {
echo "<br>Number: " . \$counter
}
?>

Delimiting the statement is a requirement if it is made of various lines of code. Here is an example:

<?php
\$incrementBy5 = 0;

while(\$incrementBy5 <= 42)
{
echo "Number: " . \$incrementBy5 . "<br>";
\$incrementBy5 += 5;
}
?>

This would produce:

As an alternative, you can replace the opening curly bracket of the while condition with a colon and replace the closing curly bracket with the endwhile keyword. Here is an example:

<?php
\$even = 2;

while(\$even <= 21):
echo "Number: " . \$even . "<br>";
\$even += 2;
endwhile
?>

This would produce:

Doing Something While a Condition is True

Instead of first performing an action before checking the condition, you may want to execute the statement, then check the condition and decide whether the condition should execute again. To support this, PHP provides the do operator that is used in conjunction with the while keyword. The formula to follow is:

do
statement(s)
while(condition

Once again, you should first specify the starting point. This time, in the body of the loop, which is after the do keyword, create a statement that will execute. After that statement has executed, specify a condition to check in the parentheses of while. If the condition is true, the statement will execute again. Once again, provide a way by which the statment should stop executing. Here is an example:

<?php
\$counter = 1;

do
echo "Number: " . \$counter++ . "<br>";
while(\$counter <= 10)
?>

This would produce:

The statement can be included in a delimiting body. Here is an example:

<?php
\$counter = 1;

do {
echo "Number: " . \$counter++ . "<br>";
} while(\$counter <= 10)
?>

You must especially delimit the condition if it contains various lines of code.

 Accessories for Managing a Loop Skipping a Step When Looping A loop is generally made to visit each value in a specified range. You can specify a condition so that when the loop gets to it, it should skip the value and not execute the same action performed on the other values. To support this, PHP provides the continue keyword. To use it, set a condition inside the loop and add the continue keyword to that condition. Here are examples: "; } echo "
"; \$number = 0; while(\$number <= 11){ \$number++; if(\$number == 5) continue; echo \$number . " "; } echo "
"; \$number = 13; do{ \$number--; if(\$number == 5) continue; echo \$number . " : "; } while(\$number > 1); ?> This would produce: The first and the second loops indicate that they are counting numbers from 1 to 12 but they are asked to skip 5 when they encounter that number. The third loop is asked to count numbers backward from 12 to 1 but it is asked to skip 5 when it encounters it. Remember that the for (and the while) loop can be delimited by curly brackets or by a colon and the endfor (or the endwhile) keyword. If there is code after one of those keywords, the keyword must end with a semicolon. The above top two loops can be written as follows: "; endfor; echo "
"; \$number = 0; while(\$number <= 11): \$number++; if(\$number == 5) continue; echo \$number . " "; endwhile ?> Breaking a Flow By default, a loop is used to count from one starting point to the end. In some cases, if something happens in the loop, such as if a certain value is reached, you may want to stop whatever the loop is doing. To support this, PHP provides the break keyword. To break the flow of a loop, inside of it, create a condition to check and add break; in it. When the condition is true, the break operator would execute. Here are three examples: "; if(\$gallons == 16) break; } echo "
"; \$letter = 'A'; while(\$letter <= 'Z'){ echo \$letter . ", "; if(\$letter == 'J') break; \$letter++; } echo "
"; \$number = 20; do{ echo \$number . " : "; if(\$number == 11) break; \$number--; } while(\$number >= 0); ?> In the first example, a customer is purchasing gas for a vehicle with an empty tank that can take up to 42 gallons but the customer wants only 16 gallons. In the second example, a loop is supposed to display letters from A to Z, but instead, it is asked to stop (break) if/when it reaches J. In the third example, a loop is supposed to count backward from 20 to 0, but it is asked to stop (break) if it gets to 11. The examples would produce: Remember that the for and the while loops can use the colon and the endfor/endwhile to delimit their bodies. If code comes after the ending word, it must be followed by a semicolon. This can be done as follows: "; if(\$gallons == 16) break; endfor; echo "
"; \$letter = 'A'; while(\$letter <= 'Z') : echo \$letter . ", "; if(\$letter == 'J') break; \$letter++; endwhile; ?>