Real World Applications? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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