← Back to context

Comment by scotty79

10 months ago

When we are on subject of loops... I'd love to have 'else' block for loops that runs when the loop had zero iterations.

Not the same thing (although I thought it was), according to the Python docs, but related:

https://docs.python.org/3/reference/compound_stmts.html

See sections 8.3, the for statement, and 8.2, the while statement.

  • Yeah, `while...else` in Python does the wrong thing. Executes `else` block when the loop finished normally (not through `break`).

    Scala for example has a `breakable {}` block that lets you indicate where you should land after a `break`

        breakable {
          while condition {
            // loop body
            if otherCondition then break;
            // rest of the body
          }
          // body of pythonic else block
        } // you land here if you break
    

    However I have no idea how to implement the kind of `else` I described in any language without checking the condition twice.