Lab 1.8 - Code

-Handle loops

Chapter 1 Main




<?php require_once "../mainfunctions.php" ?>
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>Lab 1.8</title>
<meta name="viewport" content="width=device-width; initial-scale=1.0">
<link rel="stylesheet" href="../chapterstyles.css">
</head>

<body>

<div>
<header>
    <h1>Lab 1.8 </h1>
    <h3>Handle loops</h3>
</header>
</div>

<div id="chapterMain">
<a href="chapter1main.php">Chapter 1 Main</a>
</div>


<?php

//create variable
$price = 100;

//1. FOR LOOP------------------------------
//print title
echo "<p>1. For Loop: Discount price by Week | Displaying 8 Weeks with 10% discount each week:";
echo "<br>'price' starts as $price";
echo "<br>'discountedPrice' starts as uninitialized";
echo "<br>'x' is initialized as 0 in loop statement";
echo "<br>";

for ($x=0;$x<8;$x++){
    $discountedPrice = $price - ($price * $x *.10);
    echo "<br> Week " . ($x+1) . ": \$" .round($discountedPrice, 2);
}

echo "</p><br>";


//2. WHILE LOOP------------------------------
//print title
echo "<p>2. While Loop: Discount price by Week with $20 Minimum Price";
echo "<br>'price' starts as $price";
echo "<br>'discountedPrice' starts as $discountedPrice but is manually reset to 100";
echo "<br>'x' starts as $x but is manually reset to 0";
echo "<br>";
//initialize $x to 0 before loop.
$x = 0;
//set %discPrice to $price the first time, so the loop will run
$discountedPrice = $price;

while ($discountedPrice > 30){
    $discountedPrice = $price - ($price * $x *.10);
    echo "<br>Week " . ($x+1) . ": \$" .round($discountedPrice, 2);
    $x=$x+1;
}

echo "</p><br>";


//3. DO WHILE LOOP------------------------------
//print title
echo "<p>3. Do While Loop: Quantity Discount:";
echo "<br>'price' starts as $price";
echo "<br>'discountedPrice' starts as $discountedPrice but is reset to 100";
echo "<br>'x' starts as $x but is manually reset to 0";
echo "<br>";
//initialize $x to 0 before loop.
$x = 0;
do{
    $discountedPrice = $price - ($price * $x * .01);  
    echo "<br>Minimum quantity: " . $x . ": \$" .round($discountedPrice, 2);
    //increment counter
    $x=$x+10;
}
while($x <= 70);


echo "</p>";

?>
</div>





<?php 
printFootWithCodeExample("1.8code.php");
?>
</body>
</html>





BACK