4.2 While loops

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

For the following code, indicate how many times the loop body will execute for the indicated input values. userNum = 3; while (userNum > 0) { // Do something // Get userNum from input } Input: -1 5 0

1

What will the following code output? For an infinite loop, type "IL" (without the quotes). x = 5; y = 18; while (y >= x) { cout << y << " "; y = y - x; }

18 13 8

#include <iostream> using namespace std; int main() { double celsiusValue; double fagrenheitValue; char userChar; celsiusValue = 0.0; userChar = 'y'; while (userChar == 'y'_ { fahrenheitValue = (celsiusValue * 9.0 / 5.0) + 32.0; cout << celsiusValue << " C is "; cout << fahrenheitValue << " F" << endl; cout << "Type y to continue, any other to quit: "; cin >> userChar; celsiusValue = celsiusValue + 5; cout << endl; } cout << "Goodbye." << endl; return 0; } Consider the example above. Each iteration adds ________________ to celsiusValue. a. 1 b. 5

5

What will the following code output? For an infinite loop, type "IL" (without the quotes). x = 0; while (x > 0) { cout << x << " "; x = x - 1; } cout << "Bye";

Bye

What will the following code output? For an infinite loop, type "IL" (without the quotes). (Assume the user always enters 'q'). z = 0; c = 'y'; while (c = 'y') { cout << z << " "; cin >> c; z = z + 1; }

IL

What will the following code output? For an infinite loop, type "IL" (without the quotes). x = 0; while (x <= 5) { cout << x << " "; }

IL

What will the following code output? For an infinite loop, type "IL" (without the quotes). x = 10; while (x != 3) { cout << x << " "; x = x / 2; }

IL

#include <iostream> using namespace std; int main() { const int YEARS_PER_GEN = 20; // Approx. years per generation int userYear; // User input int consYear; // Year being considered int numAnc; // Approx. ancestors in considered year consYear = 2020; numAnc = 2; cout << "Enter a past year (neg. for B.C.): "; cin >> userYear; while (consYear >= userYear) { cout << "Ancestors in " << consYear << ": " << numAnc << endl; numAnc = 2 + numAnc; // Each ancestor had two parents consYear = consYear - YEARS_PER_GEN; // Go back 1 generation } return 0; } Consider the example above. Each loop iteration outputs the current number of ancestors (numAnc), and then doubles numAnc in preparation for the next iteration. True or False.

true

#include <iostream> using namespace std; int main() { double celsiusValue; double fagrenheitValue; char userChar; celsiusValue = 0.0; userChar = 'y'; while (userChar == 'y'_ { fahrenheitValue = (celsiusValue * 9.0 / 5.0) + 32.0; cout << celsiusValue << " C is "; cout << fahrenheitValue << " F" << endl; cout << "Type y to continue, any other to quit: "; cin >> userChar; celsiusValue = celsiusValue + 5; cout << endl; } cout << "Goodbye." << endl; return 0; } Consider the example above. If the user's first input is 'n', how many times will the loop iterate? a. 1 b. 2

1

For the following code, indicate how many times the loop body will execute for the indicated input values. // Get userNum from input while (userNum > 0) { // Do something ... // Get userNum from input } Input: 5 -1

1

For the following code, indicate how many times the loop body will execute for the indicated input values. // Get userNum from input while (userNum > 0) { // Do something ... // Get userNum from input } Input: -1 5 4

0

For the following code, indicate how many times the loop body will execute for the indicated input values. // Get userNum from input while (userNum > 0) { // Do something ... // Get userNum from input } Input: 2 1 0

2

For the following code, indicate how many times the loop body will execute for the indicated input values. userNum = 3; while (userNum > 0) { // Do something // Get userNum from input } Input: 5 -1

2

Type the program's output. #include <iostream> using namespace std; int main() { int g; g = 2; while (g <= 7) { cout << g; g = g + 1; } return 0; }

234567

For the following code, indicate how many times the loop body will execute for the indicated input values. userNum = 3; while (userNum > 0) { // Do something // Get userNum from input } Input: 2 1 0

3

Type the program's output. #include <iostream> using namespace std; int main() { int g; g = 0; while (g >= -2) { cout << g; g = g - 1; } return 0; }

0-1-2

Type the program's output. #include <iostream> using namespace std; int main() { int g; g = 0; while (g >= -5) { cout << g; g = g - 2; } return 0; }

0-2-4

Type the program's output. #include <iostream> using namespace std; int main() { int g; g = 0; while (g <= 1) { cout << g; g = g + 1; } return 0; }

0123

Type the program's output. #include <iostream> using namespace std; int main() { int g; g = 0; while (g <= 5) { cout << g; g = g + 4; } return 0; }

04

#include <iostream> using namespace std; int main() { const int YEARS_PER_GEN = 20; // Approx. years per generation int userYear; // User input int consYear; // Year being considered int numAnc; // Approx. ancestors in considered year consYear = 2020; numAnc = 2; cout << "Enter a past year (neg. for B.C.): "; cin >> userYear; while (consYear >= userYear) { cout << "Ancestors in " << consYear << ": " << numAnc << endl; numAnc = 2 + numAnc; // Each ancestor had two parents consYear = consYear - YEARS_PER_GEN; // Go back 1 generation } return 0; } Consider the example above. The loop expression involves a relational operator. True or False.

True

Write a while loop that prints userNum divided by 2 (integer division) until reaching 1. Follow each number by a space. Example output for userNum = 40: 20 10 5 2 1 Note: These activities may test code with different test values. This activity will perform four tests, with userNum = 40, then with userNum = 2, then with userNum = 0, then with userNum = -1. See "How to Use zyBooks". . Also note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds, and report "Programend never reached." The system doesn't print the test case that caused the reported message. #include <iostream> using namespace std; int main() { int userNum; cin >> userNum; /* Your solution goes here */ cout << endl; return 0; }

while (userNum != 1 && userNum > 1) { userNum = userNum / 2; cout << userNum << " "; }

Use a single operator in each loop expression, and the most straightforward translation of the stated goal into an expression. while ( _____ ) { // Loop body } Iterate while x is less than 100.

x < 100

Use a single operator in each loop expression, and the most straightforward translation of the stated goal into an expression. while ( _____ ) { // Loop body } Iterate while x is greater than or equal to 0.

x >= 0

#include <iostream> using namespace std; int main() { double celsiusValue; double fagrenheitValue; char userChar; celsiusValue = 0.0; userChar = 'y'; while (userChar == 'y'_ { fahrenheitValue = (celsiusValue * 9.0 / 5.0) + 32.0; cout << celsiusValue << " C is "; cout << fahrenheitValue << " F" << endl; cout << "Type y to continue, any other to quit: "; cin >> userChar; celsiusValue = celsiusValue + 5; cout << endl; } cout << "Goodbye." << endl; return 0; } Consider the example above. For the user inputs shown, how many times did the loop iterate? a. 9 b. 10

a

Use a single operator in each loop expression, and the most straightforward translation of the stated goal into an expression. while ( _____ ) { // Loop body } Iterate while c equal 'g'.

c == 'g'

Use a single operator in each loop expression, and the most straightforward translation of the stated goal into an expression. while ( _____ ) { // Loop body } Iterate while c is not equal to 'x'.

c! = 'x'

Use a single operator in each loop expression, and the most straightforward translation of the stated goal into an expression. while ( _____ ) { // Loop body } Iterate until c equal 'z' (tricky; think carefully)

c! = 'z'

#include <iostream> using namespace std; int main() { const int YEARS_PER_GEN = 20; // Approx. years per generation int userYear; // User input int consYear; // Year being considered int numAnc; // Approx. ancestors in considered year consYear = 2020; numAnc = 2; cout << "Enter a past year (neg. for B.C.): "; cin >> userYear; while (consYear >= userYear) { cout << "Ancestors in " << consYear << ": " << numAnc << endl; numAnc = 2 + numAnc; // Each ancestor had two parents consYear = consYear - YEARS_PER_GEN; // Go back 1 generation } return 0; } Consider the example above. The user is asked to enter a value at the end of each loop iteration. True or False.

false

#include <iostream> using namespace std; int main() { const int YEARS_PER_GEN = 20; // Approx. years per generation int userYear; // User input int consYear; // Year being considered int numAnc; // Approx. ancestors in considered year consYear = 2020; numAnc = 2; cout << "Enter a past year (neg. for B.C.): "; cin >> userYear; while (consYear >= userYear) { cout << "Ancestors in " << consYear << ": " << numAnc << endl; numAnc = 2 + numAnc; // Each ancestor had two parents consYear = consYear - YEARS_PER_GEN; // Go back 1 generation } return 0; } Consider the example above. The loop updates the considered year consYear. True or False.

true

#include <iostream> using namespace std; int main() { double celsiusValue; double fagrenheitValue; char userChar; celsiusValue = 0.0; userChar = 'y'; while (userChar == 'y'_ { fahrenheitValue = (celsiusValue * 9.0 / 5.0) + 32.0; cout << celsiusValue << " C is "; cout << fahrenheitValue << " F" << endl; cout << "Type y to continue, any other to quit: "; cin >> userChar; celsiusValue = celsiusValue + 5; cout << endl; } cout << "Goodbye." << endl; return 0; } Consider the example above. The loop will always iterate at least once. True or False.

true

Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with userNum initially 9 and user input of 5, 2, -1, then with userNum initially 0 and user input of -17, then with userNum initially -1. See "How to Use zyBooks". . Also note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds, and report "Program end never reached." The system doesn't print the test case that caused the reported message. #include <iostream> using namespace std; int main() { int userNum; userNum = 9; while (/* Your solution goes here */) { cout << "Body" << endl; cin >> userNum; } cout << "Done." << endl; return 0; }

userNum >= 0


संबंधित स्टडी सेट्स

Chapter 1 - Assessing the Environment - Political, Economic, Legal, Technological

View Set

Domande stimolo conversazione test finale: with answers

View Set

Elements of Human Communication CHPT 1

View Set