Simple While Loop Example

Repetive tasks are always a burden to us. Deleting spam email, sealing 50 envelopes, and going to work are all examples of tasks that are repeated. The nice thing about programming is that you can avoid such repitive tasks with a little bit of extra thinking. Most often, these repetitive tasks are conquered in the loop .

The idea of a loop is to do something over and over again until the task has been completed. Before we show a real example of when you might need one, let's go over the structure of the PHP while loop.

Simple While Loop Example

The function of the while loop is to do a task over and over, as long as a specified conditional statement is true . This logical check is the same as the one that appears in PHP if statements to determine if it is true or false . Here is the basic structure of a PHP while loop:

Php code:

while ( conditional statement is true){ 
  do this code; 
} 
   

This isn't valid PHP code, but it displays how the while loop is structured. Here is the break down of how a while loop functions when your script is executing:

  1. The conditional statement is checked. If it is true, then (2) occurs. If it is false, then (4) occurs.
  2. The code within the while loop is then executed.
  3. After the code within the while loop has executed, the process starts again at (1). Effectively "looping" back.
  4. If the conditional statement is false, then the code within is not executed and there is no more looping. Tye code following the while loop is then executed like normal.

A Real While Loop Example

magine that you are running an art supply store. You would like to print out the price chart for brushes given a certain quantity. You sell brushes at a flat rate, but would like to display how much different quantities would cost. This will save your customers from having to do the mental math themselves.

You know that a while loop would be perfect for this reptitive and boring task. Here is how to go about doing it.

PHP Code:

<?php
   $brush_price = 5; 
   $counter = 10;   
echo "<table border=\"1\" align=\"center\">"; 
     echo "<tr><th>Quantity</th>"; 
     echo "<th>Price</th></tr>"; 
     while ( $counter <= 100 ) { 
     echo "<tr><td>"; 
     echo $counter; 
     echo "</td><td>"; 
     echo $brush_price * $counter; 
     echo "</td></tr>"; 
     $counter = $counter + 10; 
     } 
     echo "</table>"; 
     ?>

display

Quantity Price
10 50
20 100
30 150
40 200
50 250
60 300
70 350
80 400
90 450
100 500

Pretty neat, huh?! The loop created a new table row and its respective entries for each quantity, until our counter variable grew past the size of 100. When it grew past 100 our conditional statement failed and the loop stopped being used. Let's review what is going on.

  1. We first made a $brush_price and $counter variable and set them equal to our desired values.
  2. The table was set up with the beginning table tag and the table headers.
  3. The while loop conditional statement was checked, and $counter (10) was indeed smaller or equal to 100.
  4. The code inside the while loop was executed, creating a new table row for the price of 10 brushes.
  5. We then added 10 to $counter to bring the value to 20.
  6. The loop started over again at step 3, until $counter grew larger than 100.
  7. After the loop had completed, we ended the table.

You may have noticed that we placed slashes infront the quotations in the first echo statement. You have to place slashes before quotations if you do not want the quotation to act as the end of the echo statement. This is called escaping a character and we will talk about it more in later lessons.

With proper use of loops you can complete large tasks with great ease.

** Bebas disunting dengan menyebutkan sumber **

Halaman Terkait

  • HTML5 allows for MathML elements
    The HTML syntax of HTML5 allows for MathML elements to be used inside a document using ... tags. Most of the web browsers can display MathML tags. If your browser does not support MathML, then I would suggest you to use latest version o
  • HTML5 Deprecated Tags and Attributes
    The following elements are not available in HTML5 anymore and their function is better handled by CSS:
  • Complete HTML5 Tags Reference
    A complete list of standard tags available in HTML5 is given below. All the tags are ordered alphabetically along with an indication if they have been introduced newly or they have been deprecated in HTML5.
  • HTML5 - Color Names
    The following table shows 16 color names that were introduced in HTML 3.2 to support 16 colors that 8-bit graphics cards offered:
  • HTML5 - SVG Tutorial
    SVG stands for Scalable Vector Graphics and it is a language for describing 2D-graphics and graphical applications in XML and the XML is then rendered by an SVG viewer.

Kiriman terbaru