Chapter 5 Flashcards
Increment and Decrement Operators
Increase or decrease by one
++ or –
Postfix and Prefix Increment/Decrement Operators
both with increment/decrement but the difference is present in statements that do more than just increment/decrement
POSTFIX
x = 1;
y = x++;
// y = x; y = 1; x++; x = 2;
PREFIX
x = 1;
y = ++x;
// ++x; x = 2; y = x; y = 2;
Loop
part of a program that repeats
while Loop
pretest conditional loop meaning it will not iterate if the condition is false. while the condition is true it will iterate. Ideal for validating input and reading lists of data terminated by a sentinel value.
while (expression)
statement;
1. 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
Pretest Loop
tests its expression before each iteration
Infinite Loops
continues to repeat until the program is interrupted
Counters
a variable that is regularly incremented or decremented each time a loop iterates
do-while Loop
posttest conditional loop which always iterates once with the expression test occuring after each iteration. Ideal for a repeating menu.
do
statement;
while (expression);
for Loop
pretest loop with built-in expressions for initializating, testing, and updating. ideal for performing a known number of iterations
for (initialization; test; update)
statement
count-controlled loop
for (initialization; test; update) { statement; }
- INITIALIZE a counter variable to a starting value
- TEST the counter variable by comparing it to a maximum value. when the counter variable reaches its maximum value, the loop terminates.
- UPDATE the counter variable during each iteration. This is usually done by incrementing the variable.
When should one use a for Loop instead of a while or do-while Loop?
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.
Is it possible to use multiple statements in the initialization and update expressions of a for Loop?
Yes. they must be separated by a comma.
for (x = 1, y = 1; x < 10; x++, y++)
Is it possible to use multiple statements in the test expression of a for Loop?
Yes, but the logical operators (|| &&) must be used.
running total
a sum of numbers that accumulates with each iteration of a loop.
accumulator
variable used to keep the running total
sentinel
special value that marks the end of a list of values
cout.fill()
member function that changes the fill character, which is a space by default. Can change it from a space to a zero for example.
How does a program save data for later use?
It writes the data in a file which can be read at a later time.
output file
A file to which data is written. It is called an output file because it stores output.
input file
A file from which data is read. It is called an input file because the program gets input from the file.
text file
contains data encoded as texting using ASCII or Unicode.
sequential-access file
start at the beginning and must move through every piece of data to progress through the file. Like a cassette tape.
random-access file or direct access file
can jump directly to any piece of data in the file. Like an MP3 player.
What is the header file that must be including before a C++ program can work with a file?
<fstream>
</fstream>
<fstream>
</fstream>
header file that defines the data types ofstream, ifstream, and fstream. Before a C++ program can work with a file, it must define an object of one of these data
ofstream
Output file stream. You create an object of this data type when you
want to create a file and write data to it.
ifstream
Input file stream. You create an object of this data type when you
want to open an existing file and read data from it.
What is the code to open a file for input (reading)?
ifstream inputFile;
inputFile.open(“Customers.txt”);
ifstream inputFile(“Customers.txt”);
What is the code to open a file for output (writing)?
ofstream outputFile;
outputFile.open(“Employees.txt”);
ofstream outputFile(“Employess.txt”);
What is the code to close a file?
inputFile.close();
inputFile.fail()
member class of the ifstream file object(inputFile)
returns true if the if stream file object did not open
How can you let the user decide what file to open?
Let the user define the file name in a string variable.
5.16 What is an output file? What is an input file?
An output file allows you to create, write, and save data into a file.
an input file allows you to read data in a file
5.17 What three steps must be taken when a file is used by a program?
- Open the file— creates a connection between the file and the program.
- Process the file—Data is either written to the (output) file or read from the (input) file.
- Close the file—After the program is finished using the file, the file must be closed. Closing a file disconnects the file from the program.
binary file
contains data that has not been converted to text. Cannot view the contents with a text editor.
5.20 What type of file stream object do you create if you want to write data to a file?
output file
ofstream outputFile;
5.21 What type of file stream object do you create if you want to read data from a file?
input file
ifstream inputFile;
5.22 Write a short program that uses a for loop to write the numbers 1 through 10 to a file.
include <iostream></iostream>
#include <fstream>
using namespace std;</fstream>
int main() {
ofstream outputFile;
for (int numbers = 1; numbers <= 10; numbers++) { outputFile << numbers; } }
5.23 Write a short program that opens a file created and reads all of the numbers from the file.
int number;
ifstream inputFile;
inputFile.open(“filename.txt”)
if (inputFile ) {
cout «_space;number;
}
else cout «_space;“Error. File could not be opened.”;
break statement
causes a loop to terminate early.
if (condition) break;
continue statement
causes a loop to stop its current iteration and being the next one
if (condition) continue;