Chapter 5 Loop and Files

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

_________are operators that add and subtract 1 from their operands

++ and --

Before data can be written to or read from a file, the following things must happen:

- A file stream object must be created. -The file must be opened and linked to the file stream object.

In order for a program to work with a file on the computer's disk, the program must create a file __________ in memory.

stream object

In general, there are two types of files: ___________

text and binary

A _________ contains data that has been encoded as text, using a scheme such as ASCII or Unicode. Even if the file contains numbers, those numbers are stored in the file as a series of characters. As a result, the file may be opened and viewed in a text editor such as Notepad

text file

The for loop is ideal in situations where ____________.

the exact number of iterations is known

The do-while loop is also a conditional loop. Unlike the while loop, however, do-while is a posttest loop. It is ideal in situations where you always want the loop to iterate ____ ____ _____

at least once

the do-while loop always performs ________________, even if the expression is false to begin with

at least one iteration

A ________ contains data that has not been converted to text. Thus, you cannot view the contents of a binary file with a text editor.

binary file

If you write a loop that conditionally executes a block of statements, don't forget to enclose all of the statements in a set of ________.

braces

Remember when data is stored in a text file, it is encoded as text, using a scheme such as ASCII or Unicode. Even if the file contains numbers, those numbers are stored in the file as a series of __________.

characters

A ____________loop executes as long as a particular condition exists. For example, an input validation loop executes as long as the input value is invalid.

conditional

When you write a _________ loop, you have no way of knowing the number of times it will iterate

conditional

In general, there are two categories of loops: ________loops and ___________-controlled loops.

conditional , count

Fortunately, you can use the >> operator to read data such as this from a text file into a numeric variable, and the >> operator will automatically _________the data to a numeric data type.

convert

A loop that repeats a specific number of times is known as a _________ loop. For example, if a loop asks the user to enter the sales amounts for each month in the year, it will iterate 12 times

count-controlled

A _________ is a variable that is regularly incremented or decremented each time a loop iterates.

counter

When a variable is _______ in the initialization expression of a for loop, the scope of the variable is _______ to the loop. This means you cannot access the variable in statements outside the loop

defined, limited

When you work with a random-access file (also known as a __________), you can jump directly to any piece of data in the file without reading the data that comes before it.

direct access file

The _____________ loop is a posttest loop, which means its expression is tested after each iteration.

do-while

You should use the do-while loop when you want to make sure the loop __________________

executes at least once.

__________ is any expression that can be evaluated as true or false

expression

The _________ usually indicates the type of data stored in the file.

extension

The __________ header file defines the data types ofstream, ifstream, and fstream.

<fstream>

The file ________ contains all the declarations necessary for file operations.

<fstream>

After the program is finished using the file, the file must be closed. __________ disconnects the file from the program.

Closing a file

_____ _____ ____ creates a connection between the file and the program

Opening a file

A count-controlled loop must possess three elements:

It must initialize a counter variable to a starting value. It must test the counter variable by comparing it to a maximum value. When the counter variable reaches its maximum value, the loop terminates. It must update the counter variable during each iteration. This is usually done by incrementing the variable.

Here are two reasons a program should close files when it is finished using them:

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.

_______ mode, however, causes the increment to happen first.

Prefix

_____Data is either written to the file (if it is an output file) or read from the file (if it is an input file).

Process the file

The variable used to keep the running total is called an ______.

accumulator

in C++ ------- backslashes are needed to represent one backslash in a string literal.

Two

A ____________ is an object that is associated with a specific file and provides a way for the program to work with that file. It is called a "stream" object because a file can be thought of as a stream of data.

file stream object

In order for a program to work with a file on the computer's disk, the program must create a ____________ in memory.

file stream object

Files on a disk are identified by a ____________

filename

The _________ loop is ideal for performing a known number of iterations.

for

The __________ loop is specifically designed to initialize, test, and update a counter variable.

for

Count-controlled loops are so common that C++ provides a type of loop specifically for them. It is known as the _________

for loop

The ____ _____ is a pretest loop that has built-in expressions for initializing, testing, and updating

for loop

File stream. Objects of this data type can be used to open files for reading, writing, or both.

fstream

Input file stream. You create an object of this data type when you want to open an existing file and read data from it

ifstream

It is possible to define a file stream object and open a file in one statement. Here is an example:

ifstream inputFile("Customers.txt");

The following code shows an example of opening a file for input (reading).

ifstream inputFile; inputFile.open("Customers.txt");

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

increment

An __________ loop continues to repeat until the program is interrupted.

infinite

If a loop does not have a way of stopping, it is called an _______ loop

infinite

You should use the for loop instead of the while or do-while loop in any situation that clearly requires an ______, uses a _______ condition to stop the loop, and requires an _______ to occur at the end of each loop iteration

initialization, false, update

Opening an _________ allows the program to read data from the file.

input file

Calling the file stream object's close member function closes a file. Here is an example:

inputFile.close();

Remember, variables defined _________ a function have no guaranteed starting value.

inside

It is possible to write a for loop in such a way that it will never _______.

iterate

Each repetition of a loop is known as an __________

iteration

a______ is part of a program that repeats

loop

The first line of the for loop is the _________ After the key word for, there are three expressions inside the parentheses, separated by semicolons. Notice there is not a _________ after the third expression.

loop header, semicolon

The do-while loop is a good choice for repeating a ________.

menu

The do-while loop is a good choice for repeating a ___________________

menu

Be careful not to place a statement that ___ ___ ___ variable in the body of the for loop

modifies the counter

A loop that is inside another loop is called a ______.

nested loop

Output file stream. You create an object of this data type when you want to create a file and write data to it.

ofstream

The following code shows an example of opening a file for output (writing).

ofstream outputFile; outputFile.open("Employees.txt");

The initialization expression may be _______ from inside the for loop's parentheses if it has already been performed or no initialization is needed. You may also omit the update expression if it is being performed elsewhere in the loop or if none is needed. You can even go so far as to omit all three expressions from the for loop's parentheses.

omitted

Opening an ___ ____ usually creates the file on the disk, and allows the program to write data to it.

output file

_________ mode causes the increment to happen after the value of the variable is used in the expression

postfix

In both ______ and _____ modes, these operators add 1 to or subtract 1 from their operand

postfix, prefix

The while loop is known as a _________ loop, which means it tests its expression before each iteration

pretest

The for loop is a ______ ______, so it evaluates the test expression before each iteration.

pretest loop

The read operation that takes place just before the loop is called a ________ read. It provides the first value for the loop to test.

priming

This marks the location of the next item that will be read from a file.

read position

A ____ ____ is a sum of numbers that accumulates with each iteration of a loop.

running total

The do-while loop must be terminated with a ___________.

semicolon

A ______ is a special value that marks the end of a list of values.

sentinel

A ________ is a special value that cannot be mistaken as a member of the list and signals that there are no more values to be entered. When the user enters the sentinel, the loop terminates.

sentinel

There are two general ways to access data stored in a file: ___________

sequential access and direct access.

When you work with a _______________, you access data from the beginning of the file to the end of the file. If you want to read a piece of data that is stored at the very end of the file, you have to read all of the data that comes before it—you cannot jump directly to the desired data.

sequential-access file

Programs that calculate the total of a series of numbers typically use two elements: (1) A loop that reads each number in the _____. (2) A variable that accumulates the total of the numbers as they are read.

series

The while loop works like an if statement that executes over and over. As long as the expression inside the parentheses is ______

true

The while loop has two important parts: (1) an expression that is tested for a _____ or _____ value, and (2) a statement or block that is repeated as long as the expression is _______

true, false, true

The while loop is especially useful for _________ input

validating

Input _________ is the process of inspecting data given to a program by the user and determining if it is valid.

validation

The do-while loop looks something like an inverted ______ loop.

while

the __________ loop is not complete without the statement that follows it.

while

The ___ ____ is a conditional loop, which means it repeats as long as a particular condition exists. It is also a pretest loop, so it is ideal in situations where you do not want the loop to iterate if the condition is false from the beginning

while loop

C++ has three looping control structures: the ________loop, the ______ loop, and the ______ loop. The difference between these structures is how they control the repetition

while, do-while, for


Kaugnay na mga set ng pag-aaral

Economics Chapter 4 Practice Exam

View Set

Computer Security Final (shorter)

View Set

NSG3332 Maternal Infant Exam 3 Review

View Set

Intro to Education midterm exam all quiz questions (1-6)

View Set

Quiz: Extension of Structures & Algorithms

View Set