Decision Making
If..Then
The If..Then control structure allows computer programs to make decisions based on the data that is stored in the computer. A good example of getting the computer to make decisions is to find the maximum or minimum values of data that is entered. http://www.php.net/manual/en/language.control-structures.php#control-structures.if
The general form is:
If condition Then
… statements
EndIf
Condition controls the outcome and must be a Boolean expression. An example would be Age < 6.
Consider the following problem
A student has a Social Science project for which the maximum temperature for an 8 hour period of time must be found. The student has a thermometer and notes the temperature every hour. You are asked to write a program that will accept the temperatures noted over the day and then determine the maximum temperature.
<?php
//Determine maximum temperature written in PHP and designed for use at the command line
fwrite(STDOUT, "Maximum Temperature. \n");
$maximum = 0; // initialise the maximum temperature to a very low value
for ($hour=1; $hour <= 8; $hour++)
{
fwrite(STDOUT, "Enter the temperature for hour ".$hour." -> ");
fscanf(STDIN, "%d\n", $temperature);
if ($temperature > $maximum)
{
$maximum = $temperature;
}
fwrite(STDOUT, "The maximum temperature so far is ".$maximum."\n");
}
fwrite(STDOUT, "You have finished. \n");
exit(0);
?>
If ..Then ..Else
In the above example, the result of the decision made some action, or nothing to happen. The command can be used to make an alternate action happen as well, which is the norm. http://www.php.net/manual/en/control-structures.else.php
The general form is:
If condition Then
… statements
Else
... statements
EndIf
Condition controls the outcome and must be a Boolean expression. An example would be Age < 6.
The following example will demonstrate how this is done.
The Australian Taxation office allows some people to claim use of their private car. Cars which have engine capacities 1500 cc and over are allowed 65c per kilometer travelled, while those with engine capacities up to 1500cc are allowed 45c per kilometer because they are smaller. A program is needed to calculate the allowances.
<?php
//Calculating travel cost in PHP and designed for use at the command line
fwrite(STDOUT, "Tavel cost calculator. \n");
fwrite(STDOUT, "Enter the distance you have travelled -> ");
fscanf(STDIN, "%d\n", $distance);
fwrite(STDOUT, "Enter the engine capacity of your car -> ");
fscanf(STDIN, "%d\n", $capacity);
if ($capacity > 1500)
{
$allowance = $distance * 0.65;
}
else
{
$allowance = $distance * 0.45;
}
fwrite(STDOUT, "The allowance is $".$allowance."\n");
exit(0);
?>
Nested If..Then..Else
In a lot of situations, a number of comparisons based on the input must be made. The solution requires the if..then..else structure within other if..then..else statements. This is called nesting, and can become very complex very quickly. http://www.php.net/manual/en/control-structures.elseif.php
The following example will demonstrate how this is done.
Parcel-Post Airfreight charges for parcel delivery based on weight. The following is a table of their charges.
| Weight |
Price |
| < 2 kg |
$3.50 |
| 2-5kg |
$5.50 |
| >5kg |
$6.50 |
A program is required that will calculate the cost due when the weight is entered. It is necessary to check to see which category the weight falls in. If Weight is not less than 2, it must be checked again to see whether it is less than 5 or greater then 5. The charge, Charge, is calculated based on final rate.
<?php
//Calculating Air freight cost in PHP and designed for use at the command line
fwrite(STDOUT, "Air Freight cost calculator. \n");
fwrite(STDOUT, "Enter the weight of the parcel -> ");
fscanf(STDIN, "%d\n", $weight);
if ($weight < 2)
{
$cost = 3.5;
}
elseif ($weight > 5)
{
$cost = 6.5;
}
else
{
$cost = 5.5;
}
fwrite(STDOUT, "The cost to send the parcel is $".$cost."\n");
exit(0);
?>
Exercises
-
In the diving competition of World Diving Championship, 7 judges award a score to each competitor. So as the judging will be fair, a diver's final score is calculated by deducting the highest and lowest score after adding all of the scores together. You have been commissioned to create a program that could be used to calculate the score of a diver.
-
You have been contracted to write a Wages Calculator program by Freddies Fast Food. Juniors under 18 get $15.60 an hour and the rest get $17.80 an hour. Write the program to calcule the wage of an employee based on the age and number of hours worked in a week.
-
A CD manufacturing company charges Multimedia companies $6.50 per CD if they create less than 1000 copies. If the company requires more than 10000, they only pay $5.00 a copy. Otherwise, they pay $5.70 a copy. Write a program that will allow the company to calculate how much to charge for different jobs they do.
Comments (0)
You don't have permission to comment on this page.