C# Programming - Control Instructions - Discussion
Discussion Forum : Control Instructions - General Questions (Q.No. 19)
19.
What will be the output of the code snippet given below?
int i;
for(i = 0; i<=10; i++)
{
if(i == 4)
{
Console.Write(i + " "); continue;
}
else if (i != 4)
Console.Write(i + " "); else
break;
}
Discussion:
6 comments Page 1 of 1.
Sona said:
7 months ago
The for loop initializes i to 0 and continues to run until i is greater than 10, incrementing i after each iteration.
Inside the loop:
* If i is equal to 4, it prints 4 and then executes continue, which skips to the next iteration of the loop.
If i is not equal to 4, it prints i. The second else with a break will never execute because the first if condition (i == 4) will always be true when i is 4.
0 1 2 3 4 5 6 7 8 9 10 is the answer.
Inside the loop:
* If i is equal to 4, it prints 4 and then executes continue, which skips to the next iteration of the loop.
If i is not equal to 4, it prints i. The second else with a break will never execute because the first if condition (i == 4) will always be true when i is 4.
0 1 2 3 4 5 6 7 8 9 10 is the answer.
Naresh said:
1 decade ago
Nothing special in this, when i not equal to 4, lower if will execute and later after printing 4 in upper if again it will execute lower if. Hence 0 ot 10 will print.
Zubair Khan said:
9 years ago
When i! = 0 then it will print 0, 1, 3.
When its true it prints 4.
Again if false then prints 5, 6, 7, 8, 9, 10.
When its true it prints 4.
Again if false then prints 5, 6, 7, 8, 9, 10.
Stephanie said:
8 years ago
Why use the break?
Explain clearly.
Explain clearly.
Div said:
8 years ago
For exit the loop @Stephanie.
Charu said:
1 decade ago
Explain this solution.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers