Privacy Policy

Community
Standards


Youth Tech's Online PHP Class: Lesson Five

Loops [Chat: 8/16/02]

Hosted by YTCC Spark

Welcome back to PHP Class!  If you have any questions, as always, don't hesitate to email me.  Or, the preferred method, bring your questions to Chat, 10PM EST, every Friday!

Loops

Sometimes the same operations must be performed on several sets of data.  For example, let's say you wanted to know the value of 1 through 5 squared.  You could do:

print 1*1;
print 2*2;
print 3*3;
print 4*4;
print 5*5;

and that would work, but it becomes inconvenient if you wanted to do, say, one through 100.  In cases like that, one can use a loop to solve the problem.

PHP supports two basic types of loop: the FOR loop and the WHILE loop.  Each will be covered here.

While Loops

While loops specify a condition under which certain actions should be performed, and then perform it while that condition is true.  A while loop uses a boolean expression (see Lesson Four for a refresher on true/false evaluation) to determine if the loop should be run.  

The basic syntax of a WHILE loop is the following:

while(BOOLEAN_EXPRESSION){
    STATEMENTS TO BE EXECUTED IF BOOLEAN_EXPRESSION IS TRUE
}

For example, the following loop recreates the function of squares of the numbers one through five:

$i=1;
while($i <= 5){
    print $i * $i;
    $i ++;
}

In this case, the loop did not save you any coding length (it's still 5 lines), but the loop could be easily modified to go up to 100:

$i=1;
while($i <= 100){
    print $i * $i;
    $i ++;
}

Now, instead of requiring 100 lines of code, it requires four.  Clearly, this makes the program more manageable and easier to code in the first place.

The Boolean expression is evaluated prior to each execution of the statements.

For Loops

For loops simplify some of the complexities of a while loop.  They allow you to specify a starting point, an ending condition, and a statement to be executed after each iteration (execution of the loop).  The basic syntax for a FOR loop is:

for(START;BOOLEAN;ITERATION){
    STATEMENTS TO BE EXECUTED
}

The basic steps involved when a FOR loop executes are the following:

  1. Execute START statement.
  2. Evaluate BOOLEAN expression.  If true, continue to step 3.  Else, abort execution and go to first statement after loop.
  3. Execute the STATEMENTS TO BE EXECUTED.
  4. Execute ITERATION statement.
  5. Return to step 2.

For example, squaring the numbers one through five could be done like this:

for($i=1;$i<=5;$i++){
    print $i*$i;
}

And for one through one hundred:

for($i=1;$i<=100;$i++){
    print $i*$i;
}

This has reduced 100 lines of code to 3.  Quite a bit more efficient.  And what if you were to decide you wanted to cube them instead?  Without loops, you'd need to change 100 lines.  With them, you must change one:

for($i=1;$i<=100;$i++){
    print $i*$i*$i;
}

A Variation on While Loops

Sometimes you want to ensure that the code inside a loop is executed at least once, regardless of the boolean expression.  One way to do this is to use a do ... while loop.  The basic syntax for this follows:

do {
    STATEMENTS TO BE EXECUTED
} while (BOOLEAN_EXPRESSION);

Note: A semicolon must follow the boolean expression.

This is frequently used when the value for the boolean expression will come from the statements included in the loop.

Coding Challenge One!

At this point, you have made it far enough to do some stuff with PHP.  So I present to you a challenge.  Have the following pattern printed out with PHP:

XOOOOX
OXOOXO
OOXXOO
OOXXOO
OXOOXO
XOOOOX

Hint: more than one loop might be useful.

Send your code to ytccspark@youthtech.com and I'll post the names of those whose code works at the beginning of the next lesson.  Maybe I'll even have a surprise for the person whose code is the most efficient (fewest statements).  If I can think of one!

That's all for now!

--Spark

Back to the Index

 

   

Copyright © 1996-2003, Youth Tech
Questions? Comments?