152 points
*

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.

permalink
report
reply
64 points

Years ago when this meme first came my way I tried to explain this and nobody agreed with me, it was driving me mad.

permalink
report
parent
reply
36 points
do { explain } while ( !they.understand )
permalink
report
parent
reply
54 points
do {
    say("I use Arch btw");
} while ( they.interested );

This loop never runs twice.

permalink
report
parent
reply
7 points

Ruby has a fun until for refuted conditionals!

until(they.understand) { explain }

Reads like English 🙃

permalink
report
parent
reply
8 points

Yeah I ran through the logic in my mind and got confused, came to the comments to ask what I was missing

permalink
report
parent
reply
4 points

Yep, replace edge with isOnGround or something

permalink
report
parent
reply
8 points

Actually, doesn’t change the matter.

permalink
report
parent
reply
4 points

Yep, needs to check if the next step has ground, otherwise you’re already falling.

permalink
report
parent
reply
38 points

When productivity is measured in lines of code:

do {
  if (!edge) {
    run();
  }
} while (!edge);
permalink
report
reply
12 points

When productivity is measured in lines of code:

do {
  if (!edge) {
    run();
  }
} while {
  (!edge);
  }
permalink
report
parent
reply
4 points

You can probably do this:

do 
{
  if (!edge) 
  {
    run();
  }
} 
while 
{
  (!edge);
  }
permalink
report
parent
reply
5 points

You’re going to need to try harder than that - you don’t even have any doc block comments.

permalink
report
parent
reply

This is why computers should still have a Turbo button that limits the CPU to 8MHz.

permalink
report
reply
6 points

It’s still ironic to me that the turbo button’s purpose was to limit the CPU speed.

You know, like you use a turbo on a car to limit it’s performance.

permalink
report
parent
reply
5 points

Naming things is one of the two hard problems in computer science and the turbo button was the button you press if your computer is going too fast… it made sense from someone’s point of view.

permalink
report
parent
reply
4 points

Brands starting doing this in the 486 days to match customer expectation. In the 286 and 386 days it slowed the PC down to 8MHz. It was called turbo because it was 8088 Turbo mode, as the 8088 nominally operated at 4.7MHz.

Source: Those of around since the 8088 days viciously mocked the brands that did this on newsgroups because they were catering to newbs… or whatever we called them back then.

permalink
report
parent
reply
3 points
*

After they flipped the meaning. But even then, if the PC didn’t have the button it would run at full speed.

Turning it off would limit the speed. So the purpose of the existence of the button was not to make the PC go faster, but rather to make it go slower if you turned it off.

It’s kinda like as if the eco mode button in you car was labelled “turbo mode” with flipped meaning.

permalink
report
parent
reply
6 points

My dad’s computer had that and I loved pushing it :3

permalink
report
parent
reply
15 points
*

Why do you have the white bars on either side of the image?

permalink
report
reply
17 points

Fixed.

permalink
report
parent
reply
14 points

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.

permalink
report
reply
8 points

Why is it more dangerous to check before the loop?

permalink
report
parent
reply
6 points

Perhaps he’s saying relying on the break condition that’s inside the loop.

permalink
report
parent
reply
4 points

It’s not - it just feel more dangerous somehow.

permalink
report
parent
reply
6 points

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

permalink
report
parent
reply
5 points

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.

permalink
report
parent
reply

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.

permalink
report
parent
reply
2 points
*

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.

permalink
report
parent
reply
1 point

Generators are the tits

permalink
report
parent
reply
1 point

The day I learned many of the built-ins accept generator expressions I was a very happy boy

permalink
report
parent
reply

Programmer Humor

!programmerhumor@lemmy.ml

Create post

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.

Community stats

  • 3.9K

    Monthly active users

  • 1.5K

    Posts

  • 35K

    Comments