Starting Out with Programming Logic and Design (2nd Edition) |
1. Introduction to Repetition Structures
A repetition structure causes a statement or set of statements to execute repeatedly. This is done so the same code does not have to be typed over and over. We will look at two types: condition controlled loops (using true false tests) and count controlled loops (using a number to count enough times).
2. Condition Controlled Loops: while, do-while, and do-until
The while loop
Gets its name from the way it works: while a condition is true' do some tasks. It consists of a true-false test and a set of instructions to do if it is true. To show the condition in a flow chart a diamond is used with a line to the right to show what to do if true and a line going down to drop out if false.
Writing a while loop in Pseudocode
While condition
statement
statement
etc.
End while
The while and end while should line up while the rest are indented. The condition is a Boolean expression that will do the statements if true. In order for this to work, something in the conditions must change the variable tested for the Boolean test for the program to drop through.
The while is a Pretest Loop
A pretest loop tests the condition before doing the statements.
Infinite Loops
If a loop has no way to end it is called an infinite loop. This will happen in the while loop if there is no way to change the condition.
Modularizing the Code in the Body of a Loop
Using a module in the loop is as simple as calling it and helps in keeping the code clean.
The do-while Loop
The do-while is a post-test loop meaning it looks for changes after the loop is done. It will always run at least once. In a flowchart the diamond is after a group of statements with a true arrow out of the right back to the start of the statements and a false arrow dropping down.
Writing a do-while Loop in Pseudocode
Do
statement
statement
etc.
While condition
Lineup is basically the same as the while loop but test is at the end.
A do-while loop is convenient but any loop written as a do-while can also be written as a while loop.
The do-until Loop
Rather than doing a loop till a condition is false, a do-until continues while it false and stops when it turns true. Flowcharting is similar but the diamond has false to left and looping back to the statements and true to right and drops out. It also is a post-test loop.
Writing a do-until Loop in Pseudocode
Do
statement
statement
etc.
Until condition
Format is like the other two loops looked at.
Deciding Which Loop to Use
The while loop is good to use if a condition may be false to start with and you do not want it to run if it is. The do-while is good for the same but you want it to run once. The do-until is good for a run once condition but you need it to run till test becomes true.
3. Count-Controlled Loops and the For Statement
A count-controlled loop will run a specific amount of times. A for loop is a specially designed count-controlled loop. The count controlled loop works by putting a value (initializing) a counter variable (aka counter). Then the loop starts by testing the counter against the maximum times we want it to ruin. If it is not reached it will do a series of statements and add 1 to the counter. It will then go back and test the loop and exit when the maximum is reached.
The for Statement
Since these loops are common most languages provide for them with a for loop. The for will initialize and then test the loop each time through.
Using the Counter Variable in the Body of the Loop
While the purpose of a counter is store how many times the loop has been done, the variable can be used like any other variable as long as it is not changed as that would affect the loop.
Incrementing by Values Other Than 1
The value incremented is called the step amount which by default is 1. Most languages provide to use another value other than 1 by changing the step value.
Counting Backwards by Decrementing the Counter Variable
To count backward in the loop set the maximum value as less that the initial value and the step to a negative number.
Letting the User Control the Number of Iterations
Usually the programmer will set the maximum amount of times but it is possible to ask the user how many times they want to do it and set the maximum value.
Designing a Count-controlled while Loop
Any loop can become a count controlled loop.
- Declare the counter to a start value
- While the counter is less than or equal to the maximum value do the statements. Make sure the last statement adds 1 to the counter
- If counter is equal or greater to maximum value then drop out of loop.
If you do not add to the counter it will run an infinite amount of times. To use values other than 1 just tell the counter to add that number to itself. This works for negative numbers as well.
4. Calculating a Running Total
A running total is a sum of numbers that accumulates with each time through the loop. It is called an accumulator. You would set the accumulator to its initial value, then start the loop and add it to the initial value. As long as there is something to add you would keep the loop going, otherwise drop out.
5. Sentinels
A sentinel is a special value that marks the end of a list of values. It can be a value we have coded into the program or it usually is a prompt for the user to put in a value to quit the loop. To be useful as a prompt it must be clear that this would not be a normally input value.
6. Nested Loops
A nested loop is a loop inside another loop. An inner loop will go through all of its loops each time the outer loop is done.
Learning to Program in C |
No comments:
Post a Comment