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);
Discussion:
9 comments Page 1 of 1.
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.
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.
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.
Ragni said:
10 years ago
But what is wrong in the original code? or option (A)?
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.
Pankaj Chauhan said:
10 years ago
Option C should be correct answer. Because do while will print 11. Hence C is the correct answer.
Inathi Gqola said:
1 decade ago
Yes, the syntax is totally correct. The practice is wrong though. So unreadable.
Vikram said:
1 decade ago
Without brackets single line of code will execute. Please tell me.
Vinay said:
1 decade ago
There is no need of braces for a single line of code.
Nikhil said:
1 decade ago
For loop has no opening and closing braces here. Then its syntax is wrong here.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers