Question

In: Computer Science

int count = 10; while (count >= 0) { cout << count << endl; count =...

int count = 10;
while (count >= 0)
{
cout << count << endl;
count = count + 3;
}

How many times will this loop be executed?

  • A.

    Endless loop

  • B.

    3 times

  • C.

    0 times

  • D.

    Once

Solutions

Expert Solution

Answer:

A.Endless Loop

The loop will execute infinitely. It is an endless loop.

Reason:


    the loop will iterate until the the value of count becomes negative
   once the value of count becomes negative the loop ends.
    but here instead of decrementing count we are incrementing count
   so, the value of count will never become
   negative.So, the loop will never end.

This Explanation is given in the comments along with the code for better understanding:

The output of the code:

For reference addding the code here:

#include <iostream>
using namespace std;
int main(){
        //here we declared and initilaized an
        //integer a with 10
        int count = 10;
        /*the loop will iterate until the
        the value of count becomes negative
        once the value of count becomes negative the
        loop ends*/
        while (count >= 0){
                //printing count
        cout << count << endl;
        /*but here instead of decrementing count 
        we are incrementing count
        so, the value of count will never become 
        negative.So, the loop will never end*/
        count = count + 3;
        }
}

Related Solutions

unsigned u =10; int i = -42; cout << i+i <<endl; cout << u+i<<endl;//if int take...
unsigned u =10; int i = -42; cout << i+i <<endl; cout << u+i<<endl;//if int take 32 bit, output 4294967264 I know when unsigned and singed operate together, they need to be converted, but why is the answer 4294967264? What does it have to do with int .…… 32 bit
Translate c++ code into mips assembly: int main() {                 cout << "Numbers:" << endl;            &nbs
Translate c++ code into mips assembly: int main() {                 cout << "Numbers:" << endl;                                 int x[] = {18, 12, 6, 500, 54, 3, 2, 122};                 int i;                                 for (i=0; i<8; i++)                 {                                                 cout << x[i] << endl;                 }                 return 0; } below is my code: .data        str1: .ascii "Numbers:"     str2: .ascii "\n"    x: .word 18,12,6,500,54,3,2,122       .text                      ...
int value = 1; do { if (value % 2 == 0) cout << value <<...
int value = 1; do { if (value % 2 == 0) cout << value << " "; value = value + 1; } while (value % 7 != 0); cout << "\n" << value; What will be displayed?
***Convert the C++ to Python*** #include <iostream> using std::cout; using std::cin; using std::endl; int charClass; char...
***Convert the C++ to Python*** #include <iostream> using std::cout; using std::cin; using std::endl; int charClass; char lexeme[100]; char str[200]; char nextChar; const int LETTER = 0; const int DIGIT   = 1; const int UNKNOWN = -1; const int OPAREN = 2; const int CPAREN = 3; const int PLUS = 4; const int MINUS = 5; const int MUL = 6; const int DIV = 7; const int ID_CODE = 100; const int PLUS_CODE = 101; const int MINUS_CODE =...
c++ programming What will the following C++ code display? int numbers[4]={99,87}; cout<<numbers[3]<<endl; 87                     &nbs
c++ programming What will the following C++ code display? int numbers[4]={99,87}; cout<<numbers[3]<<endl; 87                            b. 0                                  c. garbage                        d. an error What is the output of the following program segment? double mylist[5]; for(int i=0; i<5; i++) mylist[i]= (pow(i,2)+ 1 /2.0; cout<<fixed<<showpoint<<setprecision(2); for(int i=0; i<5; i++) { cout<<setw(5)<<mylist[i]; }               a. .5           1.5        4.5        9.5        16.5               b. 0.50      1.50      4.50      9.50     16.50              c. 0.50      1.50      8.50     27.50     64.50             d. 0.50      1.50      2.50      3.50      4.50      15. what is stored...
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return...
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return count;   } } class Worker extends Thread{ Counter count;   Worker(Counter count){     this.count = count;   }   public void run(){     for (int i = 0; i < 1000;i++){       synchronized(this){         count.inc();       }}   } } public class Test {     public static void main(String args[]) throws InterruptedException   {     Counter c = new Counter();     Worker w1 = new Worker(c);     Worker w2 = new Worker(c);     w1.start();     w2.start();     w1.join();     w2.join();     System.out.println(c.get());      ...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i <n; ++ i) {cout << "j =" + j; j = j + 5; }}
Show the output of the following code segment. int count=0;                         for (int i=2; i <=...
Show the output of the following code segment. int count=0;                         for (int i=2; i <= 4; i++ ) {                                     StdOut.println(i);                                     for (int j=1; j <3; j++) {                                                 count++;                                                 StdOut.println(i +" " + j +" "+ count);                                     }                         } count i j I print 0 2 1 3 1 1 1 2 3 2 2 3 3 3 3 4 3 Show the output of the function call statements shown.             double x =...
Consider the following code segment:    count = 1    while count <= 10:       print(count,...
Consider the following code segment:    count = 1    while count <= 10:       print(count, end=" ") Which of the following describes the error in this code? The loop control variable is not properly initialized. The comparison points the wrong way. The loop is infinite. The loop is off by 1. Does this code contain an error? If so, what line is the error on? 0: ans = input("Yes/No? ") 1: if ans == "Yes": 2: print("Confirmed!") 3: else...
(C++)Change the following loop to a while loop: int i; for (i=0; i<10; i++) {    ...
(C++)Change the following loop to a while loop: int i; for (i=0; i<10; i++) {     cout<<i<<endl; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT