r4cppp icon indicating copy to clipboard operation
r4cppp copied to clipboard

Do while loops

Open dempo93 opened this issue 5 years ago • 1 comments

I think it is worth mentioning that do..while loops can be achieved by prepending the body of the loop to the condition

   let x=1;

   // while loop
   while {x == 0}{
        println!("This is not executed");
   } 

   // do while loop
   while {
        println!("This is executed");
        x == 0
   }{} 

dempo93 avatar May 01 '20 17:05 dempo93

while {
    println!("This is executed");
    x == 0
}{} 

Devil's advocate here -

This form might be misleading to beginners (such as myself) since it's not really a do-while, it's explicitly a while with an empty body and a rather complex condition. You can't break or continue in it, for example, and at first glance, the syntax kind of obscures what's actually going on.

So I would understand leaving this out so as to not encourage that kind of hack—or perhaps include it, but in the context of "This is technically possible but probably not good form."

ThirteenPeeps avatar May 29 '20 04:05 ThirteenPeeps