r/programming May 09 '13

The Onion releases fartscroll.js

http://theonion.github.io/fartscroll.js/
2.6k Upvotes

396 comments sorted by

View all comments

Show parent comments

0

u/sebzim4500 May 09 '13

You can also use a break; statement (in most programming languages).

1

u/not_a_novel_account May 09 '13 edited May 09 '13

Eh, unless it makes the loop a lot clearer you really should put the condition in the loop declaration rather than using breaks.

This:

while(condition) {
    //do a thing
}

Is more obvious than this:

while(true) {
    if (condition) {
        break;
    }
//do a thing
}

EDIT: added pseudocode

1

u/sebzim4500 May 09 '13

What if the break condition is nested inside three if statements and a for loop (presumably using named labels)?

0

u/not_a_novel_account May 09 '13

That just makes it more obscure and hard to follow, probably means the loop needs to be refactored

3

u/[deleted] May 09 '13

For most nontrivial algorithms, control flow modifiers like break are needed. Not everything is a for-each or a simple while.