Iteration
Most programs need some process to be repeated many times. A process usually needs many lines of code. To avoid having to write that part of the code many times, the Repeat Structure is used. This comes in three basic forms
- Test last loop - unknown number of loops
- Test first loop - unknown number of loops
- Fixed number of loops
Common boolean operators for PHP
Test First Loop
Getting a part of a program to repeat using Do..Loop or Test First Loop structure. http://www.php.net/manual/en/control-structures.while.php
The Test First Loop gives the option to enter into a code segment, and continue using this code until a condition is met. The code may never be activated if the appropriate condition is met.
The general form is:
Do While | Until condition
… statements
The loop condition controls the outcome and must be a Boolean expression. An example would be Age < 6 where Age is a variable, < is a boolean operator and 6 is a test value.
Example - simple guessing game
<?php
$secret = 12;
fwrite(STDOUT, "Guess the secret number <0..100> -> ");
fscanf(STDIN, "%d\n", $guess);
// Loop until the number is guessed
while ($guess != $secret)
{
fwrite(STDOUT, "Guess the secret number <0..100> -> ");
fscanf(STDIN, "%d\n", $guess);
}
fwrite(STDOUT, "You guessed it. \n");
exit(0);
?>
Test Last Loop
In this case, the loop structure is entered before testing of a condition is done. In other words, the code must be executed at least once. It is important to ensure that the loop can be exited!. http://www.php.net/manual/en/control-structures.do.while.php
The general form is:
Do
…statements
Loop While | Until condition
Example - number adder (keeps adding entered numbers until a specific total is exceeded)
<?php
//Number adding program written in PHP and designed for use at the command line
//loop stops when 100 is exceeded
fwrite(STDOUT, "Accumulator program. \n");
$total = 0;
do
{
fwrite(STDOUT, "Enter a number between 1 and 10 -> ");
fscanf(STDIN, "%d\n", $number);
$total = $total + $number;
fwrite(STDOUT, "Total so far is ".$total."\n");
} while ($total <= 100);
fwrite(STDOUT, "You have finished. \n");
exit(0);
?>
Fixed Loop Structure - For .. Next Structure
Sometimes it is necessary to repeat a section of code a set amount of times. The For..Next loop structure is used to handle these problems. It has a "built in" counter that is automatically increased as the code is repeated. http://www.php.net/manual/en/control-structures.for.php
Example - add up scores of members of a cricket team (11 players)
<?php
//Adding up scores written in PHP and designed for use at the command line
//adds up 11 scores
fwrite(STDOUT, "Cricket Scores. \n");
$total = 0;
for ($player=1; $player <= 11; $player++)
{
fwrite(STDOUT, "Enter a score for player ".$player." -> ");
fscanf(STDIN, "%d\n", $score);
$total = $total + $score;
fwrite(STDOUT, "Total so far is ".$total."\n");
}
fwrite(STDOUT, "You have finished. \n");
exit(0);
?>
Exercises
-
Don Ladly, coach for the Bilby footy team, needs a program that will add up the goals kicked by a given player over the season. Write a program that will accept the player's name and the goals kicked each game (if any) and display a message such as "Bill Jones kicked 65 goals this season".
-
Bunnys hardware needs a program that will allow stock control of a paint sale where 80 cans of paint are available. Write a program that will allow the check-out operator to enter the number of cans purchased and advise when no more paint is left.
Comments (0)
You don't have permission to comment on this page.