Why do Do-While Loops Exist?
why do them do-loops exist i dont want them anymore
It’s actually pretty simple~!
- Normal while-loops check the condition before ever running.
- Do-while loops check after running, meaning that the loop will run at least once.
I’ve never had to use it, myself. Here’s my attempt at an example:
So you’ve got a game. It’s got a timer; and once that timer hits zero, the game exits. Let’s say that something craps out, and you’re in the game before setting the timer: say this is a problem, because the game checks for player-connections and reports on ‘em. If the game just clocks out without doing the checks, everybody’ll be reported as rage-quitting! You wanna ensure that people are registered as properly connected, so a do-while loop would be preferred.
There are better ways around that particular problem, but hey. Do-while loops are a lot like exclusive-or, in that they’re real purpose is to just prove how clever you are.
Hope that all helps!