Looping Structures
What is a loop?
|
|
Loops help us do things over and over and over.
This loop will “Do Something” forever. This is called an infinite loop. It will crash your program. How can we stop it? We need a conditional statement / Boolean expression to get it to stop. The first type of loop is a pre-test loop. In this case, the condition is tested before the code within the loop block is run for the first time. |
For example:
Create a flowchart for a program that asks the user for a rating between 1 and 5, then print the number of stars representing the rating.
Create a flowchart for a program that asks the user for a rating between 1 and 5, then print the number of stars representing the rating.
Sentinel Values
Accumulators
- A Sentinel Value is a special value used to terminate a loop.
- You typically choose a sentinel value as something that is not legitimate data value while the loop is iterating.
- In the loop that asks the user for a correct password, the sentinel value would be the value of the correct password This is the value which causes the loop to terminate.
Accumulators
- An Accumulator is a variable that accumulates the values of data in a program. Accumulators are typically used for counting and totaling.
- Accumulators can be incremented with any of the following statements: