Friday, November 28, 2008

Lesson 6 : While Loops

Objective
This lesson will teach you how to use While Loops to count and process data. A
While Loop can be used to count down or count up to a certain point.

Step 1
Open a new window in your Text Editing Program.
Step 2
Type in the lines just as they appear in text given below:


$x = 1;
while ($x < 5) {
echo ‘Current Value: ‘, $x , ‘
’;
$x = $x + 1;
}


Step 3
Name this file while.php and save it in your Project Folder.
Step 4
Upload while.php to your server.
Step 5
View the page (www.YourWebDomain.com/while.php) in your Web Browser. You
should see a display similar to what appears in text given below:


Current Value: 1
Current Value: 2
Current Value: 3
Current Value: 4
Current Value: 5


We just created a file that creates a variable $x and then uses a While Loop to
display and manipulate the value contained in variable $x.
When the While Loop begins, $x is equal to 1. The While Loop executes until $x
has a value that is greater than 5.

Line 2

$x = 1;

This line creates the variable $x and gives it a value of 1.

Line 3

while ($x < 5) {

This line begins the While Loop. The code between the parenthesis is the Test.
The code between the brackets will execute, and then return back to the Test. If
the Test is still valid, the code between the brackets will execute again.

Line 4

echo ‘Current Value: ‘, $x , ‘
’;

This is the first line of code that appears between the brackets. Since the Test
(line 3) is valid, the code between the echo statement is printed to the screen.

Line 5

$x = $x + 1;

This is the second line of code that appears between the brackets. This code
takes the current value of $x and adds 1 to that value. If the value of $x is
currently 1, the value of $x will be 2 after this line has been executed.


Note: If you do not increment the value of $x, the statement ($x < 5) will always be
true and the While Loop will continue to execute for an infinite number of times.
This can cause many problems.


Line 6

}

This line ends the While Loop. All While Loops must have a beginning and
ending bracket.

You can run multiple tests at once during a While Loop. To run multiple tests at
once you must use And/Or Operators.

When you use an OR Operator, the While Loop will execute as long as either of
the Tests is true. OR is represented by two pipe characters:

When you use an AND Operator, the While Loop will execute only as long as
both of the Tests are true. AND is represented by two ampersands: &&
And/Or Operators can also be used with If/Else Statements.

The AND/OR perators are listed in the table below:


Example Meaning

$x < 5 $y < 20 While $x is less than 5 OR $y is less than 20
$x < 5 && $y < 20 While $x is less than 5 AND $y is less than 20


In this exercise we will use an OR to test multiple variables at the same time.


$x = 1;
$y = 2;
while ($x < 5 $y < 20) {
echo ‘X: ‘, $x ,’ and Y: ‘, $y , ‘
’;
$x = $x + 1;
$y = $y + 2;
}
?>

Try changing the values of $x and $y and changing the values in the While Loop
to create different results.


Conclusion
In Lesson 6 we learned how to use While Loops to increment variables and
process data. While Loops will become more useful in the future but their
concepts are important to understand as we move forward.

In Lesson 7 we will more uses of While Loops and If/Else Statements. We will
learn how to integrate these with Forms to produce an interactive webpage.

No comments: