Does any language have a while-else flow structure?
- by dotancohen
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.