C# Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - General Questions (Q.No. 5)
5.
Which of the following is the correct way to rewrite the following C#.NET code snippet given below?
int i = 0; 
do
{
    Console.WriteLine(i);
    i+ = 1; 
} while (i <= 10);
int i = 0; 
do
{
    Console.WriteLine(i);
} until (i <= 10);
int i;
for (i = 0; i <= 10 ; i++)
    Console.WriteLine(i);
int i = 0; 
while (i <= 11)
{
    Console.WriteLine(i);
    i += 1; 
}
int i = 0;
do while ( i <= 10)
{
    Console.WriteLine(i); 
    i += 1;
}
int i = 0;
do until (i <= 10)
{
    Console.WriteLine(i);
    i+=1; 
}
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
9 comments Page 1 of 1.

Nikhil said:   1 decade ago
For loop has no opening and closing braces here. Then its syntax is wrong here.

Vinay said:   1 decade ago
There is no need of braces for a single line of code.

Vikram said:   1 decade ago
Without brackets single line of code will execute. Please tell me.

Inathi Gqola said:   1 decade ago
Yes, the syntax is totally correct. The practice is wrong though. So unreadable.

Pankaj Chauhan said:   10 years ago
Option C should be correct answer. Because do while will print 11. Hence C is the correct answer.

Pankaj Chauhan said:   10 years ago
Option C is correct answer. In above scenario do while loop will print 11. So option C should be the correct answer.

Ragni said:   10 years ago
But what is wrong in the original code? or option (A)?

Sunshine said:   9 years ago
@Pankaj Chauhan, Why do you think that do while will print 11? It won't because 11 is not less or equal to 10. It will evaluate false and exit the loop, and 11 won't be printed.

Madusha Rathnayake said:   6 years ago
Option B is correct obviously.

But in the syntax instead of being i+ = 1; it should be i += 1; . On the other hand , it will not print 11 because the increment code is given after the output code. If the code inside "{ }" changed their order then 11 would print.

Post your comments here:

Your comments will be displayed after verification.