COP2000 Chapter 5
These are operators that add and subtract one from their operands.
++ and --
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;
21
What will the following code display? int number = 6; ++number; cout << number << endl;
7
What will the following code display? int number = 6; cout << ++number << endl;
7
True/False: A while loop is somewhat limited, because the counter can only be incremented by one each time through the loop.
False
True/False: You may nest while and do-while loops, but you may not nest for loops.
False
This is a special value that marks the end of a list of values.
Sentinal
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 loop
True/False: You may not use the break and continue statements within the same set of nested loops.
false
Something within a while loop must eventually cause the condition to become false, or a(n) __________ results.
infinite loop
In a for statement, this expression is executed only once.
initialization expression
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
The do-while loop is considered a(n) _________ loop.
post-test
This operator increments the value of its operand, then uses the value in context.
prefix increment
When the increment operator precedes its operand, as in ++num1, the expression is in this mode.
prefix mode
The while loop is this type of loop.
pretest
True/False: An output file is a file that data is written to.
true
True/False: The update expression of a for loop can contain more than one statement, e.g. counter++, total+= sales.
true
How many times will the following loop display "Hello"? for (int i = 0; i < 20; i++) cout << "Hello!" << endl;
20
What will the following code display? int number = 6 int x = 0; x = --number; cout << x << endl;
5
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
This statement may be used to stop a loop's current iteration and begin the next one.
continue