-8

i am reading C-Sharp 7.0 in a Nutshell - Joseph Albahari, in jump statements section, there is a code like

for (int i = 0; i < 10; i++)
{
if ((i % 2) == 0) // If i is even,
continue; // continue with next iteration
Console.Write (i + " ");
}

Output is : 1 3 5 7 9

but when i comment the continue jump statement like below

for (int i = 0; i < 10; i++)
    {
    if ((i % 2) == 0) // If i is even,
    //continue; // continue with next iteration
    Console.Write (i + " ");
    }

Output is : 0 2 4 6 8

can someone explain how continue statement affects flow of loop ?

washaq
  • 21
  • 2
  • 8
  • 2
    Delete the comment and re-indent. It will get magically revealed. – Eugene Sh. Jan 24 '18 at 19:12
  • 1
    Your code is now `if ((i % 2) == 0) Console.Write(i + " ");` – zzxyz Jan 24 '18 at 19:12
  • 10
    This looks like a great opportunity to step through the code in a debugger and *observe* what's happening. When you do that, what happens? What did you expect to happen? Why? – David Jan 24 '18 at 19:12
  • please correct me if i am wrong, first code says when number is even continue to write odd number !!, is it correct? – washaq Jan 24 '18 at 19:15
  • @washaq: The first code says "If the number is even, skip it. Else (if it's odd), write it." The second code says "If the number is even, write it. Else (if it's odd), do nothing." What other output were you expecting? *Why?* – David Jan 24 '18 at 19:16
  • A C# program is unchanged by replacing comments with spaces. Take both your before and after program and replace all the comments with spaces. Do you understand that these are now two different programs? – Eric Lippert Jan 24 '18 at 19:16
  • @David, i am new at programming i am still learning, i was not expecting anything just trying to understand. – washaq Jan 24 '18 at 19:17
  • 3
    The **[step debugger](https://msdn.microsoft.com/en-us/library/y740d9d3.aspx)** solves many mysteries, answers many questions and can pre-answer many SO posts. Google can answer more – Ňɏssa Pøngjǣrdenlarp Jan 24 '18 at 19:19
  • I think you might have the expectation that a line of code is a statement. That is simply false. C# treats line breaks, spaces, comments, and so on, as the same -- just empty space. (With some exceptions, like line breaks in a verbatim string.) The body of the `if` is the statement which follows it; by commenting out the `continue;` you made the next line into the body of the `if`. – Eric Lippert Jan 24 '18 at 19:19
  • 3
    @washaq: *"i was not expecting anything"* - Then I guess the question has been answered. Code was written, output was observed. Nothing is different than what would be expected, so there's no problem to solve. If you're just asking what the `continue` statement does, there is documentation which covers that. You also have a perfectly reasonable example (your first code) which demonstrates it. – David Jan 24 '18 at 19:21
  • https://stackoverflow.com/questions/6414/c-sharp-loop-break-vs-continue?rq=1 – Daniel A. White Jan 24 '18 at 19:22
  • @David yes, it is understood thanks. – washaq Jan 24 '18 at 19:23
  • I'm voting to close this question as off-topic because there appears to be no problem to be solved. Either way, the question is unclear. – EJoshuaS - Reinstate Monica Jan 24 '18 at 20:30
  • For those clamouring to downvote, look at his/her rep and remember we were all beginners at some stage. – HockeyJ Feb 08 '18 at 14:44

4 Answers4

2

Without the curly brackets, C# will compile the next statement to be executed when the condition of the if is true. A comment is not a statement. This is one of the main reasons it's best to always include curly brackets, even with a single statement.

continue tells the program to jump to the start of the loop and retest the condition. In this case the Console.Write calls will be skipped.

There is also break which ends the loop completely and does not retest the condition.

halfer
  • 18,701
  • 13
  • 79
  • 158
Daniel A. White
  • 174,715
  • 42
  • 343
  • 413
  • sorry i did not understand, i am not asking about how comments change the statement, i am asking how continue jump statement affects it. – washaq Jan 24 '18 at 19:13
  • You deleted the line which was executed conditionally. The next line became the one under that condition. – Eugene Sh. Jan 24 '18 at 19:15
  • so, continue says that if number is even continue to write odd number?? – washaq Jan 24 '18 at 19:16
  • `continue` just jumps back to the start of the loop. any logic after it will be skipped. – Daniel A. White Jan 24 '18 at 19:17
  • @washaq: No, `continue` simply says "stop executing this iteration of the loop and begin the next iteration." `continue` itself has nothing to do with even/odd numbers. Your confusion is coming from your `if` block, not from `continue`. – David Jan 24 '18 at 19:17
  • https://stackoverflow.com/questions/6414/c-sharp-loop-break-vs-continue?rq=1 – Daniel A. White Jan 24 '18 at 19:21
  • @DanielA.White thank you, everything is clear now. – washaq Jan 24 '18 at 19:23
2

Your fundamental confusion I think has to do with the fact that commenting out a statement removes the statement entirely. It does not make a "do nothing" statement that is then the body of the if.

However, I think there is also a confusion expressed about what continue does. A good way to understand this is to reduce it to something simpler.

Suppose you have

for (int i = 0; i < 10; i++)
{
  if ((i % 2) == 0)
    continue;
  Console.Write (i + " ");
}

Let's reduce this to a simpler program, in the sense that for is complicated and while is less complicated. Your program fragment is the same as:

{
  int i = 0;
  while (i < 10)
  {
    if ((i % 2) == 0)
      goto DoTheLoopIncrement;
    Console.Write (i + " ");
    DoTheLoopIncrement: 
    ++i;
  }
}

Which is the same as:

{
  int i = 0;
  DoTheLoopTest:
  if (i < 10)
    goto DoTheLoopBody;
  else
    goto DoneTheLoop;
  DoTheLoopBody:
  {
    if ((i % 2) == 0)
      goto DoTheLoopIncrement;
    Console.Write (i + " ");
    DoTheLoopIncrement: 
    ++i;
    goto DoTheLoopTest;
  }
}
DoneTheLoop:
...

Notice how much longer and harder to read the "goto" version is. That's why we use while and for. But you must understand that this is precisely what while and for and continue are doing in order to make sense of their control flows. They are just a pleasant way of writing a goto.

Now: do you understand what break means? Break is just a pleasant way to write goto DoneTheLoop.

Eric Lippert
  • 612,321
  • 166
  • 1,175
  • 2,033
  • my question is looked like i am confused with comment thing, actually not, i know what comment signs do, i was trying to understand how continue statement affects the flow of loop, now i understood it, and thank you for helping me understand goto and break statements, and it is nice to get a reply from the someone who is mentioned in the book by Arrays of array article on Microsoft's Blog. – washaq Jan 24 '18 at 20:00
  • @washaq I'd say it's even nicer to get a reply about c# by one of the guys who designed it :-) – Jcl Jan 25 '18 at 19:56
  • @Jcl voaw i did not know about it, that is realy nice. İ thought it was a stupid question because people made me think like that, but Eric Lippert answered very kindly and understandable. – washaq Jan 25 '18 at 20:08
  • 1
    @washaq I don't think there are "wrong questions"... the downvoting in stack overflow questions generally means "not enough researched question on your own" or "no previous effort to answer the question yourself"... but one can't learn without making questions. It's a matter of making them to yourself first and try to answer them by yourself (and if you can't, then ask others)... that's called "research" :-) . This used to be hard some time ago, but there's a ton of information these days available at your fingertips – Jcl Jan 25 '18 at 20:12
  • @Jcl i do not care about downvoting but people here act like why are bothering us with this kind of stupid questions, did you asked google? !!, anyway thank you for encouragment and nice tips. – washaq Jan 25 '18 at 20:21
  • @washaq there are over 8 million users in Stack Overflow. Don't get discouraged by just a few :-) – Jcl Jan 25 '18 at 20:22
1

Poor formatting:

for (int i = 0; i < 10; i++)
{
if ((i % 2) == 0) // If i is even,
continue; // continue with next iteration
Console.Write (i + " ");
}

Better formatting:

// Output is : 1 3 5 7 9 
for (int i = 0; i < 10; i++)
{
   if ((i % 2) == 0) // If i is even,
      continue; // continue with next iteration
   Console.Write (i + " ");
}

Without "continue":

// Output is : 0 2 4 6 8
for (int i = 0; i < 10; i++)
{
   if ((i % 2) == 0) // If i is even,
     //   continue; // continue with next iteration
     Console.Write (i + " ");
}

Best formatting:

// Output is : 1 3 5 7 9 
for (int i = 0; i < 10; i++)
{
   // If i is even,
   if ((i % 2) == 0) 
   {
      // continue with next iteration
      continue; 
   }
   Console.Write (i + " ");
}
paulsm4
  • 99,714
  • 15
  • 125
  • 160
  • 3
    Yep. I think the proper formatting and indentation are underestimated and omitted in most of programming textbooks, while I would place it as a basic skill to be learnt as a prerequisite for the next chapters. – Eugene Sh. Jan 24 '18 at 19:22
  • 1
    @EugeneSh.: I've sometimes heard that all programmers should learn Python if for no other reason than to understand that whitespace is important. (Even if it's not important to the compiler, it's important to the code.) – David Jan 24 '18 at 19:23
  • @Eugene Sh: thank you, I agree. Morever, I suspect these "formatted examples" are probably going to help the OP understand the underlying problem better than a "wordy explanation". – paulsm4 Jan 24 '18 at 19:23
  • i will keep that in mind, and also will warn the author. – washaq Jan 24 '18 at 19:24
  • 1
    Might be worth mentioning you can ask VS to format the code for you with Edit->Advanced->Format Document. (Usually but not always Ctrl-K, Ctrl-D). In C#, this is nearly always an accurate indication of how the code will be interpreted by the compiler. – zzxyz Jan 24 '18 at 19:27
0

Continue will ignore all the statements that follow inside the loop and continue with the next iteration.

Rene MF
  • 157
  • 2
  • 11