CS 107: Chapter 5

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Programs that calculate the total of a series of numbers typically use these two elements.

1) A loop that reads each number in the series 2) A variable that accumulates the total of the numbers as they are read

List the advantages of using repetition structure

1) Reduces duplicated code, which makes the program large. 2) Shorter sequence of statements 3) Centralized, shorter code means corrections and changes are easier and quicker to make.

Explain the key difference between a condition-controlled loop and a count-controlled loop

A condition-controlled loop, such as the while loop, uses Boolean evaluation. In contrast, a count-controlled loop uses a counter.

Explain how a counter-controlled loop works

A counter-controlled loop uses three processes: 1) Initialization: counter variable is initialized to a specific value before the loop begins. 2) Test: the loop tests the counter variable by comparing it to the maximum value. 3) Increment: Increase the amount of the counter variable, usually by one.

infinite loop

A loop in which the terminating condition is never satisfied. This is usually the result of a logic error - for example, forgetting to increment, decrement, or otherwise change the value of the condition in the loop statement.

For-loop

A particular kind of count-controlled loop which uses a counter as the control/test.

The while loop is a pretest loop. Explain.

A pretest loop tests its condition before performing the iteration - that is, at the beginning of the loop. Because of this, steps must be taken to ensure that the loop executes at least once.

Provide a use case for sentinels

A sentinel is a better technique when processing a long list of values with a loop, especially where the exact number of items may vary or is not known.

Why should you take care in choosing the value of a sentinel?

A sentinel value must be unique enough that it will not be mistaken for a regular value. For example, if a list of values consists of weights, zero can be used as a sentinel value because no one's weight is zero.

loop iteration

A sequence of instructions that is repeated until a certain condition is met.

In addition to using Step with a negative number such as -1 and inverting the counter, what is another method for counting backwards in a loop?

A simple method is to decrement a loop; that is, is to simply subtract from the counter value, such as: Set counter = counter - 1

condition-controlled loop

An iteration statement that repeats a set of instructions until a true/false condition is met.

while loop

An iteration structure that works as: While a condition is true, do some task. The loop has two parts: 1) a condition that is tested for true or false value, and 2) a statement or set of statements that is required as long as the condition is true.

repetition structure

Commonly known as a loop.

List example uses for a counter-controlled loop

Counter-controlled loops are used in cases where the exact number of times when iteration is needed is known. For example, days of the week, days of a month, etc.

do-while loop pseudocode

Do .....statement .....statement .....etc. While condition These statements are the body of the loop. They are always performed once, and then repeated while the condition is true.

Explain how to count backward in a counter-controlled loop.

For a counter-control loop to count backwards, the code must use Step with a negative integer, with maxValue initializing the counter. The last value should be the lowest number. Example: For counter = 10 to 1 Step -1 .....statement .....etc. End For

Show the typical structure of a for-loop

For counterVariable = startingValue to maxValue .....statement .....statement .....etc. End For

Explain the most common logic error in counter-controlled loop code.

Forgetting to increment, or decrement, the counter variable.

Explain the key difference between a while loop, a do-while loop, and do-until loop.

In a while loop, the loop condition is checked first before proceeding to the statements, meaning it is a pre-test loop. In a do-while loop one or more statements are executed, then the condition is tested and the loop continues if the condition evaluates to true. Do-until works very similarly to do-while, except that the loop continues if the condition evaluates to false. The while loop is a pretest loop; both do-while and do-until are posttest loops.

Must all counter-controlled loops, such as the for-loop, increment by one? Explain.

No, the increment, known as the step amount, does not have to always be one. In code, the step is assumed to be one unless otherwise specified. For cases where the step amount must be greater than 1, simply add step and the desired amount explicitly to the code. Example: For counter = 0 to 100 Step 10 .....statement .....statement .....etc. End For Remember, if Step is not explicitly set otherwise, the loop will assume that the Step value is 1.

Is a do-while loop necessary? Explain.

No. Any loop that is written as a Do-While loop can also be written as a While loop.

iteration structure

The code required to create a loop

In a counter loop, what does the control word "to" mean?

The control word "to" has the same meaning as "less than or equal to". Therefore, the loop "For counter = 1 to 5" means "Iterate the loop as long as counter is less than or equal to 5"; thus the loop would execute 5 times.

while loop pseudocode

While condition .....statement .....statement .....etc. End While The statements are the body of the loop. They are repeated while the condition is true.

List the three main types of loops

While, Do-While and Do-Until

Explain the key differences between for loop and while loop

With the For loop, the number of iterations is known; it will not execute less or more than the maxValue given., though some languages provide mechanisms to break out of the loop before the terminating value is reached. With the While loop, the number of iterations is not explicitly set, and in fact, because the While loop is a condition-controlled loop, a Boolean evaluation determines whether the loop iterates; that is, the While loop has to continually evaluate the condition at the top of the loop, even if the value is changed within the statements. Although you can write a While loop to emulate the same behavior of a For loop, the exact mechanics of the two are indeed a bit different. You cannot however take a For loop and make it operate like a While loop. A For loop has an absolute terminating value, whereas technically a While loop does not. The following provides a detailed but easy-to-understand explanation: https://diffzi.com/for-loop-vs-while-loop/

Should the accumulator be initialized

Yes, the accumulator should be initialized as zero because it is a running total, and this explicitly sets the initial value to ensure correct summing. This is because the code looks like this: sum = sum + 1 sum = sum + number If sum were not to be properly initialized, this could throw a math error, as technically an uninitialized variable has no value. Even worse, in some languages, a default value is automatically assigned that may not be zero, and this can lead to undesired results.

do-until loop

a loop that iterates as long as a condition is false, and then stops when the condition becomes true.

count-controlled loop

a loop that repeats a specific number of times

sentinel

a special value that marks the end of a list of values

running total

a sum of numbers that accumulates with each iteration of a loop.

do-while loop

repeatedly executes a block of statements until a specified Boolean expression evaluates to false.

accumulator

the variable used to keep the running total


Ensembles d'études connexes

BIOL 1009 topic 16 molecular basis of inheritance

View Set

Auditing Chapter 5 - Test Review Questions

View Set

(9) T-SQL Fundamentals: NULL Marks

View Set

Pharmacology Chapter 25 Muscle Relaxants

View Set

AP Environmental Science - Most Asked Questions

View Set

Chapter 15: Oncological Disorders

View Set

small business management chapter 6 planning your path into full-time business

View Set