Programming-Idioms

History of Idiom 78 > diff from v31 to v32

Edit summary for version 32 :

Version 31

2015-10-18, 13:12:51

Version 32

2015-10-29, 14:05:16

Idiom #78 "do while" loop

Execute a block once, then execute it again as long as boolean condition c is true.

Idiom #78 "do while" loop

Execute a block once, then execute it again as long as boolean condition c is true.

Code
doowhile c b = do a <- b; if c a
                          then doowhile c b
                          else return a

doowhile (=="") getLine
Code
doowhile c b = do a <- b; if c a
                          then doowhile c b
                          else return a

doowhile (=="") getLine
Code
loop {
    doStuff();
    if !c { break; }
}
Code
loop {
    doStuff();
    if !c { break; }
}
Comments bubble
Rust has no do-while loop with syntax sugar. Use loop and break.
Comments bubble
Rust has no do-while loop with syntax sugar. Use loop and break.
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20let%20mut%20i%20%3D%205%3B%0A%09loop%20%7B%0A%09%09println!(%22%7B%7D%22%2C%20i)%3B%0A%0A%09%09if%20i%20%25%202%20%3D%3D%200%20%7B%20i%20%2F%3D%202%3B%20%7D%0A%09%09else%20%7B%20i%20%3D%203*i%20%2B%201%3B%20%7D%0A%09%09%0A%09%09if%20i%20%3D%3D%201%20%7B%20break%3B%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20println!(%22And%20final%20value%3A%20%7B%7D%22%2C%20i)%3B%0A%7D%0A&version=stable
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20let%20mut%20i%20%3D%205%3B%0A%09loop%20%7B%0A%09%09println!(%22%7B%7D%22%2C%20i)%3B%0A%0A%09%09if%20i%20%25%202%20%3D%3D%200%20%7B%20i%20%2F%3D%202%3B%20%7D%0A%09%09else%20%7B%20i%20%3D%203*i%20%2B%201%3B%20%7D%0A%09%09%0A%09%09if%20i%20%3D%3D%201%20%7B%20break%3B%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20println!(%22And%20final%20value%3A%20%7B%7D%22%2C%20i)%3B%0A%7D%0A&version=stable
Code
do {
	someThing();
	someOtherThing();
} while(c);
Code
do {
	someThing();
	someOtherThing();
} while(c);
Comments bubble
The block code is not repeated in the source.
Comments bubble
The block code is not repeated in the source.
Demo URL
https://ideone.com/oRSbiv
Demo URL
https://ideone.com/oRSbiv