Section 9d

Pre-Test vs Post-Test Loops


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Different Starting Conditions

Pre Test vs Post Test

The kind of loop discussed up to this point might be called a pre-test loop because it tests for loop continuation before conducting any actions in the loop code block. This is very helpful for most iterating circumstances. However, you can see that the loop will only continue if the most recently input character is not a period. If it is not a period, then the loop action will increment the character counter by one, and the next action will be to fetch another character. This is pretty straightforward . . . but it has a bug.

Once the loop is started, then the action inside the loop curly braces will bring in new characters. The problem is that the first time the loop is run, it will not have a valid character to test, unless it has been given one before the start of the loop. To make this loop work correctly, you will need to capture the first character so that it can be tested when the loop first starts. Preparing a loop by getting data ready for testing in advance is called priming the loop.

Here is how it could be done.

// fetch or acquire the first character in the sentence

// loop while newest character found is not a period
{
// count the character (increment a character counter)

// go fetch the next character
}

You will see, and use, this priming action quite regularly once you get started with loops.

As you recall, the pre-test loop is much like the if test except that by definition, the pre-test looping action supports code repetition and the if statement only implements an action once. While priming works fine to get this kind of job done, there is another kind of loop that might simplify the process a little more.

A second kind of loop might be called a post-test loop since it allows the code block to run one time before any loop control testing is conducted. The kind of loop you have been studying so far would not run at all if the starting conditions were not met. This new post-test kind of loop will always run at least one time. That is because its decision is not made until the end of the loop. It looks like the following in pseudo code.

start the loop here (without a test)
{
conduct the repeated actions

in this code block
}
while the conditions for continuing are true,
go back and repeat this loop

Since the "start the loop here (without a test)" text is a little verbose (i.e., it states more than is needed), the following text will be used to simplify the actions.

start loop here
{
conduct the repeated actions

in this code block
}
while the conditions for continuing are true,
go back and repeat this loop

You have probably noticed by now that you have not actually seen the C code yet since all the information thus far has been presented in pseudo code. You will see the code soon, but you will not be surprised by it. It is going to look a lot like the pseudo code you have been using.

Consider one more example of this post-test kind of post-test loop before you go on. Think about what happens if you are presenting a menu of choices to a user. It is always a given that the menu will be displayed at least once. However, if the user picks the wrong choice, an error message may be displayed, and then the menu must be displayed again. This is a classic example of how to use this kind of loop. Take a look at the pseudo code for this kind of condition.

// start loop here
{
// display menu choice 1

// display menu choice 2

.

.

// display last menu choice (N)

// prompt for user input

// accept user input

// check for incorrect user selection
{
// display an error message

// tell user to try again

// pause the program so that user can read this message
}
}
// end loop, continue if correct answer has not been input

This operation makes use of the fact that the post-test loop will go through one iteration before it makes a decision. By allowing that, the loop does not have to be primed with an extra input prior to the loop beginning. The pseudo code can be tightened up a little more, but you need to look at a new quantity that was introduced in this pseudo code. This is the Computer Science concept and implementation of N. You have already seen this used previously, and you will learn more about this in the next topic as the conceptual basis for loops is wrapped up.