Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualBluefirehawk

Posted 30 November 2012 - 08:14 AM

The code I am presenting is not doing something usefull at all to be honest but imagine a difficult calculation going on over a grid in which if a certain condition is met you need to break out of both loops and continue the rest of the algorithm with the results already calucalted?

I would say wrong semantic. Since you loop as long as a condition is met, you should need a while loop. Breaking out of a for loop is a strong indicator for that.

It is a bit nitpicky, but from a theoretical computer science stand point, for loop with a break statement is wrong. A for loop is intended to repeat the internal statement for a known amount. Therefore a for loop can be expanded by repeating the line of code by x amount of time.

A while loop is very different, each while loop has a variance and invariance which you can use to proof your loop aborts, and with the invariance you can proof that your loop calculates what it should.
</detail>

I am sure there is a freak case where a goto statement makes more sense and is more readable than any other solution, But I have yet to see a good example for it.
You can live a happy and productive life writing clean code without ever using a goto statement.

#2Bluefirehawk

Posted 30 November 2012 - 08:14 AM

The code I am presenting is not doing something usefull at all to be honest but imagine a difficult calculation going on over a grid in which if a certain condition is met you need to break out of both loops and continue the rest of the algorithm with the results already calucalted?

I would say wrong semantic. Since you loop as long as a condition is met, you should need a while loop. Breaking out of a for loop is a strong indicator for that.

It is a bit nitpicky, but from a theoretical computer science stand point, for loop with a break statement is wrong. A for loop is intended to repeat the internal statement for a known amount. Therefore a for loop can be expanded by repeating the line of code by x amount of time.

A while loop is very different, each while loop has a variance and invariance which you can use to proof your loop aborts, and with the invariance you can proof that your loop calculates what it should.
</detail>

I am sure there is a freak case where a goto statement makes more sense and is more readable than any other solution, But I have yet to see a good example for it.
You can live a happy and productive life writing clean code without ever using a goto statement.

#1Bluefirehawk

Posted 30 November 2012 - 08:14 AM

The code I am presenting is not doing something usefull at all to be honest but imagine a difficult calculation going on over a grid in which if a certain condition is met you need to break out of both loops and continue the rest of the algorithm with the results already calucalted?

I would say wrong semantic. Since you loop as long as a condition is met, you should need a while loop. Breaking out of a for loop is a strong indicator that you really need a while loop.

It is a bit nitpicky, but from a theoretical computer science stand point, for loop with a break statement is wrong. A for loop is intended to repeat the internal statement for a known amount. Therefore a for loop can be expanded by repeating the line of code by x amount of time.

A while loop is very different, each while loop has a variance and invariance which you can use to proof your loop aborts, and with the invariance you can proof that your loop calculates what it should.
</detail>

I am sure there is a freak case where a goto statement makes more sense and is more readable than any other solution, But I have yet to see a good example for it.
You can live a happy and productive life writing clean code without ever using a goto statement.

PARTNERS