Chapter 5

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Iteration

-Each repetition of a loop

Initialization Expression

-The first expression in a for loop -Normally used to initialize a counter variable to its starting value -The first action performed by the loop, and is only done once -If the counter variable is used only in the loop, it makes sense to define & initialize it in the initialization expression -Can execute more than one statement in the initialization expression; separate the statements with commas -May be omitted from the loop header if it has already been performed or no initialization is needed ex. int num = 1 for ( ; num <= maxVal; num++)

Input Validation

-The process of inspecting data given to a program by the user and determining if it is valid

Reading data

-The process of retrieving data from a file -When a piece of data is read from a file, it is copied from the file into a variable in RAM

Writing data

-The process of saving data in a file -When a piece of data is written to a file, it is copied from a variable in RAM to the file

Postfix Mode

-The unary operator for increasing or decreasing is placed after the variable -The increment/decrement will happen after the value of the variable is used in the expression

Prefix Mode

-The unary operator for increasing or decreasing is placed before the variable -The increment/decrement will happen first

Loop control variable

-The variable that controls the number of times that the loop iterates

Update expression

-Third expression in a for loop -Executes at the end of each iteration -Typically the statement that increments the loop's counter variable -Not limited to using increment statements in the update expression, ex. num+=2 can be the update -Can execute more than one statement in the update expression; separate the statements with commas -May omit the update expression if it is being performed elsewhere in the loop or if none is needed (not recommended) ex. int num = 1; for ( ; num <= maxValue; )

Writing data to a file

-Use the stream insertion operator (<<) with ofstream objects to write data to a file

Accumulator

-Variable used to keep the running total 1) Set accumulator to 0 2) Is there a number to read? Yes (True) 1) Read the number 2) Add the number to the accumulator No (false) -Accumulator will contain the total of the numbers that were read by the loop

fail member function

-Way to detect a failed attempt to open a file inputFile.fail()

Read position

-When a file has been opened for input, the file stream object internally maintains a special value known as a read position -Marks the location of the next byte that will be read from the file -When an input file is opened, its read position is initially set to the first byte in the file

Deciding which loop to use

-While loop: ideal in situations where you do not want the loop to iterate if the condition is false from the beginning; validating input that has been read & reading lists of data terminated by a sentinel value -do-while loop: Ideal in situations where you always want the loop to iterate at least once; good choice for repeating a menu -for loop: Ideal in situations where the exact number of iterations is known

Steps when using a file

1) Open the file: Creates a connection between the file and the program; opening an output file usually creates the file on the disk and allows the program to write data to it; opening an input file allows the program to read data from the file 2) Process the file: Data is either written to the file (output file) or read from the file (input file) 3) 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

do-while Loop

-General format: do { statement; statement; } while (expression); -Must be terminated with a semicolon -Always performs at least one iteration, even if the expression is false to begin with -Good choice for repeating a menu

fstream

-Header required for file access -Defines the data types ofstream, ifstream, and fstream -Before a program can work with a file, it must define an object of one of these data types -Also a file stream data type; can be used to open files for reading, writing, or both

Increment

-Increase value by one -Unary operator: ++

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

Pretest loop

-It tests the expression before each iteration -The while loop is a pretest loop

ofstream

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

Test Expression

-Second expression in a for loop -Controls the execution of the loop -As long as this expression is true, the body of the for loop will repeat -If you want to combine multiple expressions in the test expression, you must use the && or || operators

Body

-Statement of the while loop that is repeated -Considered a conditionally executed statement, because it is executed only under the condition that the expression is true

Reading data from a file

-The >> operator reads data from a file

for loop

-Count-controlled loop -Specifically designed to initialize, test, and update a counter variable -General format: for (initialization; test; update) { statement; statement; } -Pretest loop, evaluates the test expression before each iteration -Performs the initialization expression, then evaluates the test expression (if false, the loop is terminated), if the expression is true the body of the loop is executed, then the update is performed, and the test expression is evaluated again

Decrement

-Decrease value by one -Unary operator: --

Conditional loop

-Executes as long as a particular condition exists

Loop

-A control structure that causes a statement or group of statements to repeat

Input File

-A file that data is read from -Program gets input from the file

Output file

-A file that data is written to -Program stores output in it

Infinite loop

-A loop does not have a way of stopping, and continues to repeat until the program is interrupted

Nested loops

-A loop that is inside another loop -An inner loop goes through all of its iterations for each iteration of an outer loop -Inner loops complete their iterations faster than outer loops -To get the total number of iterations of a nested loop, multiply the number of iterations of all the loops

Count-controlled loop

-A loop that repeats a specific number of times -Must initialize a counter variable to a starting value -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; usually done by incrementing the variable

Sentinel

-A special value that marks the end of a list of values -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

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 -Keeps count of the number of iterations the loop has performed

Sequential access file

-Access data from the beginning of the file to the end of the file

User-controlled loop

-Allows the user to decide the number of iterations, because the loop asks the user if he or she wants to repeat the process

Direct access file

-Also known as random access file -Can jump to any piece of data in the file without reading the data that comes before it

While Loop

-An expression that is tested for a true or false value -A statement or block that is repeated as long as the expression is true -General format: while (expression) { statement; statement; } -Expression is any expression that can be evaluated as true or false, and statement is any valid C++ statement -The first line is the loop header, which consists of the key word while followed by an expression enclosed in parentheses

File stream object

-An object that is associated with a specific file and provides a way for the program to work with that file -Must create a file stream object in memory in order for the program to work with a file on the computer's disk

Closing a file

-Causes any unsaved data that may still be held in a buffer to be saved to its file -Will not deplete more of the operating system's resources than necessary objectName.close();

Text File

-Contains data that has been encoded as text, using a scheme such as ASCII or Unicode -Numbers are stored in the file as a series of characters

Binary file

-Contains data that has not been converted to text

Posttest loop

-Expression is tested after each iteration

Opening a file

-For input (reading) ifstream inputFile; inputFile.open("fileName.txt"); -For output (writing) ofstream outputFile; outputFile.open("fileName.txt"); -When opening a file, you will need to specifiy its path as well as its name ex. inputFile.open("C:\\data\\inventory.txt")


Set pelajaran terkait

HW11: Homework - Ch. 11: Behind the Supply Curve: Inputs and Costs

View Set

Chapter 18: macOS, Linux, and Scripting

View Set

ATI NURS 126 Pharmacology Practice B

View Set

Child Health Final Exam Practice Questions

View Set

Resp 23 - Transport of CO2 from the Tissues

View Set

The Constitution: Article III - Judicial Branch

View Set

Check the Solution - Systems of Equations

View Set

Chapter 9: Head and Neck Anatomy

View Set

Linguistics 101 Quiz 6: German Phonology Problem

View Set

The Distributive Property (Quiz)

View Set