Server-Side Scripting/Iteration

This lesson introduces condition and loop control structures.

Objectives and Skills


Objectives and skills for this lesson include:

  • Understand structured programming concepts
  • Understand control flow
  • Understand exception handling
  • Use conditions and loops in server-side scripts


  1. Wikipedia: Structured programming
  2. Wikipedia: Conditional (computer programming)
  3. Wikipedia: Control flow
  4. Wikipedia: Exception handling


  1. YouTube: Loop in Go | Golang
  2. YouTube: JavaScript Tutorial for Beginners - 11 - Loops
  3. YouTube: PHP Loops Tutorial
  4. YouTube: Conditionals and Loops in Python





Complete the following activities using HTML, CSS, and a server-side scripting language. Apply best practices for user interface design and your selected scripting language, including modules, comments, indentations, naming conventions, and constants. Use HTML forms and input elements for input, server-side scripts for processing, and HTML elements for output. Use separate functions for each type of processing. Avoid global variables by passing parameters and returning results. Create test data to validate the accuracy of each program. Add comments at the top of the code modules and include references to any resources used. Add the completed code to your website as /lesson4.

Loops (Iteration)

  1. Review Math Games: Add Two Numbers Up to 5. Create a math expression game. Allow the user to enter a value they would like to see math expressions for and the number of expressions they want. Display the expressions one at a time, using their value for the first operand, a random number between 0 and their value for the second operand, and an input element for the sum. For example, if the user enters 3, your web page might display:
        3 + 2 = ⃞
    When the user submits their answer, either display Correct or display Incorrect with the correct answer, and include the next expression for them to fill in. Include elements to display the number of correct answers and the total number of expressions answered. Continue processing until the requested number of expressions have been displayed. You will need hidden input elements to keep track of game progress.
  2. Review MathsIsFun: Definition of Average. Create an average calculator. Allow the user to enter values one at a time. Each time they submit their value, add it to a running total and display the average. You will need hidden input elements to keep track of the total and number of values entered. Continue processing values until the user enters a blank value (no value). When they submit a blank value, reset the sum and count to zero.

Nested Loops

  1. Review Wikipedia: Body mass index and MathsIsFun: Metric - US/Imperial Conversion Charts. Create a BMI calculator. Allow the user to enter their height and weight in either US or metric units. If the user selects US units, display a BMI table with columns for height from 58 inches to 76 inches in 2-inch increments and rows for weight from 100 pounds to 250 pounds in 10-pound increments. If the user selects metric units, display a BMI table with columns for height from 150 centimeters to 190 centimeters in 5-centimeter increments and rows for weight from 45 kilograms to 115 kilograms in 5-kilogram increments. Highlight the user's place on the table with your choice of CSS formatting (background color, border, color, font style, font weight, etc.).
  2. Review MathsIsFun: 10x Printable Multiplication Table. Create a program that uses nested loops to generate math tables. Rather than simply creating a 10 by 10 multiplication table, allow the user to enter the starting and ending values and allow them to choose addition, subtraction, multiplication, or division. Include row and column labels. For example, the output for addition from 1 to 3 might look like:
        +   1   2   3
        1   2   3   4
        2   3   4   5
        3   4   5   6
    The output for multiplication from 3 to 5 might look like:
        *   3   4   5
        3   9  12  15
        4  12  16  20
        5  15  20  25

Lesson Summary


Additional items may be contributed by course participants

  • Structured programming techniques include the use of selection statements, iteration techniques, and the use of subroutines or functions to prevent the repetition of code. By properly implementing these techniques, code is easier to read, understand, and maintain.[1]
  • Loops offer a quick and easy way to do something repeatedly.[2]
  • Early exit from a function or loop is the most common deviation from the structured programming requirement of single exit points. Most modern languages support the early exit of a function with a return statement or exit from a loop with a break statement and provide the necessary clean up from exiting out of the routine or loop early.[3]
  • "break" is the statement used in most programming languages to trigger an early exit.[4]
  • A common reason for loops becoming infinite is the using a variable in a condition statement, then never updating that variable in the loop.[5]
  • Repeating a block of code until a criteria is met is known as iterating. This method of programming is also known as looping, common loops are:
    • While Loop - A while loop will check a condition than run through an iteration of code. This will continue until the condition has been met.[6]
    • Do While Loop -A Do While loop will run through an iteration of code before it checks the condition. This will continue until the condition has been met.[7]
    • For Loop - a For Loop iterates through a block of code a certain number of times before it breaks.[8]

Key Terms


Additional items may be contributed by course participants

breaking the loop
Normally, a loop exits when its condition becomes false, but we can force the exit at any time using the special break directive.[9]
conditional statement
Used to perform different actions based on conditions.[10]
control flow
The order in which individual statements, instructions or function calls of an imperative program are executed or evaluated.[11]
exception handling
When an exception occurs, the normal flow of the program is disrupted and the program terminates abnormally, therefore these exceptions are to be handled.[12]
The increment operator (++) increments (adds one to) its operand and returns a value.[13]
infinite loop
A loop that theoretically runs forever.[14]
Programming structure that repeats a sequence of instructions until a specific condition is met.[15]

See Also