int j;
for (j = 0; j <= 10; j++)
{
if (j == 7)
{
Console.WriteLine("Found 7");
continue;
Console.WriteLine(j + "\n");
like this
int j;
for (j = 0; j <= 10; j++)
{
if (j == 7)
{
Console.WriteLine("Found 7");
continue;
it only prints 7
Thanks
2 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Nov 1, 2013, 5:09 PM
In your snippet. this line:
Console.WriteLine(j + "\n");
is unreachable because it is preceded by the continue statement which will always jump over it to the end of the for statement.
We can correct that and print all the numbers as follows:
using System;
class Program
{
static void Main()
{
int j;
for (j = 1; j <= 10; j++)
{
if (j == 7)
{
Console.WriteLine("Found 7" + "\n");
continue;
}
Console.WriteLine(j + "\n");
}
Console.ReadKey();
}
}
The offending line is now outside the if statement and so will print all the numbers except 7.
Tito OnowuPosted Nov 1, 2013, 5:44 PM