CH 5: Loops and Files
increment operator
++ increases the value of a variable by 1
Before data can be written to or read from a file, what things must happen?
- a file stream object must be created -the file must be opened and linked to the file stream object
decrement operator
-- decreases the value of a variable by 1
If the accumulator starts with any value other than ____, it will not contain the correct total when the loop finishes
0 (zero)
Programs that calculate the total of a series of number typically use 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
A count-controlled loop must possess three elements:
1.) It must initialize a counter variable to a starting value 2.) It must test the counter variable by comparing it to a maximum value. When the counter variable reaches its maximum value, the loop terminates 3.) It must update the counter variable during each iteration. This is usually done by incrementing the variable
what cout statement will be displayed given the following program segment? a = 2 b = 5 c = a * b++; cout << a << " " << b << " " << c ;
2 6 10 because the b is in postfix mode
what cout statement will be displayed given the following program segment? a = 2 b = 5 c = a * ++b; cout << a << " " << b << " " << c ;
2 6 12 because the b is in prefix mode
The _______ header file defines the data types ofstream, ifstream, and fstream.
<fstream>
what header file does C++ file access require?
<fstream> , it contains all the declarations necessary for file operations
When processing a file.....
Data is either written to the file (if it is an output file) or read from the file (if it is an input file)
True or False The do-while loop doesn't perform if the expression is false
False the do-while loop always performs at least one iteration, even if the expression is false to begin with
True or False In a while loop, the semicolon comes after the while expression
False the semicolon is placed after the statement, if placed after the while expression it is assumed to be anull statement and disconnects the while statement from the block that comes after it
When a file is used by a program, three steps must be taken. What are they?
Open the file Process the file Close the file
What loop is a pretest loop and is ideal in situations where the exact number of iterations is known?
The for loop
What loop is a pretest loop and is ideal in situations where you do not want the loop to iterate if the condition is false from the beginning?
The while loop, a conditional loop, it repeats as long as a particular condition exists
True or False Although a program's files are automatically closed when the program shuts down, it is a good programming practice to write statements that close them
True
True or False In a while loop, a loop will never iterate if the test expression is false to start with
True
True or False It is possible to write a for loop in such a way that it will never iterate
True
True or False the for loop is a pretest loop
True
True or False It is possible to create a User-Controlled for loop
True the user can determine the maximum value of the counter variable in a for loop and therefore determine the number of times the loop iterates
what does the following display? cout << endl ;
a blank line
when using an increment operator in statement that does more than incrementing, what happens if the increment operator is in postfix mode?
causes the increment to happen after the value of the variable is used in the expression ex: num = 4 cout << num++; cout will display 4, then num will be incremented to 5
After the program is finished using the file, the file must be _______.
closed closing a file disconnects the file from the program
loop control variable
controls the number of times the loop iterates
A _____ _____ is a small "holding section" of memory to which file-bound data is first written.
file buffer
Most operation systems temporarily store data in a ___ _____ before it is written to a file
file buffer
When a ____ _____ is filled, all the data stored there is written to the file. This technique improves the systems 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.
file buffer
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
what are the three expression in parentheses following the loop header "for" in a for loop?
initialization test update (the third expression is not followed by a semicolon)
The ___________ __________ is normally used to initialize a counter variable to its starting variable. This is the first action performed by the for loop and it is only done once.
initialization expression
The while loop is known as a ______ loop, which means it tests its expressions before each iteration
pretest
The read operation that takes place just before the loop is called a __________ _____. It provides the first value for the loop to test.
priming read
input validation
process of inspecting data given to a program by the user and determining if it is valid
what are the following examples of.... word processors image editors spreadsheets games web browsers
programs that store data in files
When you work with a ______-_____ file, you can jump directly to any piece of data in the file without reading the data that comes before it.
random-acess (also known as direct acess file)
what expressions make it very convenient to use a counter variable to control the number of iterations that the loop performs?
the built-in expressions in the for loop, initialization testing updating
what are the three looping control structures in C++?
the while loop the do-while loop for loop
which loop is useful for validating input?
while loop
Two _____ are needed to represent one backlash in a string literal
backslashes
A _____ file contains data that has not been converted to text. Thus, you cannot view its contents with a text editor.
binary
do-while loop format
do statement; while (expression) ;
format of the do-while loop when the body of the loop contains multiple statements
do { statement; statement; } while (expression) ;
You need to write a loop that will keep reading and adding integers to a sum, until the sum reaches or exceeds 21. The numbers are all less than 20 and the sum is initially 0. Which is the preferred loop construct to use?
do while loop
The ___-____ loops is a posttest loop, which means its expression is tested after each iteration.
do-while
what loop is a good choice for repeating a menu?
do-while loop
What loop is a posttest loop and is ideal in situations where you always want the loop to iterate at least once?
do-while loop, a conditional loop
conditional loop
executes as long as a particular condition exists when you write one of these, you have no way of knowing the number of times it will iterate. ex: an input validation loop executes as long as the input value is invalid
An ________ file is a file to which data is written.
output, the program stores output in it
The process of retrieving data from a file is know as __________ from the file. When a piece of data is read from a file, it is copied from the file into a variable in RAM.
reading data
A _____ is a special value that marks the end of a list of values.
sentinel
There are two general ways to access data stored in a file....
sequential access and direct access
When you work with a ______-_____ file, 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
what is the programming style you should use with the while loop?
similar to that of the if statement -if there is only one statement repeated by the loop, it should appear on the line after the while statement and be indented one additional level -if the loop repeats a block, each line inside the braces should be indented
if the ________ expression is left out, the loop has no built-in way of terminating.
test
The _______ _________ controls the execution of the loop. As long as this execution is true, the body of the for loop will repeat. The for loop is a pretest loop, so it evaluates the ______ _________ before each iteration.
test expression
A _______ file 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 a Notepad.
text
In general, there are two types of files....
text and binary
If we want to combine multiple expressions in the test expression, what operators must be used?
the && or | | operators
The while loop works like an if statement that executes over and over. As long as the expression inside the parentheses is ______, the conditionally executed statement or block will repeat.
true
The ________ _________, is a statement that increments the loop's counter variable. It executes at the end of each iteration.
update expression
accumulator
variable used to keep the running total, accumulates the total of the numbers as it reads each number in a series
The _______ loop has two important parts: 1.) an expression that is tested for a true or false value 2.) a statement or block that is repeated as long as the expression is true
while
format of the while loop
while (expression) statement; expression is any expression that can be evaluated as true or false and statement is any valid C++ statement
You need to write a loop that reads integers and adds them to a sum as long as they are positive. Once 0 or a negative value is read in your loop terminates. Which is the preferred loop construct to use?
while loop
validating input that has been read and reading lists of data terminated by a sentinel value are good applications of the __________ loop.
while loop
Programmers usually refer to the process of saving data in a file as ____________ to the file. When a piece of data is written to a file, it is copied from a variable in RAM to the file.
writing data
True or False It is impossible to execute more than one statement in the initialization expression and the update expression
False It is possible, when using multiple statements in either of these expressions, simply seperate the statements with commas
True or False The update expression cannot be omitted if it is being performed elsewhere in the loop
False it can be omitted, even if none is needed
what is the program output of the following programming using a simple while loop? #include <iostream> using namespace std; int main ( ) { int number = 0; while (number < 5) { cout << "Hello\n" ; number++; } cout << "That's all!\n" ; return 0 ; }
Hello Hello Hello Hello Hello That's all! because each time the expression is tested, it adds an increment so after the 5th hello the expression would be 6 defying the (number < 5), that's why the cycle stops repeating and goes to the following statement "That's all"
what happens when opening a file?
It creates a connection between the file and the program.
When a program needs to save data for later use, it writes the data in a _____. The data can then be read from the _____ at a later time.
file
A ____ _______ ______ is an object that is associated with a specific file and provides a way for the program to work with that file. A file can be thought of as a stream of data.
file stream object
The ____ member function of cout changes the fill character, which is a space by default
fill member
The ____ loop is ideal for performing a known number of iterations.
for
Write a simple for loop that prints "Hello" five times
for (count = 0; count < 5; count++) cout << "Hello" << endl;
format of the for loop when it is used to repeat a single statement
for (initialization; test; update) statement;
format of the for loop when it is used to repeat a block
for (initialization; test; update) { statement; statement; //Place as many statements here // as necessary. }
The ___ ____ is specifically designed to initialize, test, and update a counter variable.
for loop
You have a variable, n, with a non-negative value, and need to write a loop that will keep print n blank lines. What loop construct should you use?
for loop
You should use the ____ _____ instead of the while or do-while loop in any situation that clearly requires an initialization, uses a false condition to stop the loop, and requires an update to occur at the end of each loop iteration.
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. Write an example.
ifstream inputFile("Customers.txt");
when using an increment operator in statement that does more than incrementing, what happens if the increment operator is in prefix mode?
increment happens first ex: num = 4; cout << ++num; cout will display 5
.doc
indicates that the file contains a Microsoft Word document
.jpg
indicates that the file contains a graphic image that is compressed
.txt
indicates that the file contains text
In nested loops..... -an ________ loop goes through all of its iterations for each iteration of an outer loop -Inner loops complete their iterations faster than _______ loops -to get the total number of iterations of a nested loop, multiply the number of iterations of all the loops
inner outer
Opening an _______ file allows the program to read data from the file.
input
An ______ file is a file from which the data is read.
input, the program gets input from the file
Calling the file stream object's close member function closes a file. Write an example.
inputFile.close( );
Each repetition of a loop is known as an _______.
iteration
infinite loop
loop continues to repeat until the program is interrupted
the operand of the increment and decrement operators must be an __________
lvalue, identifies a place in memory whose contents may be changed
A loop that is inside another loop is called a _______ loop
nested
Output file stream. You create an object of this data type when you want to create a file and write data to it.
ofstream
Write an example that defines an ofstream object named outputFile and opens the Employees.txt file
ofstream outputFile("Employees.txt");
True or False the stream insertion operator (<<) can be used with ofstream objects to write data to a file.
True
True or False when increment and decrement operators are used in statements that do more than just incrementing or decrementing (mathematical or relational expressions) it is important if they are in postfix or prefix mode
True
True or False A set of braces are needed when writing a loop that conditionally executes a set of statements
True
True or False In simple statements it doesn't matter if the increment or decrement operator is used in postfix or prefix mode.
True
True or False 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 systems resources than necessary,
True
True or False The do-while loop must be terminated with a semicolon
True
True or False The initialization expression may be omitted from inside the for loop's parentheses if it has already been performed or no initialization is needed.
True
True or False variables defined inside a function have no guaranteed starting value
True
True or False the increment and decrement operators can be used in both postfix mode and prefix mode
True prefix mode: ++num; --num; postfix mode: num++; num--;
loop
a control structure that causes a statement or group of statements to repeat
count-controlled loop
a loop that repeats a specific number of times, the exact number of iterations that a loop must perform is known
running total
a sum of numbers that accumulates with each iteration of a loop
counter
a variable that is regularly incremented or decremented each time a loop iterates
user-controlled loop
allows the user to decide the number of iterations
Opening an ______ file usually creates the file on the disk, and allows the program to write data on it.
output