Computer Programming: Chapter 5
These are operators that add and subtract one from their operands.
++ and --
What will the following code display? int number = 6 int x = 0; x = --number; cout << x << endl;
5
What will the following code display? int number = 6; ++number; cout << number << endl;
7
The scope of a variable declared in a for loop's initialization expression always extends beyond the body of the loop.
False
You may nest while and do-while loops, but you may not nest for loops.
False
You may not use the break and continue statements within the same set of nested loops.
False
An output file is a file that data is written to.
True
The update expression of a for loop can contain more than one statement, e.g. counter++, total+= sales.
True
This statement may be used to stop a loop's current iteration and begin the next one.
continue
What will the following loop display? int x = 0; while (x < 5) { cout << x << endl; x++; }
0 1 2 3 4
How many times will the following loop display "Hello"? for (int i = 0; i < 20; i++) cout << "Hello!" << endl;
20
A while loop is somewhat limited, because the counter can only be incremented by one each time through the loop.
False
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:
at least once
If you want a user to enter exactly 20 values, which loop would be the best to use?
for
In a for statement, this expression is executed only once.
initialization
This is a control structure that causes a statement or group of statements to repeat.
loop
To write data to a file, you define an object of this data type.
ofstream
A file must be ________ before data can be written to or read from it.
opened
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?
outFile << number;
When the increment operator precedes its operand, as in ++num1, the expression is in this mode.
prefix
This operator increments the value of its operand, then uses the value in context.
prefix increment
This is a special value that marks the end of a list of values.
sentinel
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