Ch_5_Looping Flashcards
Loop
A control structure that causes a statement or group of statements to repeat.
Priming Read (Priming Statement)
The first read operation, which takes place just before the loop. It provides the first value for the loop to test.
Update Expression
The third expression in a for loop that executes at the end of each iteration, before the test expression is tested again.
++ Operator
Unary operator that increases an operand value by 1.
– Operator
Unary operator that decreases an operand value by 1.
Prefix Mode
Operator placed before the variable. Causes the increment/decrement to happen before the variable is used in an expression.
Postfix Mode
Operator placed after the variable. Causes the increment/decrement to happen after the variable is used in an expression.
Counter
A variable that is regularly incremented or decremented each time a loop iterates.
Running Total
Sum of numbers that accumulates with each iteration of a loop.
Accumulator
The variable used to store a Running Total.
Sentinel
A special value that marks the end of a list of values.
Sentinel Controlled Loop
A loop that terminates then the user enters a sentinel value.
Do-While Loop
A post-test loop. The expression is tested after each iteration.
For Loop
A Pretest Loop that combines the initialization, testing, and updating of a loop control variable in a single loop header.
Two Categories of Loops
- Conditional Loops
- Count Controlled Loops
Conditional Loop
Executes as long as a condition exists.
Count Controlled Loop
A loop that repeats a specific number of times.
When should the While Loop be used?
- Pre Test Loop
- When you do not want the loop to iterate if the test condition if false from the beginning.
- Input Validation
- When iterating over a list of data that terminates with a sentinel value.
When should the Do-While Loop be used?
- Post-Test Loop
- When you want the loop to iterate at least once.
- Repeating a menu.
- Asking users if they want to repeat a set of actions.
When should the For Loop be used?
- Pre-Test Loop
- Ideal in situations when the exact number of iterations is known.
Break Statement
- When encountered in a Loop, the loop immediately stops and the program moves to the statement immediately following the loop.
- In a nested loop, the break statement only breaks the loop it is placed in.
Continue Statement
When encountered, all the statements in the body of the loop that appear after it are ignored, and the loop prepares for the next iteration.
Text File
A file that contains data that has been encoded as text, using a scheme such as ASCII or Unicode.
Binary File
A file that contains data that has not been converted to text.
Two General File Access Methods
- Sequential Access
- Direct Access
Sequential Access File
You access data from the beginning to the end of the file, in sequence.
Direct Access (Random Access)
You can directly access any piece of data in the file without reading the data that comes before it.
File Stream Object
An object that is associated with a specific file and provides a way for the program to work with that file.
Five steps taken when a file is used by a program
- Include the header file needed to perform file input/output
- Define a file stream object
- Open the file
- Use the file
- Close the file
fstream Header FIle
- Contains all the declarations necessary for file operations.
- Defines the data types ofstream, ifstream, and fstream
ofstream
- Output File Stream
- Used to create a file and write data to it.
ifstream
- Input File Stream
- Used to open an existing file and write data to it
fstream
- File Stream
- Used to open files for reading, writing, or both.
cctype header file
-Needed to use the toupper() and tolower() functions