C# Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - General Questions (Q.No. 1)
1.
What does the following C#.NET code snippet will print?
int i = 0, j = 0; 

label:
    i++;
    j+=i;
if (i < 10)
{
    Console.Write(i +" ");
    goto label; 
}
Prints 1 to 9
Prints 0 to 8
Prints 2 to 8
Prints 2 to 9
Compile error at label:.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
14 comments Page 1 of 2.

Ankita said:   6 years ago
It's a loop. If the condition gets true, it will go further until it gets false.

Sivaleela said:   7 years ago
It's not a while loop, it is just an if loop then how may it will repeat the loop. I think it will print only 1.

Output = 1

Pavithra said:   8 years ago
J+=i says j=i+1
J+i=i

Both are same @Koti.

Koti said:   9 years ago
J+=i?
J+i=i

Is it right or worng?

Please tell me.

Shaharukh said:   9 years ago
I think lable1 is considered as case and that's why it will give output.

Suvarna said:   9 years ago
j+=i; means,

j=j+i;
(1)

Ankush said:   10 years ago
Sir can you tell me how += operator works?

Rohan said:   1 decade ago
Why did we use j here?

Ashish said:   1 decade ago
Code must be as:

int i = 0, j = 0;

label:
i++;
j += i;
if (i < 10)
{
Console.WriteLine(i + " ");

goto label;

}
Console.ReadLine();

Then output will be 1 to 9.

Raveendra Bj said:   1 decade ago
The o/p is 123456789,

Why? because everytime i++ increment the i value.

And goto statement sends control.

There is nothing evil in this code.

Thanks!


Post your comments here:

Your comments will be displayed after verification.