Respuesta :
Answer:
Option(c) is the correct answer for the given question.
Explanation:
In the given code initially the variable i is initialized by 0 .The for will iterated and behave as outer loop which hold another loop i.e while loop which behaves as inner loop .
So every iteration of i in for loop the whole while loop is executed .
means
for(int i = 0; i <= 20; i++ ) // outer loop
while ( i < 20 ) // inner loop
{
Console.WriteLine ( i ); // display the value of i inside while loop
i++;
}
Console.WriteLine ( i ); //display the value of i
}
initially i=0 in for loop so 0<=20 condition is true
So it executed the statement inside the for loop .We see that there is another loop inside the for loop i.e while loop .
0<20 in while loop condition is true so it executed the statement inside the while loop so it print 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19
on 20 the condition of while loop is false and while loop is terminated .After that next statement is executed i.e Console.WriteLine ( i ); so 20 is printed value of i is incremented in for loop so i becomes 21 and condition is false loop becomes terminated.
Following are the program in c#
using System;
class HelloWorld {
static void Main() {
for(int i = 0; i <= 20; i++ ) // outer loop
{
while ( i < 20 ) // inner loop
{
Console.WriteLine ( i ); // display the value of i inside while loop
i++;
}
Console.WriteLine( i ); //display the value of i
}
}
Output:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
We see that the scope of control variable is different from both the segment with different output.
So Option(c) is the correct answer.