Chapter 5 Flashcards
(41 cards)
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>