Server-Side Scripting/Conditions and Loops

From Wikiversity
Jump to navigation Jump to search

This lesson introduces condition and loop control structures.

Objectives and Skills[edit | edit source]

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

Readings[edit | edit source]

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

Multimedia[edit | edit source]

  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

Examples[edit | edit source]

Activities[edit | edit source]

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 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 program and include references to any resources used.

Loops[edit | edit source]

  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.
  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 a hidden count element to keep track of the 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 back to zero.

Nested Loops[edit | edit source]

  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[edit | edit source]

Additional items will be contributed by course participants

Key Terms[edit | edit source]

Additional items will be contributed by course participants

See Also[edit | edit source]

References[edit | edit source]