Java loop question

Discussion in 'Mac Programming' started by yg17, Sep 14, 2005.

  1. yg17 macrumors G5


    Aug 1, 2004
    St. Louis, MO
    I've got a true/false question on my Java class homework:

    for loops and while loops use the same stopping mechanism.

    At first, I think its true because you can almost always write a for loop as a while loop and vise versa, on the other hand, it's false because the syntax for stopping is different. Anyone wish to shed some light on this? Thanks
  2. HiRez macrumors 603


    Jan 6, 2004
    Western US
    That's a tricky question but I'd probably say "true". They both evaluate an expression at the top of the loop and execute the body if it evaluates to non-zero. The only difference is in initializing the loop control variable and the for statement keeps all the loop control information together. Both can be stopped manually with a break statement.
  3. VanMac macrumors 6502a


    May 26, 2005
    Rampaging Tokyo
  4. mrichmon macrumors 6502a

    Jun 17, 2003
    Depends on what you mean by a "stopping mechanism". Personally I wouldn't regard the loop conditional as the stopping mechanism since if the loop conditional evaluates to "true" then the loop continues.

    However there are other language features in Java that do halt execution of a loop. Consider what the "break" and "continue" keywords do... in many ways the side effect of these keywords are more related to being a "stopping mechanism" in my experience.

    The question as posted is not clearly the expressing concepts it is referring to. By "stopping" do they mean "normal loop completion", or "loop termination", in the context of exceptions handling this fuzzy terminology gets even more complicated. However, my bet would be that they are referring to loop termination.
  5. cbiffle macrumors member

    Jun 19, 2005
    Tempe, AZ
    Let your teacher know that this professional Java OLTP developer says that's a crappy question. :)

Share This Page