From: John Slimick on
On 2010-04-22, Ben Bacarisse <ben.usenet(a)bsb.me.uk> wrote:
> Chad <cdalten(a)gmail.com> writes:
>
>> On Apr 22, 3:00 pm, j...(a)toerring.de (Jens Thoms Toerring) wrote:
>>> Chad <cdal...(a)gmail.com> wrote:
>>> > Which one of the following case is preferred to use do-while loop
>>> > rather than while loop.
>>> > Case 1: when the while loop is enclosed by an 'if' statement
>>> > Case 2: when a while loop is nested inside another while loop
>>> > Case 3: when there is a duplicate 'if' statement before and after the
>>> > while loop
>>> > Case 4: when there is a duplicated block of code in front and the top
>>> > of the while loop
>>> > Case 5:when there is an 'if' statement embedded at the top of a for-
>>> > loop
>>>

Once upon a time there was a language FORTRAN II/IV whose semantics
stated that even if the parameters of a DO loop were such that
the exit condition was already met, one went through the loop
anyway (the "once through" rule). All of the ALGOL based languages
did not have the guaranteed "once through" rule. Klaus Wirth
said that the presence of constructs
like "do while" and "repeat until" were for the FORTRAN programmers
who depended on the "once through" phenomenon in a new ALGOL-based
language.

To answer your question, try thinking like a FORTRAN programmer.

john slimick
slimick(a)pitt.edu
From: Nick Keighley on
On 23 Apr, 00:50, Ben Bacarisse <ben.use...(a)bsb.me.uk> wrote:

> Ah!  Given that all the answers are wrong, we need to find the least
> wrong.  

I once answered an exam question with "The question you meant to ask
is...". I passed.
From: Chad on
On Apr 23, 1:12 am, Nick Keighley <nick_keighley_nos...(a)hotmail.com>
wrote:
> On 23 Apr, 00:50, Ben Bacarisse <ben.use...(a)bsb.me.uk> wrote:
>
> > Ah!  Given that all the answers are wrong, we need to find the least
> > wrong.  
>
> I once answered an exam question with "The question you meant to ask
> is...". I passed.

<off topic>
I really don't understand the American Education System. San Francisco
State University, a school that will pretty much accept anyone with a
pulse, rejects me. But UC Berkeley, which is one of the hardest
schools to get into, offers me conditional admissions.
</off topic>
From: Jens Thoms Toerring on
Chad <cdalten(a)gmail.com> wrote:
> Maybe this is just some kind of broader programming misconception on
> my part here, but I somehow thought that having
> something like

> if(do_something) {
> if(do_something_else) {
> }
> }

> while(do_stuff) {

> if(do_something) {
> if(do_something_else) {
> }
> }
> }

> Could only be re-written as a do/while loop. Ie, you couldn't shove
> the if's inside the while loop itself.

If the code in the inner if clauses is identical you can use
a do loop

do {
if ( do_something ) {
if( do_something_else ) {
}
}
} while ( do_stuff );

avoiding unnecessary code duplication (and, hopefully) make
the logic easier to understand).
Regards, Jens
--
\ Jens Thoms Toerring ___ jt(a)toerring.de
\__________________________ http://toerring.de
From: Daniel Pitts on
On 4/22/2010 2:34 PM, Chad wrote:
> Which one of the following case is preferred to use do-while loop
> rather than while loop.
>
> Case 1: when the while loop is enclosed by an 'if' statement
> Case 2: when a while loop is nested inside another while loop
> Case 3: when there is a duplicate 'if' statement before and after the
> while loop
> Case 4: when there is a duplicated block of code in front and the top
> of the while loop
> Case 5:when there is an 'if' statement embedded at the top of a for-
> loop
>
>
> My first initial guess was case 3. Ideas?
The best answer I can think of:
Consider what the code would look like if you used for, do/while, while,
and/or recursion.

Choose the idiom that reduces code complexity unless it add unreasonable
run-time cost.

Reducing duplicate conditionals, as well as reducing duplicate
body-statements are both ways to reduce complexity.

Using recursion over iteration might simplify code, but cause stack
overflow depending on the use case.

Encapsulating the conditional and/or the body of the loop in a function
or method call may reduce overall complexity, especially in cases where
both while and do/while end up with duplication.

Having read somewhat further down the thread, I see this was a question
posed by a teacher. It was a bad question. At the level of expertise
you need to answer it correctly, you wouldn't have to be asked about it
in such a manor.



--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>