programming chapter 5

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Select the code that repeatedly reads a value from console input into the character variable c until a 'Q' or 'q' has been entered.

char c; cin >> c; while (c != 'Q' && c != 'q') cin >> c;

What is the number of iterations in the following loop (i.e, how many times is the body of the loop executed?): for (int i = 1; i <= n; i++) { // iteration }

n times

What is the number of iterations in the following loop (i.e, how many times is the body of the loop executed?): for (int i = 1; i < n; i++) { // iteration }

n-1 times

A loop inside of another loop is called a _________ loop. infinite do-while break nested

nested

(T or F) A loop can be used to tell a program to execute statements repeatedly.

t

(T or F) Another common technique for controlling a loop is to designate a special value when reading and processing a set of values. This special input value, known as a sentinel value.

t

(T or F) If the variable is declared inside a loop control structure, it cannot be referenced outside the loop.

t

(T or F) In general, a for loop is commonly used when the number of repetitions is known in advance.

t

Which of the loop statements always has their body executed at least once.

The do-while loop

Rewrite the following loop into a while loop. int sum = 0; for (int i = 0; i <= 4; i++) sum = sum + i;

int sum = 0, i = 0; while (i <= 4) { sum = sum + i;i++; }

Take a look at the following code. What code should replace MISSING_CODE so that the program continuously reads data from the file, numbers.txt, until it reaches the end of file. ifstream myInputFile("numbers.txt"); double sum = 0;double number; while (MISSING_CODE) // Read data to the end of file { myInputFile >> number; // Read data cout << number << " "; // Display data sum += number; }

!myInputFile.eof()

What is the output of the following code? black image int r = 0, c = 0;

**** **** ****

What is the output of the following code? int count = 0; while (count < 5) { cout << count << " "; count++; }

0 1 2 3 4

How many times is the cout statement executed? for (i = 1; i <= 3;++i) for (j = 1; j <= 4; ++j) cout << "*"; Infinite 1 7 12

12

What will be displayed when the following code is executed? black notes int number = 6

3 0

What is the value in the variable count after the loop is finished? int count = 1; do { count += 1; } while (count <= 4); 5 1 4 3

5

How many times is the following loop body repeated (i.e., how many iterations)? int i = 1; while (i < 6) { if ( (i%2) == 0) cout << i << " even" << endl; else cout << i << " odd" << endl; ++i; }

5 times

What is the value stored in the variable sum when the loop ends: int sum = 0; int item ;for (item = 0; item < 5; ++item) { sum += item; if (sum > 4) break; }

6

How many times is the following loop body repeated (i.e., how many iterations)? #include <iostream> #include <string> #include <iomanip> using namespace std; int main() { int i = 1; while (i < 10) if (i % 2 == 0) cout << i << endl; }

Infinite, because i never changes

How many times is the following loop body repeated (i.e., how many iterations)? #include <iostream> #include <string> #include <iomanip> using namespace std; int main() { int i = 1; while (i < 10) if (i % 2 == 0) ++i; }

Infinite, because i never changes

(T or F) The while loop and for loop are called posttest loops because the continuation condition is checked after the loop body is executed. The do-while loop is called a pretest loop because the condition is checked before the loop body is executed.

f

Which of the following loop prints "Welcome to C++" 10 times? for (int count = 1; count < 10; count++) { cout << "Welcome to C++" << endl; } for (int count = 0; count <= 10; count++) { cout << "Welcome to C++" << endl; } for (int count = 1; count <= 10; count++) { cout << "Welcome to C++" << endl; }

for (int count = 1; count <= 10; count++) { cout << "Welcome to C++" << endl; }

Given an int variable counter that has already been declared, which of the following while loops will print a single line consisting of 10 asterisks.

int counter = 1; while (counter <= 10) { cout << "*"; ++counter; }

(T or F) The code shown below (in blue) shows an example of code using a while loop: char c;cin >> c;while (c != 'Q' && c != 'q') cin >> c; The code listed below (shown in black) shows the same code, rewritten using a do-while loop. char c;do {cin >> c;} while (c != 'Q' && c != 'q');

t

(T or F) ​​If there is only one statement in a loop body, the ​​braces can be omitted​.

t


Ensembles d'études connexes

Section 2.1 Properties Of Matter

View Set

Saunders NCLEX-PN1 exam 3 Fluid/electrolytes Acid/Base

View Set

Function Biology - Chapter 6 Homework

View Set