Repetition Lab

The objective for today's lab is to investigate the sentinel controlled while loop and the for loop.

The first part of this lab involves creating a sentinel controlled while loop. The code is supposed to allow you to enter numbers until a 0 is entered. For each number entered, processing simply consists of an output of the number. But, the sentinel is never to be processed.

  1. Create a New Project
  2. Run this code
    The loop is never entered. So, remove the = 0 initialization
  3. Now try this code
    Now the sentinel is processed. So, lets put the input back prior to the loop.
  4. Now VERY CAREFULLY try this code with an infinite loop
  5. Now try this correct code
    Notice that a correct sentinel controlled loop follow this outline:

    input first value
    Use WHILE to check value against sentinel value
       Input the next value
       Processing in the loop
  1. Average sample one code
  2. Average sample two code
  3. Average correct code

The second part of this lab is to explore the for loop.

  1. Create a New Project
  2. Run this code
    Modify the code to count from 6 to 52
  3. Run this code
    Compare the while loop to the for loop
  4. Modify the code so that the user gets to input the first value code
  5. Modify the code so that the user gets to input the increment code
  6. What if the user wants to count backwards? code
    This doesn't work....
  7. We don't need to subtract a negative. Just add the negative step value. code
  8. This isn't correct either. Should the test still be <=? code