Does any language have a while-else flow structure?
Posted
by
dotancohen
on Stack Overflow
See other posts from Stack Overflow
or by dotancohen
Published on 2012-08-30T05:35:37Z
Indexed on
2012/10/02
9:38 UTC
Read the original article
Hit count: 254
coding-style
Consider this flow structure which I happen to use often:
if ( hasPosts() ) {
while ( hasPosts() ) {
displayNextPost();
}
} else {
displayNoPostsContent();
}
Are there any programming languages which have an optional else clause for while, which is to be run if the while loop is never entered? Thus, the code above would become:
while ( hasPosts() ) {
displayNextPost();
} else {
displayNoPostsContent();
}
I find it interesting that many languages have the do-while construct (run the while code once before checking the condition) yet I have never seen while-else addressed. There is precedent for running an N block of code based on what was run in N-1 block, such as the try-catch construct.
I wasn't sure whether to post here or on programmers.SE. If this question is more appropriate there, then please move it. Thanks.
© Stack Overflow or respective owner