Get support from Xavier Media
It is currently Sun Dec 08, 2013 12:27 pm

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Lesson 12 - Advanced Loops
PostPosted: Thu Jul 13, 2006 7:49 pm 

Points:
Now that we have had a little more practice with control structures I want to introduce the last ones to you. They are "Do while", "switch", "while", "elseif", and "for". First lets start with "switch".

Switch/Case loops:


Lets say you wanted to find the city someone was from, with a "for" loop your code might look like this:

[php] <?php
$city = "Temecula";
if ($city == "L.A.") {
echo "You live in L.A.";
} else {
if ($city == "Dallas") {
echo "You live in Dallas";
} else {
if ($city == "New York") {
echo "You live in New York";
} else {
if ($city == "Washington") {
echo "You live in Washington";
} else {
if ($city == "Phoenix") {
echo "You live in Phoenix";
} else {
echo "You live somewhere else...";
}
}
}
}
}
?>[/php]


However, if you were to use a "switch" or "case" loop you could make your code a little more friendlier:


[php] <?php
$city = "Temecula";
switch($city) {
case "L.A.": echo "You live in L.A."; break;
case "Dallas": echo "You live in Dallas"; break;
case "New York": echo "You live in New York"; break;
case "Washington": echo "You live in Washington"; break;
case "Phoenix": echo "You live in Phoenix"; break;
default: echo "You live somewhere else...";
}
?>[/php]


As you can see this saves a little more room than an "if" statement. I want you to notice that there is a "break" after each "case" statement. "Break" tells the computer to "stop the code! break out of it!" This keeps PHP from executing each following case statement. These "break" commands are not required, however, without them the case statement will execute every case statement following the "case" statement that is TRUE.


For example if the code was:


[php] <?php
$city = "Temecula";
switch($city) {
case "L.A.": echo "You live in L.A.";
case "Dallas": echo "You live in Dallas";
case "Temecula": echo "You live in Temecula";
case "Washington": echo "You live in Washington";
case "Phoenix": echo "You live in Phoenix";
default: echo "You live somewhere else...";
}
?>[/php]



You would see this print out:


Code:
You live in Temecula
You live in Washington
You live in Phoenix
You live somewhere else...



Now don't get me wrong; this is VERY handy for a lot of things, but in this case we want to "break" out of the switch code so we added "break;". One last thing I want you to remember about switch loops is that you don't put a "case" command before the "default" because there is no case left that is why it is the default.




For Loops


There are three parts to a FOR loop: the variable, the condition, and the action. First you define a variable for the loop, then you check that variable against the condition to see if it is TRUE, and last you set an action to happen to the variable at the end of each iteration through the loop. For example:


[php] <?php
for ($x = 0; $x < 10; $x = $x + 1) {
echo "$x";
}
?>[/php]

This loop will print the value of "$x" while "$x" is less than 10. Then it will ad '1' to the value of "$x" and start over.

Code:
0
1
2
3
4
5
6
7
8
9





Now look at this loop:



[php] <?php
$text = "Learn PHP";

for ($var = strlen($text); $var > 1; $var = $var - 1) {
echo "The value of \$var is $var";
}
?>[/php]


First we made a string called "$text" and put the words "Learn PHP" in it. Remember that the first thing you have to do for a "FOR" loop is make a variable that will control the loop, so I made a variable called "$var" and set it equal to the number of characters in the string "$text" (which is nine). Then I said that while the value of "$var" is greater than "1" ($var > 1) subtract "1" from it ($var - 1). This code will output:

Code:
The value of $var is 9
The value of $var is 8
The value of $var is 7
The value of $var is 6
The value of $var is 5
The value of $var is 4
The value of $var is 3
The value of $var is 2


The code will run the last line (The value of $var is 2) and then subtract "1" from the value of "$var" - at which point the loop will start over again. However, this time the value of "$var" will now be "1" which is NOT greater than "1" so the loop will terminate.





While



A "while" loop is very simple.

[php] <?php
while ($n == TRUE) {
do this or that...
}
?>[/php]

In human language it can be read as

Code:
"while (the condition) is TRUE do this"


For example:


[php] <?php
$n = TRUE;
while ($n == TRUE) {
echo "$n is TRUE!";
}
?>[/php]
This loop will run forever because it will always equal TRUE. It will just keep printing out "TRUE is TRUE!"


You can change the condition to anything you want. Just look at these:

[php] <?php

$n = 12;
while ($n <= 13) {
echo "$n is less than or equal to 13!";
$n = $n + 1;
}

$n = 15;
while ($n > 13) {
echo "$n is greater than thirteen!";
$n = $n - 1;
}

$n = 1;
while ($n != 9) {
echo "$n is NOT equal to 9!";
$n++;
}

?>[/php]

if you ran these you would see the following:


Code:
12 is less than or equal to 13!
13 is less than or equal to 13!
15 is greater than thirteen!
14 is greater than thirteen!
1 is NOT equal to 9!
2 is NOT equal to 9!
3 is NOT equal to 9!
4 is NOT equal to 9!
5 is NOT equal to 9!
6 is NOT equal to 9!
7 is NOT equal to 9!
8 is NOT equal to 9!



The first loop will run twice before it equals FALSE. In human language it says:
Code:
make a vaiable named "n" and set it equal to 12
while "$n" is less than or equal to 13
print "(the value of $n) is less than or equal to 13!"
then add "1" to the value of "$n" and start the loop over.


The second while loop will also run twice before it equals FALSE. In human language it says:
Code:
make a vaiable named "n" and set it equal to 15
while "$n" is greater than 13
print "(the value of $n) is greater than thirteen!"
then subtract "1" from the value of "$n" and start the loop over.



The final while loop will run eight times before it equals FALSE. In human language it says:
Code:
Set the vaiable named "n" to a value of "1"
while "$n" is NOT greater than nine
print "(the value of $n) is NOT equal to 9!"
then add "1" to the value of "$n" and start the loop over.



Do / While


"Do while" loops are like while loops in every way but one - the condition isn't checked to see if it is TRUE until the END of the loop iteration. The following code will run one time before it is checked and found to be FALSE.

[php] <?php
$number = 8;
do {
echo "value of \$number is $number";
} while ($number > 10);
?>[/php]

this will print:

Code:
value of $number is 8





Lets make some advanced loops and put what we have learned to work.


For more practice with LOOPS you can visit these sites:
Melonfire
Tizag
pixelDepth switch


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Registered users: No registered users


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

Portal » Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
[
SEO MOD © 2007 StarTrekGuide ]