Ch. 4 Loops (Lessons 1-12)

¡Supera tus tareas y exámenes ahora con Quizwiz!

Compute the product of all integer elements within the vector primeNumbers. int primeProduct = 1; for (int theNumber....... ) { primeProduct = primeProduct * theNumber; }

.... : primeNumbers) The for loop will iterate through each element of the vector primeNumbers.

To visit every character in a string, a for loop should iterate over indices _____. 0 to size 0 to size-1 1 to size

0 to size-1; The last index is at size-1. Ex: For "Hey", length is 3, with H at 0, e at 1, and y at 2.

How many times will this loop iterate? for (i = 0; i < 8; ++i) { ...... } 7 times 8 times 9 times

8 times; The iterations will be for i of 0, 1, 2, 3, 4, 5, 6, and 7; counting those yields 8 iterations.

4.4 What are the values of i for each iteration of: for (i = 0; i < 6; ++i) { ...... } 1, 2, 3, 4, 5 0, 1, 2, 3, 4, 5 0, 1, 2, 3, 4, 5, 6

0, 1, 2, 3, 4, 5; i starts with 0. When i reaches 6, the loop body will not be entered because 6 < 6 is false.

Complete the program such that variable count ends having the number of negative values in an input list of values (the list ends with 0). So if the input is -1 -5 9 3 0, then count should end with 2. count = 0 val = Get next input While val is not 0 If __(A)__ __(B)__ val = Get next input

A = val < 0 B = count = count + 1; Variable count should be incremented each time a negative input value is seen.

4.11 block

A block is a brace-enclosed {...} sequence of statements, such as found with an if-else, for loop, or while loop. A variable name's scope extends from the declaration to the closing brace }.

continue statement

A continue statement in a loop causes an immediate jump to the loop condition check. A continue statement can sometimes improve the readability of a loop.

scope

A declared name is only valid within a region of code known as the name's scope. Ex: A variable userNum declared in main() is only valid within main(), from the declaration to main()'s end.

Loop basics

A loop is a program construct that repeatedly executes the loop's statements (known as the loop body) while the loop's expression is true; when false, execution proceeds past the loop. Each time through a loop's statements is called an iteration.

While Loop

A while loop is a program construct that repeatedly executes a list of sub-statements (known as the loop body) while the loop's expression evaluates to true. Each execution of the loop body is called an iteration. Once entering the loop body, execution continues to the body's end, even if the expression would become false midway through.

sentinel value

Loops are commonly used to process an input list of values. A sentinel value is a special value indicating the end of a list, such as a list of positive integers ending with 0, as in 10 1 6 3 0. The example below computes the average of an input list of positive integers, ending with 0. The 0 is not included in the average.

If the input value is 0, does the loop body execute? Yes No

No; As long as the input value is not 0 (such as -1, -5, 9, or 3), the loop body will execute. But when the value IS 0, the loop's expression is false and thus the loop body is not executed.

enumeration type

Some variables only need store a small set of named values. For example, a variable representing a traffic light need only store values named GREEN, YELLOW, or RED. An enumeration type declares a name for a new type and possible values for that type.

state machine

The example illustrates the idea of a state machine that is sometimes used in programs, especially programs that interact with physical objects, wherein the program moves among particular situations ("states") depending on input; see What is: State machine.

range-based loop

The range-based for loop is a for loop that iterates through each element in a vector or container. A range-based for loop is also known as a for each loop. The range-based loop declares a new variable that will be assigned with each successive element of a container, such as a vector, from the first element to the last element.

FIXME comments provide a way for a programmer to remember what needs to be added.

True; FIXME comments help a programmer keep track of what hasn't been done yet.

Incremental programming may help reduce the number of errors in a program. True False Incremental programming helps catch errors earlier, before they get lost inside bigger programs.

True; Incremental programming helps catch errors earlier, before they get lost inside bigger programs.

Complete the for loop to iterate 500 times. (Don't forget the parentheses). for { ... }

(i = 0; i < 500; ++i) The loop will iterate with i of 0, 1, ..., 488, and 499, which is 500 iterations.

Once a program is complete, one would expect to see several FIXME comments.

False FIXME comments should have been replaced by actual code in a completed program.

Expert programmers need not develop programs incrementally. True False

False; Even experts program incrementally. They may take slightly bigger steps than a novice, but expert programmers are continually compiling and running their programs as they write code.

for ( ...... : interestRates) { cout << theRate << "%" << endl; }

double theRate The variable must be declared within the range-based for loops' parentheses before the colon (:).

4.9 A good programming process is to write the entire program, then incrementally remove bugs one at a time. True False That's a terrible way to program, yet that's how many new programmers program. Better to first write a simple version of the program, test it, and then add more behavior incrementally.

false; That's a terrible way to program, yet that's how many new programmers program. Better to first write a simple version of the program, test it, and then add more behavior incrementally.

Goal: Loop 10 times for (i = 0; _____; ++i) { ... } i < 9 i < 10 i < 11

i < 10; The 10 iterations will have i of 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. (Because counting started at 0, iterating with i = 10 would yield 11 iterations.).

Goal: Loop 99 times for (i = 0; _____; ++i) { ... } i < 99 i <= 99 i == 99

i < 99; The iterations will have i of 0, 1, ..., 97, 98.

Complete the for loop to iterate numDogs times. numDogs is an int. for (i = 0;.... ++i) { ... }

i < numDogs; The loop will iterate with i of 0, 1, ..., numDogs-1. So if numDogs is 5, the loop will iterate with i as 0, 1, 2, 3, and 4 (so 5 times).

Goal: Loop numYears times (numYears is an int variable). for (i = 0; _____; ++i) { ... } numYears i <= numYears i < numYears

i < numYears; The desired number of iterations is commonly in a variable like numYears. If numYears is currently 5, then the loop will iterate with i as 0, 1, 2, 3, and 4 (so 5 iterations).

Goal: Loop 20 times for (____; i < 20; ++i) { ... } i = 0 i = 1

i = 0; Because the comparison is i < 20, then i should start with 0. (If the comparison was i <= 20, then i should start with 1).

If a for loop iterates through a string s using variable i, the loop body can access the current character as: s.at( _____ ) i-1 i i+1

i; i will vary from 0 to size-1, and is used to index the current character in the string.

Calculate the sum of all values within the sensorReadings vector. double sumVal = 0.0; for (double readingVal : sensorReadings) { sumVal =.... ; }

sumVal + readingVal The range-based for loop assigns readingVal with each element of sensorReadings. The for loop's statement adds readingVal to sumVal to calculate sum of all values.


Conjuntos de estudio relacionados

Quiz #1: Chapter 8 Abdomen Vascular

View Set

Foundations of Project Management: Week 2 - Module 2 Challenge

View Set

INB 300 Chapter 8, Chapter 8: Foreign Direct Investment, Chapter 8 International Business, International business Exam 2, IB101 chapter 9, Global Business Chapter 6, Global Business Chapter 9, International Business Chapter 18, Chapter 12 Global Fina...

View Set

4.13.F - Lesson: Reading Check Cantos 9, 24, and 26

View Set

HBS Core - Accounting, Financial Accounting

View Set