Chapter 5
Prefix vs. Postfix
++ and -- operators can be used in complex statements and expressions In prefix mode (++val, --val) the operator increments or decrements, then returns the value of the variable In postfix mode (val++, val--) the operator returns the value of the variable, then increments or decrements
Buffer
A file _________ is a small holding section of memory that file-bound information is first written to.
Opened
A file must be ________ before data can be written to or read from it.
False
A while loop is somewhat limited, because the counter can only be incremented by one each time through the loop.
outFile << number;
Assuming outFile is a file stream object and number is a variable, which statement writes the contents of number to the file associated with outFile?
Breaking Out of a Loop
Can use break to terminate execution of a loop Use sparingly if at all - makes code harder to understand and debug When used in an inner loop, terminates that loop only and goes back to outer loop
The continue Statement
Can use continue to go to end of loop and prepare for next repetition while, do-while loops: go to test, repeat loop if test passes for loop: perform update step, then test, then repeat loop if test passes Use sparingly - like break, can make program logic hard to follow
The for Loop
General Format: for(initialization; test; update) statement; Perform initialization Evaluate test expression If true, execute statement If false, terminate loop execution Execute update, then re-evaluate test expression
20
How many times will the following loop display "Hello"? for (int i = 0; i <= 20; i++) cout << "Hello!" << endl;
For
If you want a user to enter exactly 20 values, which loop would be the best to use?
Post test
The do-while loop is considered a(n) _________ loop.
Infinite Loop
The loop must contain code to make expression become false Otherwise, the loop will have no way of stopping this will repeat an infinite number of times
At least once
The statements in the body of a while loop may never be executed, whereas the statements in the body of a do-while loop will be executed:
Pre test
The while loop is considered a(n) _________ loop.
++ and --
These are operators that add and subtract one from their operands.
While
This is a pre-test loop that is ideal in situations where you do not want the loop to iterate if the condition is false from the beginning.
while
This is a pre-test loop that is ideal in situations where you do not want the loop to iterate if the condition is false from the beginning.
prefix increment
This operator increments the value of its operand, then uses the value in context.
ofstream
To write data to a file, you define an object of this data type.
False
True/False: The scope of a variable declared in a for loop's initialization expression always extends beyond the body of the loop.
True
True/False: The update expression of a for loop can contain more than one statement, e.g. counter++, total+= sales.
False
True/False: You may not use the break and continue statements within the same set of nested loops.
Files: What is Needed
Use fstream header file for file access ifstream for input from a file ofstream for output to a file fstream for input from or output to a file
0 1 2 3 4
What will the following loop display? int x = 0; while (x < 5) { cout << x << endl; x++; }
Loop
a control structure that causes a statement or statements to repeat
nested loop
a loop inside the body of another loop Inner loop goes through all repetitions for each repetition of outer loop Inner loop repetitions complete sooner than outer loop Total number of repetitions for inner loop is product of number of repetitions of the two loops.
The do-while Loop
a posttest loop - execute the loop, then test the expression General Format: do statement; while (expression); Note that a semicolon is required after (expression) this loop will execute one time because do-while is a posttest loop.
Counters
a variable that is incremented or decremented each time a loop repeats Can be used to control execution of the loop (also known as the loop control variable) Must be initialized before entering loop
running total
accumulated sum of numbers from each repetition of loop
Input validation
the process of inspecting data that is given to the program as input and determining whether it is valid. The while loop can be used to create input routines that reject invalid data, and repeat until valid data is entered.
sentinel
value in a list of values that indicates end of data Special value that cannot be confused with a valid value, e.g., -999 for a test score Used to terminate input when user may not know how many values will be entered
accumulator
variable that holds running total
The while Loop
while (expression) statement; expression is evaluated if true, then statement is executed, and expression is evaluated again if false, then the loop is finished and program statements following statement execute