# JavaScript Programming/While Loops

This lesson introduces JavaScript while and do while loops.

## Objectives and Skills

Objectives and skills for this lesson include:

• Complete and debug loops
• for; while; do; break; continue

## Activities

Complete the following activities using external JavaScript code. Apply JavaScript best practices, including comments, indentations, naming conventions, and constants. Use input elements for input and respond to input or change events or add buttons and respond to click events. Use 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.

### While Loops

Complete the following using a while loop structure.

1. Create a program that uses a loop to generate a list of multiplication expressions for a given value. Ask the user to enter the value and the number of expressions to be displayed. For example, a list of three expressions for the value 1 would be:
`1 * 1 = 1`
`1 * 2 = 2`
`1 * 3 = 3`
A list of five expressions for the value 3 would be:
`3 * 1 = 3`
`3 * 2 = 6`
`3 * 3 = 9`
`3 * 4 = 12`
`3 * 5 = 15`
2. Review MathsIsFun: Pi. Write a program that uses the Nilakantha series to calculate Pi based on a given number of iterations entered by the user.
3. Review MathsIsFun: Definition of Average. Create a program that asks the user to enter grade scores. Start by asking the user how many scores they would like to enter. Then use a loop to request each score using a prompt and add it to a total. Finally, calculate and display the average for the entered scores.
4. Review MathsIsFun: Definition of Average. Create a program that asks the user to enter grade scores. Start by asking the user how many scores they would like to enter. Then request each score using an input element and add it to a total. Finally, calculate and display the average for the entered scores. In this approach, the loop action is performed by the user rather than by the code.

### Do While Loops

Complete the following using a do while loop structure.

1. Review MathsIsFun: Definition of Average. Create a program that asks the user to enter grade scores. Use a loop to request each score using a prompt and add it to a total. Continue accepting scores until the user enters no value (empty input). Finally, calculate and display the average for the entered scores.
2. Review Khan Academy: A guessing game. Write a program that allows the user to think of a number between 0 and 100, inclusive. Then have the program try to guess their number. Start at the midpoint (50) and ask the user if their number is (h)igher, (l)ower, or (e)qual to the guess using a prompt. If they indicate lower, guess the new midpoint (25). If they indicate higher, guess the new midpoint (75). Continue efficiently guessing higher or lower until they indicate equal, then print the number of guesses required to guess their number and end the program.
3. Review MathsIsFun: Definition of Average. Create a program that asks the user to enter grade scores. Request each score using an input element and add it to a total. Continue accepting scores until the user enters no value (empty input). Finally, calculate and display the average for the entered scores. In this approach, the loop action is performed by the user rather than by the code.
4. Review Khan Academy: A guessing game. Write a program that allows the user to think of a number between 0 and 100, inclusive. Then have the program try to guess their number. Start at the midpoint (50) and ask the user if their number is (h)igher, (l)ower, or (e)qual to the guess using three buttons. If they indicate lower, guess the new midpoint (25). If they indicate higher, guess the new midpoint (75). Continue efficiently guessing higher or lower until they indicate equal, then print the number of guesses required to guess their number and end the program. In this approach, the loop action is performed by the user rather than by the code.

## Lesson Summary

• Loops are used to avoid code repetition. 
• Loops are useful when aiming for DRY (Don't repeat yourself) solutions vs. WET (Write every time) solutions when programming. 
• Four components of any loop are the initialization of a loop control variable, terminating condition, update step (increment or decrement), and a loop body.  
• Loop may run n number of times or may not run at all, depending on the condition.
• We get an infinite loop when we forget to include terminating condition, when the condition can never be met or if it makes the loop to start over and over again. 
• A nested loop, or a loop inside of another loop, can be used to better structure code and make it maintainable.[source?]

## Key Terms

do while loop
This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
increment
The increment operator (++) increments (adds one to) its operand and returns a value.
infinite loop
A loop that theoretically runs forever.
iteration
It's a repetition of code or cycle, performed by a loop
While loop
The while loop executes a block of code repeatedly as long as a specified condition is true.