In: Computer Science
How many times will an "X" appear in the following nested
loop?
for (outer = 0; outer <=5; outer++)
{for (inner = 1; inner <=4; inner+1)
{cout<<("X");}
}
9 |
|
10 |
|
20 |
|
24 |
|
none of the above |
answer option D
we can see that in the post condition of the inner loop it is mentioned just inner+1
so it is like
inner +1 ;
so the value of inner is always 1 and leads to infinite loop
#include <bits/stdc++.h>
using namespace std;
int main()
{
int outer, inner;
for (outer = 0; outer <= 5; outer++)
{
for (inner = 1; inner <= 4; inner + 1)
{
cout << "The value of inner is " << inner << endl;
cout << ("X");
}
}
return 0;
}