In: Computer Science
int count = 10;
while (count >= 0)
{
cout << count << endl;
count = count + 3;
}
How many times will this loop be executed?
Endless loop
3 times
0 times
Once
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;
}
}