C++ Chapter 5 Loop Test
__________ are C++ operators that change their operands by one.
++ and --
What is the output of the following code? #include <iostream> using namespace std; int main() { int n = 0; for (n = 1; n >= 10; n++) { cout << n << " "; } cout << n << " is done. "; }
1 is done
Type the exact output of the following code. X is declared as an int. x = 5; while (x >= 3) { x--; cout << x << " "; }
4 3 2
What will the following code print? num is declared as an int. num = 8; cout << --num << " "; cout << num++ << " "; cout << num;
7 7 8
The while loop has two important parts; a condition that is tested and a statement or block of statements that is executed
A. as long as the condition is true.
In order for a C++ program to read data from a file
All of the above are required
The -- operator
All of the above are true
The above code will display: int n, j; for (n = 1; n <= 20; n = n + 2) { for (j = 1; j <= 20 - n; j = j + 2) cout << " "; for (j = 1; j <= n; j++) cout << "G"; cout << endl; }
An isosceles triangle of G's with the longest side on the bottom
To use the files in a C++ program you must include the _______ header file.
C. fstream
The ++ operator
D. All of the above
The _______ statement causes a loop to terminate early.
D. break
To use an output file in a C++ program you must
E. do B and C but not A.
A while loop is somewhat limited because the counter can only be incremented or decremented by one each time through the loop
False
A while loop is somewhat limited, because the counter can only count up, not down
False
A while loop may have a semicolon after the test expression
False
In a do while loop, the {}'s are optional
False
To decrement a number means to increase its value
False
When a loop is nested inside another loop, the outer loop goes through all its iterations for each iteration of the inner loop.
False
You can nest a for loop inside another for loop, but cannot nest a while loop inside another while loop or a do-while loop inside another do-while loop. (T/F)
False
An initialization expression may be omitted from the for loop if no initialization is required
True
Assuming x has been declared, the following could be used as a valid for loop for (; x < 10; x++)
True
The >> operator returns true or false
True
The block of code in the body of a while statement can contain an unlimited number of statements provided they are enclosed in a set of braces
True
When a loop is nested inside another loop, the inner loop goes through all its iterations for each iteration of the outer loop.
True
A variable that keeps a running total of data values is called a(n)
accumlator
The statements in the body of a do while loop are executed
at least once
What function is used that allows the user to enter their own file name when reading or writing data?
c_str()
A(n) _____ is a variable that is regularly incremented or decremented each time a loop iterates
counter
The ideal type of loop to use for displaying a menu is a(n) ______ loop.
do-while
The ideal type of loop to use if you want a user to enter exactly 20 values is a(n) ______ loop.
for
To ________ a value means to increase it
increment
If nothing within a while loop ever causes the condition to become false, a(n) ________ may occur.
infinite loop
In a for loop statement, the ______ expression is executed only once.
initialization
A(n) _____ is a variable that controls the number of times a loop iterates
loop control variable
A sentinel is a special value that
marks the end of a list of values
The statements in the body of a while loop are executed
only if the test condition is initially true.
The do-while loop is a(n) ______ loop, whereas the For loop is a(n) ______ loop.
post test, pretest
A for loop is considered a(n) _______ loop.
pretest
The while loop is a(n) _____ loop, where as the do while loop is a(n) _____ loop
pretest, post test
The while loop has two important parts: a condition that is tested and a statement or block of statements that is
repeated as long as the condition is true
A(n) ______ is a special value that marks the end of a list of values
sentinel
If a while loop has no braces around the body of the loop
the loop body contains just one statement.
A for (loop) statement contains three expressions; initialization, test, and
update
You may define a ________ in the initialization expression of a for loop.
variable
For data validation, it is best to use a(n)
while loop
Write the equivalent code to make the following more concise: while(ans == 'Y' || ans == 'y') where the condition only tests for a capital letter.
while(toupper(ans) == 'Y')