C++ Which of the following is a correct way to ensure that once we read a nonnegative value into x, the loop will stop? (1) int x; while(x < 0){ cin >> x; if(x < 0) cout << “x must be >=0”; } (2) int x; for(cin >> x ; x < 0 ); cin >> x; cout << “x must be >=0”; (3) int x; do{ cin >> x; if(x < 0) cout << “x must be >=0”; } (4) int x; cin >> x; while(x < 0){ cin >> x; cout << “x must be >=0”; }   Choices: a. 2 and 4 b. 3 and 4 c. 2 and 3 d. 1 and 3

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter3: Assignment, Formatting, And Interactive Input
Section3.4: Program Input Using Cin
Problem 8E
icon
Related questions
Question

C++

Which of the following is a correct way to ensure that once we read a nonnegative value into x, the loop will stop?

(1)

int x;
while(x < 0){
cin >> x;
if(x < 0)
cout << “x must be >=0”;
}

(2)

int x;
for(cin >> x ; x < 0 );
cin >> x;
cout << “x must be >=0”;

(3)

int x;
do{
cin >> x;
if(x < 0)
cout << “x must be >=0”;
}

(4)

int x;
cin >> x;
while(x < 0){
cin >> x;
cout << “x must be >=0”;
}

 

Choices:

a. 2 and 4

b. 3 and 4

c. 2 and 3

d. 1 and 3

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Types of Loop
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning