Chapter 7 Margin Notes

General Note: In this chapter, we learn how to write programs that perform the same operation over and over and over and over and over and over and over and over and over and over and over again.

Page 264. Iteration is a fancy word for doing something repeatedly.

Page 264, "of course". I hate books that use the term "of course". Nothing is obvious.

Page 264, looping. A loop is code that repeats over and over again (see Iteration above), and at some point (when a test condition is encountered) it is designed to stop or be exited from. 

Page 264, Endless Loop. If a loop continues to run indefinitely, it is called an endless loop and usually results in the program being 'non responsive'.

Page 264, Types of loops. There are 3 types of loops---Pretest loops, Posttest loops, and Fixed Repetition loops. Pretest loops test for an exit condition before the body of the loop is executed. Posttest loops test for an exit condition after the body of the loop is executed. Fixed Repetition loops execute the body of the loop a fixed number of times. In C++, these three types of loops are implemented via the while, do/while and for loops.

Page 264, The While Loop. A Boolean test is an expression whose result evaluates to a true or a false value.

Page 265. To exit the loop, something must change within the body of the loop to impact the Boolean result of the test expression--if this doesn't happen, you have an Endless Loop.

Page 265, Debugging Tip. Always frame the body of the loop in curly braces---even if there's just one statement. Anyone want to take a shot at the answer to the Debugging Tip?

Page 266, Debugging Tip. I'm not sure what the author is getting at here.

Page 266, Example 7.1. Make sure you do all of these!

Page 268, Debugging Tip. Check this out---Infinite loops won't generate a compiler error.

Page 269, Flag-Controlled Loop. Don't get thrown by the term---a flag-controlled loop is just a loop where the user is given a chance to cause the loop to terminate by entering a value (usually something not reasonable, like -1, or the word 'Elvis') which when detected by the program, sets the 'flag' variable to false, resulting in a false Boolean test expression.

Page 269, Data Entry Using while. Notice that the code reads one character 'outside' the loop, in addition to reading the character inside the loop. This isn't the only way to code this, but it's probably the cleanest, in that the period isn't counted in the character code. This code would also work, but would result in the period being counted.

int main() {
  const char PERIOD = '.';
  char inChar = ' ';
  int charCount = 0;

  cout << "Enter several characters, pressing ENTER after each\n"
    << "entry. Terminate the input with a period. " << endl;

  while (inChar != PERIOD)
    cin >> inChar;

  cout << "The number of characters entered was " << charCount << endl;


Page 270, Sentinel value. A sentinel value is much like a flag--the difference is that it is used directly in the test expression.

Page 271, The do/while Loop. The difference between the While Loop and the do/while Loop is the location of the test expression. With a While Loop, the test expression is evaluated at the 'top' of the loop, before the body of the loop. With a do/while Loop, the test expression is evaluated at the 'bottom' of the loop, after the body of the loop has been executed. What this means is that with a do/while Loop, the body of the loop is guaranteed to be executed at least once!

Page 275, Loop-Controlled Menu Driven Programs. A key usage for loops--the ability of a program to perform its functions multiple times.

Page 281, Debugging Tip. The author doesn't show you the way it should be coded--like this...

  while (choice !=  'q' &ap;