Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

What will be the output of the following C# code?
static void Main(string[] args)
{
int i = 10 , j = 0;
label:
i–;
if ( i > 0)
{
Console.WriteLine(i+ ” “);
goto label;
}
Console.ReadLine();
}

What will be the output of the following C# code?
static void Main(string[] args)
{
int i = 10 , j = 0;
label:
i–;
if ( i > 0)
{
Console.WriteLine(i+ ” “);
goto label;
}
Console.ReadLine();
}
a) 1 2 3 4 5 6 7 8 9 10
b) 10 9 8 7 6 5 4 3 2 1 0
c) 9 8 7 6 5 4 3 2 1
d) 10 9 8 7 6 5 4 3 2 1

Answer: c
Explanation: for i = 10, loop executes for first time in ‘if’ loop as (i>0) i.e (9 > 0) and hence printing ‘9’. Similarly, label condition executes again go for (i–) i.e (9-1=8) and hence again prints i = 8. In this way looping condition executes as 9, 8 to 3, 2, 1.
Output : 9 8 7 6 5 4 3 2 1

Join The Discussion