Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

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

Search: