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!
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
+ 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! :)