coping without do-while
Feb. 19th, 2010 09:48 pmYou'd like to write code like:
but your language does not offer do-while (a.k.a do-loop). What do you do?
(a)
(b)
Other?
do {
A
} while(test)but your language does not offer do-while (a.k.a do-loop). What do you do?
(a)
A
while(test){
A
}
(b)
while(TRUE){
A
if (!test) break
}
Other?
(no subject)
Date: 2010-02-20 05:53 am (UTC)(no subject)
Date: 2010-02-20 05:58 am (UTC)(no subject)
Date: 2010-02-20 06:12 am (UTC)(no subject)
Date: 2010-02-20 06:17 am (UTC)(no subject)
Date: 2010-02-20 06:23 am (UTC)(no subject)
Date: 2010-02-20 06:37 am (UTC)(no subject)
Date: 2010-02-20 06:45 am (UTC)A naive onlooker might say that code running after the loop is evidence that you just refuted TRUE.
(no subject)
Date: 2010-02-20 07:05 am (UTC)loop { # do stuff break if ...; # do other stuff break if ...; }It's an infinite loop if you don't break out of the loop somehow, so in practice you use it whenever you want to do multiple tests, multiple exits, or exits in odd places. It doesn't do anything different from what while(TRUE) does, but it's a clear syntactic marker that you're doing things in this way.I do wish you could do
as well as
but loop does ok.
(no subject)
Date: 2010-02-20 07:14 am (UTC)(no subject)
Date: 2010-02-20 09:33 am (UTC)(no subject)
Date: 2010-02-20 11:59 am (UTC)until(test)is equivalent towhile(!test).(no subject)
Date: 2010-02-20 02:32 pm (UTC)until ...
end
(a while loop with negated test) but not
do ...
until
(a while loop with negated test that unconditionally runs once before the test).
(no subject)
Date: 2010-02-20 03:33 pm (UTC)(no subject)
Date: 2010-02-20 03:36 pm (UTC)(no subject)
Date: 2010-02-20 05:41 pm (UTC)(no subject)
Date: 2010-02-20 07:13 pm (UTC)(no subject)
Date: 2010-02-20 09:10 pm (UTC)(no subject)
Date: 2010-02-20 09:45 pm (UTC)while (x = next_element()) {
printf(x);
} delimited-by {
printf(", ");
};
or something.
(no subject)
Date: 2010-02-20 09:47 pm (UTC)(no subject)
Date: 2010-02-20 10:00 pm (UTC)(no subject)
Date: 2010-02-24 04:27 pm (UTC)A
if(test)i++;
}
(no subject)
Date: 2010-02-24 05:24 pm (UTC)(no subject)
Date: 2010-02-24 11:23 pm (UTC)