This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Strange problem with loop

Hi.

Ive been programing for nearly five months now so I am not a noob but I cannot understand this;

for ( variable = 1 ; variable < 100 ; variable += 1 )
{
  // some code

  if ( foofar )
    continue ;
  else
    sumfunc () ;
}

<endpre>

if i continue does the variable get added?


  • The continue will jump to the closing brace of the for loop, so it will side-step any work you have inside the for body. But the expr3 (your increment) will be performed before expr2 is tested again to decide if a further iteration should be made.

    If you don't have the language standard (paragraph 6.8.6.2) you can test this yourself. Remember that you can use the free MinGW Win32 compiler to write test applications on the PC. As long as you take care of the size of variables, you can debug a lot of algorithms on the PC before moving the code to the 8051 target.

  • By the way: You did write "strange problem", but didn't seem to tell what you find strange. What you see, and what you expected to see.

  • Ive been programing for nearly five months now so I am not a noob but I cannot understand this

    In that case, you should really try to get your hands on a copy of K&R (*). It will answer questions like yours, and many more, very quickly and correctly, since the book was actually written by the people who created C in the first place.

    From said book, on the continue statement: "[...]; in the for, control passes to the increment step.". The answer to your question is therefore "Yes.".

    (*) B. W. Kernighan, D. M. Ritchie; "The C programming language"

  • "Ive been programing for nearly five months now so I am not a noob."

    I've been programming for more than 25 years, and I'm still a noob at some aspects of programming!

    It's always useful to keep a good reference manual besides you when you work. My favourite is "C: A Reference Manual" by Harbison and Steele.

    Just noticed that one of my colleagues has just nabbed mine!

  • It's always useful to keep a good reference manual besides you when you work. My favourite is "C: A Reference Manual" by Harbison and Steele.

    Do you guys really have to scare poor aspiring programmers with heavyweights like "the standard" and H&S, when a clear answer can be found in K&R? ;)

  • You have to buy the full language standard, but you can at least get a committee draft for free:
    www.open-std.org/.../n1124.pdf

    Here is links to buy the official releases:
    www.iso-9899.info/.../The_Standard

  • The standard isn't really so scary. You browse to the index at the back of the document, and look for "continue". Then you get sent to a section describing the continue, with code examples for "for", "while" and "do-while".

    The standard is only scary if you decide from the start that it must be scary. But since it is the ultimate reference for a compiler vendor, it is a must for a developer to have handy.

    It may not be a document you read from start to end when bored - or you may have to be bored to do it :) - but it is excellent to supply the answer if you have a doubt about something.