+ 2

Real World Applications?

For some reason I’m still struggling to understand when/how these “do while” loops would be useful. Could someone give me a real world example of a time you’ve used (or could imagine using) these loops? Thank you!

22nd Mar 2018, 2:55 AM
Amanda
4 Respostas
+ 20
there are infinite ways in which U can use while loop //suppose U want to execute something multiple times , then instead if writing it many times ... its better to use a loop
22nd Mar 2018, 2:57 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 4
There really aren't too many real world applications, but an example would be to continuously try performing an operation until it succeeded. In this case, the "do" part tries the operation, and if it fails, it loops and tries again. Example: bool operationSuccessful = false; do { dom = http.request("example.com"); if (dom != null) { operationSuccessful = true; } } while (!operationSuccessful); Generally, I'd just use a regular while loop, even if it is slightly harder to implement.
22nd Mar 2018, 4:32 AM
LunarCoffee
LunarCoffee - avatar
+ 2
Right, I understand how “for loops” and “while loops” are useful, I’m just having a harder time with “do...while” loops. I suppose it’s when you need to check that something returns true, at least once?
22nd Mar 2018, 3:18 AM
Amanda
+ 1
Thanks LunarCoffee! That was actually really helpful! And I’m relieved to know I’m not crazy to think a while loop generally just makes more sense. I appreciate you taking the time to answer! :)
22nd Mar 2018, 5:42 AM
Amanda