Chapter 5 Review

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Describe the difference between pretest loops and posttest loops.

A Pretest loop tests its condition before each iteration. A posttest loop tests its condition after each iteration. A posttest loop will always execute at least once.

Why is it critical that counter variables be properly initialized?

A counter is used to control or keep track of the number of times a loop iterates. In a loop, the counter is usually incremented or decremented. if the counter variable is not properly initialized, it will not hold the correct number.

Why are the statements in the body of a loop called conditionally executed statements?

Because they are only executed when a condition is true

Why should you indent the statements in the body of a loop?

By indenting the statements, you make them stand out from the surrounding code. This help you to identify at a glance the statements that are conditionally executed by a loop

Why should a program close a file when it's finished using it?

Most operating systems temporarily store data in a file buffer before it is written to a file. A file buffer is a small "holding section" of memory to which file-bound data is first written. When the buffer is filled, all the data stored there is written to the file. This technique improves the system's performance. Closing a file causes any unsaved data that may still be held in a buffer to be saved to its file. This means the data will be in the file if you need to read it later in the same program. Some operating systems limit the number of files that may be open at one time. When a program closes files that are no longer being used, it will not deplete more of the operating system's resources than necessary

Which loop should you use in situations where you wish the loop to repeat until the test expression is false, but the loop should execute at least one time?

The do-while loop

Which loop should you use in situations where you wish the loop to repeat until the test expression is false, and the loop should not execute if the test expression is false to begin with?

The while loop

What is the difference between the while loop and the do-while loop?

The while loop is a pretest loop and the do-while loop is a posttest loop.

What is a file's read position? Where is the read position when a file is first opened for reading?

When a file has been opened for input, the file stream object internally maintains a special value known as a read position. A file's read position marks the location of the next byte that will be read from the file. When an input file is opened, its read position is initially set to the first byte in the file. So, the first read operation extracts data starting at the first byte. As data is read from the file, the read position moves forward, toward the end of the file.

A(n) --------is a sum of numbers that accumulates with each iteration of a loop.

accumulator

Why is it critical that accumulator variables be properly initialized?

accumulator is used to keep a running total of numbers. In a loop, a value is usually added to the current value of the accumulator. If it is not properly initialized, it will not contain the correct total.

Why should you be careful not to place a statement in the body of a for loop that changes the value of the loop's counter variable?

because the for loop has an update expression that normally changes the content of a counter. If you have code inside that loop that also changes the counter, the loop will not able to properly control the value of the counter.

The statement causes a loop to terminate immediately.

break

The statement causes a loop to skip the remaining statements in the current iteration.

continue

A(n)-------- is a variable that "counts" the number of times a loop repeats.

counter

T /F The cout statement in the following program segment will display 5: int x = 5; cout << ++x;

false (should be 6)

To _____ a value means to increase it by one, and to ___ a value means to decrease it by one.

increment, decrement

Inside the for loop's parentheses, the first expression is the ______ , the second expression is the _______ , and the third expression is the ________ .

initialization, test, update

Each repetition of a loop is known as a(n) _______ .

iteration

A loop that is inside another is called a(n) ______ loop.

nested

What data type do you use when you want to create a file stream object that can write data to a file?

ofstream

When the increment or decrement operator is placed after the operand (or to the operand's right), the operator is being used in _______ mode.

postfix

A loop that evaluates its test expression after each repetition is a(n) ______ loop.

posttest

When the increment or decrement operator is placed before the operand (or to the operand's left), the operator is being used in ______ mode.

prefix

A loop that evaluates its test expression before each repetition is a(n) _______ loop.

pretest

A(n)--------- is a variable that is initialized to some starting value, usually zero, then has numbers added to it in each iteration of a loop.

running total

A(n) ----------is a special value that marks the end of a series of values.

sentinel

T /F In a nested loop, the inner loop goes through all of its iterations for every single iteration of the outer loop.

true

T /F The while loop is a pretest loop.

true

T/ F All three of the for loop's expressions may be omitted.

true

T/ F In a nested loop, the break statement only interrupts the loop in which it is placed.

true

T/ F When you call an ofstream object's open member function, the specified file will be erased if it already exists.

true

T/F Variables may be defined inside the body of a loop.

true

The _______ loops will not iterate at all if their test expressions are false to start with.

while and for

What header file do you need to include in a program that performs file operations?

<fstream>

Which loop should you use when you know the number of required iterations?

The for loop

The statement or block that is repeated is known as the ____ of the loop.

body

The --------- loop always iterates at least once.

do-while

T /F One limitation of the for loop is that only one variable may be initialized in the initialization expression.

false

T /F The for loop is a posttest loop.

false

T/ F It is not necessary to initialize counter variables.

false

T/ F The break statement causes a loop to stop the current iteration and begin the next one.

false

T/ F The continue statement causes a terminated loop to resume.

false

T/ F The do-while loop is a pretest loop.

false

T/F The operand of the increment and decrement operators can be any valid mathematical expression.

false

T /F In a nested loop, the outer loop executes faster than the inner loop.

false (Inner loops complete their iterations faster than outer loops.)

T/ F To calculate the total number of iterations of a nested loop, add the number of iterations of all the loops.

false (To get the total number of iterations of a nested loop, multiply the number of iterations of all the loops.)

The __________ loop is ideal for situations that require a counter.

for

What data type do you use when you want to create a file stream object that can read data from a file?

ifstream

A loop that does not have a way of stopping is a(n) --------loop.

infinite

T /F A variable may be defined in the initialization expression of the for loop.

true

T /F The cout statement in the following program segment will display 5: int x = 5; cout << x++;

true


Set pelajaran terkait

AL 10 / [windows] keybinds: 34.9

View Set

Chapter 43 Lewis questions- Lower GI

View Set

Pharmacology II Prep U Chapter 54: Drugs Acting on the Upper Respiratory Tract

View Set

Bus 381 - Chapter 14: Occupational Health and Safety

View Set

Test 2 Care and prevention of injury in athletes

View Set

Chromosome Structure and Function

View Set

Kap.9: Aktivitetsbasert kalkulasjon (ABC)

View Set

BUSINESS FOR LAW CHAPTER 7 QUESTIONS: CAPACITY OF CONTRACT

View Set