C.S. 150 Chapter 5 (Control Structures II (Repetition))
For looping
A control statement for specifying iteration, which allows code to be executed repeatedly.
Infinite loop
A loop that continues to execute endlessly
EOF (End of File) controlled while loop
A while loop that continues to execute until the end-of-file marker is reached.
Suppose sum and numare intvariables, and the input is 1 5 6 3 -1. What is the output of the following code? sum = 0; cin>> num; while (num!= -1) { sum = sum + num; cin>> num; } cout<< sum << endl; A. 15 B. 14
A. 15
In ____ structures, the computer repeats particular statements a certain number of times depending on some condition(s). A. Looping B. Branching C. Selection D. sequence
A. Looping
What is the initial statement in the following for loop? int i; for (i = 1; i < 20; i++) cout << "Hello World"; cout << "!" << endl; A. i= 1; B. i< 20; C. i++; D. cout << "Hello World";
A. i = 1;
Which executes first in a do...while loop? A. the statement B. loop condition C. the expression D. update statement
A. the statement
sentinel-controlled while loop
Awhile loop that uses a sentinel value to end the loop.
Suppose j, sum, and num are int variables, and the input is 6 4 1 4 -1. What is the output of the code? sum = 0; cin >> num; for (int j = 1; j <= 3; j++) { sum = sum + num; cin >> num; } cout << sum << endl; A. 10 B. 11 C. 14 D. 15
B. 11
To generate a random number, you can use the function rand of the header file ____________________. A. fstream B. cstdlib C. cmath D. string
B. cstdlib
Which of the following loops is guaranteed to execute at least once? A. forloop B. do...while loop C. while loop
B. do...while loop
____ loops are called posttest loops. A. for loop B. do...while loop C. while loop
B. do...while loop
cin >> ch; while (cin) { cout << ch; cin >> ch; } This code is an example of a(n) ____ while loop. A. sentinel-controlled B. flag-controlled C. EOF-controlled D. counter-controlled
C. EOF-controlled
A(n) ____-controlled while loop uses a bool variable to control the loop. A. Counter B. Sentinel C. Flag D. eof
C. Flag
What is the output of the following C++ code? num = 10; while (num > 10) num = num - 2; cout << num << endl; A. 0 B. 6 C. 8 D. 10
D. 10
What is the output of the following C++ code? num = 10; while (num> 10) num= num-2; cout<< num<< endl; A. 0 B. 6 C. 7 D. 10
D. 10
What is the output of the following loop? count = 5; cout << 'St'; do { cout << 'o'; count--; } while (count <= 5) A. St B. Sto C. Stop D. This is an infinite loop.
D. This is an infinite loop
Which of the following is a repetition structure in C++? A. if B. switch C. while ... do D. do ... while
D. do...while
do-while loop
Executes as long as the condition is true. To avoid an infinite loop, body must contain a statement that makes the expression false
The following for loop executes 5 times. for (i= 0; i<=5; i++) cout<< i; True or False?
False
The statement in the body of a while loop acts as a decision maker, True or False?
False
break
To exit early from a loop • Can eliminate the use of certain (flag) variables. To skip the remainder of a switch structure
In a counter-controlled while loop, the loop control variable must be initialized before the loop. True or False?
True
In the case of the sentinel-controlled while loop, the first item is read before the while loop is entered. True or False?
True
The control statements in the for loop include the initial statement, loop condition, and update statement. True or False?
True
The following while loop terminates when j >= 20. j = 0; while (j < 20) j++; True or False?
True
The following while loop terminates when j >=20. j = 0; while (j < 20) { cout<< "Hi" << endl; j++; } True or False?
True
The number of iterations of a counter controlled loop is known in advance. True or False?
True
The output of the following C++ code is 2 3 4 5. n = 1; while (n < 5) { n++; cout<< n << " "; } True or False?
True
continue
Used in while, for, and do...while structures. It skips remaining statements and proceeds with the next iteration of the loop.
Nested control structure
Using a control structure within a control structure
Flag-controlled while loop
uses a Boolean variable to control the execution of the loop