Hacker News new | past | comments | ask | show | jobs | submit login

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.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: