Northcentral Technical College Introduction To Programming
10-152-310
Introduction to Programming with JavaScript NTC 10-152-310

Learning Plan 5 A while loop with unknown loop count

# A while loop with unknown loop count

#### File: /Learning Plan 5/whileUnknownCount.html

### Unknown Loops with `while`

> We don't know when we'll stops, it just depends.

- When we code a loop that does not have a preset number of loops we need a way to stop the loop when it is done. There are two common approaches:
  1. Loop until some predetermined value is read, like "quit".
  2. Loop and read data until the end of the data.

### Stop On A Sentinel

sen•ti•nel

noun

1. a soldier or guard whose job is to stand and keep watch.
• figurative something that appears to be standing guard or keeping watch.

- The term sentinel in programming means a special value that a program is looking for to take certain actions. Here are some examples.
  - A program that asks a user for words until the entered word is, "quit".
  - A program that reads records from a file until it reaches a customer number that is 999999.
  - A program that performs security scans on a hard drive every half hour until midnight, it then outputs a report for the previous day and starts over.
- Here's an example that I will uncomment to make work:

### A Sentinel in the data

- Sometimes a sentinel is just part of the data as in this example (Hang on, I have to uncomment it.)

### Comparison to Stop a Loop

- Frequently, we need to compare two values to determine if we stop a loop

- It works with characters, too.

### The End is Near

> Read until we're done.

- Our programs read data from sources like files and database tables.
- We usually need to read until we've read everything.
- Sentinels are not used as much as they used to be. Modern languages are better equipped to loop until we're done.
- Here's a detailed example using the Student Exam Records set. Here is the entire data set:

"Bill Smith" | 78
"Sue Johnson" | 89
"Jill Green" | 99
"Steve Brown" | 83
"Nancy Brown" | 95
"Tom Little" | 90

- In our Record Set system for the course we know that in order to read these records we first need to do this:
- This function gives us access to the records set but not to the first record. You could think of it as pointing to the spot before the first record. Like this:

|  | Current record
"Bill Smith" | 78 |
"Sue Johnson" | 89
"Jill Green" | 99
"Steve Brown" | 83
"Nancy Brown" | 95
"Tom Little" | 90

- Next we call this function at the top of a `while` loop, which will cause the first record to be available.
  - `records.readNextRecord();`

"Bill Smith" | 78 | Current record
"Sue Johnson" | 89
"Jill Green" | 99
"Steve Brown" | 83
"Nancy Brown" | 95
"Tom Little" | 90

- Then within the loop we use functions that start with "get" to read each field of the record. For this record set we use:
  - `records.getStudentName();`
  - `records.getStudentScore();`
- Because the current record is the first one, these functions return:
  - Student Name: `Bill Smith`
  - Student Score: `78`
- The next step is to move to the next record. The `while` loop starts again with this function.
  - `records.readNextRecord();`
- If there is another record this function moves our current record to the next one.

"Bill Smith" | 78
"Sue Johnson" | 89 | Current record
"Jill Green" | 99
"Steve Brown" | 83
"Nancy Brown" | 95
"Tom Little" | 90

- Then "get" functions will now retrieve:
  - Student Name: `Sue Johnson`
  - Student Score: `89`
- Then the loop starts over with `records.readNextRecord();` and moves our current record forward.

"Bill Smith" | 78
"Sue Johnson" | 89
"Jill Green" | 99 | Current record
"Steve Brown" | 83
"Nancy Brown" | 95
"Tom Little" | 90

- We continue to move the current record and read the data from the current record until we get here:

"Bill Smith" | 78
"Sue Johnson" | 89
"Jill Green" | 99
"Steve Brown" | 83
"Nancy Brown" | 95
"Tom Little" | 90 | Current record

- Now with the next call to `records.readNextRecord();` something different happens. When there are no more records to read this function will return `false` and the loop will stop.

"Bill Smith" | 78
"Sue Johnson" | 89
"Jill Green" | 99
"Steve Brown" | 83
"Nancy Brown" | 95
"Tom Little" | 90 |
`return false;` |  | Current record

### Quick Example

- Here's what this looks like in a very small program. All the looping elements are here.

### Full Example

- Here's a full example: Print Examination Scores

### The End is Labs, of course

> Everything should be made as simple as possible, but not simpler. - Albert Einstein

- 5.2 Lab 1
  - 5.2 Assignment 1