• snowraven@sh.itjust.works
    link
    fedilink
    arrow-up
    153
    arrow-down
    1
    ·
    edit-2
    1 year ago

    Purely in technical terms, this meme doesn't really fit unless they start running literally from the edge.

    While and do while are equal except for the very first test. So if the very first test does not evaluate to false, they are essentially same. In the meme that implies they started running sometime before reaching the edge, that runs the "run()" atleast once and later on for every run it would be checked and it would be false at the cliff edge.

  • hperrin@lemmy.world
    link
    fedilink
    arrow-up
    38
    ·
    1 year ago

    When productivity is measured in lines of code:

    do {
      if (!edge) {
        run();
      }
    } while (!edge);
    
  • AMillionMonkeys@lemmy.world
    link
    fedilink
    English
    arrow-up
    14
    ·
    1 year ago

    One of these days I'll read through the PEP and figure out why Python doesn't have do-while. I understand that it's just as bad, but while(True) feels so dangerous.

    • DreamButt@lemmy.world
      link
      fedilink
      English
      arrow-up
      6
      ·
      1 year ago

      Curious what use case you have for needing a do-while. Honestly I barely use while at all, a good ol for-loop normally does the trick

      • MrLuemasG@lemmy.world
        link
        fedilink
        arrow-up
        5
        ·
        1 year ago

        The use cases definitely do come up where you want the logic inside the loop to execute at least once. One common use case I have is validating user input in console applications. Put the instructions for validating the user's inputs inside a do while and then run logic to validate it at the end - that way you can easily loop back to the start and re-prompt them for the user input again.

      • IMO I find it really useful in scenarios where you have a set condition that needs to be met (and can always be met so it's not endless) and looping through to achieve it. I wrote a simple part calculation for a production company I worked for. Essentially while parts_needed < parts packed, pack qty 'x' of part 1, and qty 'x' of part 2 until all parts were filled to the correct amounts. Though it can be done with either / both I just find it more legible.

      • pixelscript@lemmy.ml
        link
        fedilink
        arrow-up
        2
        ·
        edit-2
        1 year ago

        I got an application that runs through a big calculation that has to pass several sanity checks along the way. If any check fails, the input parameters are tweaked and the calculation starts again from the top, iteratively approaching the ideal solution. Do-while is perfect for this.

        It beats recursion in non-tail-call-optimized implementations (JavaScript…). And while this could be done just as well with a comon while loop plus a flag variable, I like the way the syntax of do-while naturally reads as, "Do [thing]. … Did it work? No? Do it again".

        I'd still argue it's redundant. If they got rid of it tomorrow I'd refactor and cope with no complaints. But as long as it's around, I like using it.

    • glibg10b@lemmy.ml
      link
      fedilink
      arrow-up
      27
      arrow-down
      3
      ·
      1 year ago

      Well, it's wrong. The only difference between the two kinds of loops is that do-while starts by unconditionally running the body once

      • KTVX94@lemmy.myserv.one
        link
        fedilink
        English
        arrow-up
        2
        ·
        1 year ago

        Yes, that's true. It's also good enough to make an abstract concept more approachable while getting a kick out of it too. Honestly this is more of a "well akcshually" thing that someone who already knows how this works would point out (nobody has so far) than something that misleads students who don't get it. It works.

    • xmunk@sh.itjust.works
      link
      fedilink
      arrow-up
      11
      ·
      1 year ago

      Please switch over to something that actually has a condition that needs to logically be evaluated on each step and not as an end condition - yes this meme is technically correct for the case where they both start adjacent to the cliff and programmers should always consider (cliff) edge cases… but the visual strongly implies they've been running for a while.

      • KTVX94@lemmy.myserv.one
        link
        fedilink
        English
        arrow-up
        2
        ·
        1 year ago

        I mean yeah, it does have that problem, but even if it's not entirely accurate it's good enough to illustrate an otherwise abstract concept that's not exactly easy to understand for someone who's just getting started with programming and may not really be interested in it, rather looking to be an artist or game designer. Also humor really helps when it comes to learning. It does way more good than bad in my experience so far.