+ 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!
4 RĂ©ponses
+ 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
+ 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.
+ 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?
+ 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! :)