Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

yg17

macrumors Pentium
Original poster
Aug 1, 2004
15,027
3,002
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
 

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
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.
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
3
yg17 said:
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

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.