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 2 of 2.

Kazeem said:   1 decade ago
Whenever We are working with Labels, Label gets Executed Even if it is not called. so for the first time i value will be incremented to 1 and then control passes to if condition there it prints i value(1).

Then label is called from if body. Now again i value is incremented to 2. So it repeats until condition in if becomes false. So we get output As 1 to 9.

Vimala said:   1 decade ago
console.write will execute as it mean to produce output, when the code gets executed the i++ inside the label will be incremented ,that is why it's from 1 to 10

Sibi said:   1 decade ago
Here the output will be error.one main thing is the print statement itself is wrong.

Arun said:   1 decade ago
How does console.writeline statement is executed?


Post your comments here:

Your comments will be displayed after verification.