From: Chad on
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?
From: Jens Thoms Toerring on
Chad <cdalten(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

You use a do loop if you want the body of the loop to be executed
at least once, a while loop (or for loop) otherwise. So if you
find yourself writing

if ( some_condition )
do {
<loop body>
} while ( some_condiion );

then you should have used a while loop instead. But if you
find that you wrote

<loop body>
while ( some_condtion ) {
<loop body>
}

you should have used a do loop. That seems to fit your case 4
(modulo the "top of the while loop" bit perhaps, it should be
"when there's a block of code directly before the while loop that
is identical to the code making up the body of that while loop").

But I don't understand most of your cases, perhaps the descrip-
tions are a bit to vague...

> My first initial guess was case 3. Ideas?

Why do you think so? Could you post an example of what's
meant there?
Regards, Jens
--
\ Jens Thoms Toerring ___ jt(a)toerring.de
\__________________________ http://toerring.de
From: Chad on
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
>
> You use a do loop if you want the body of the loop to be executed
> at least once, a while loop (or for loop) otherwise. So if you
> find yourself writing
>
> if ( some_condition )
>     do {
>         <loop body>
>     } while ( some_condiion );
>
> then you should have used a while loop instead. But if you
> find that you wrote
>
> <loop body>
> while ( some_condtion ) {
>     <loop body>
>
> }
>
> you should have used a do loop. That seems to fit your case 4
> (modulo the "top of the while loop" bit perhaps, it should be
> "when there's a block of code directly before the while loop that
> is identical to the code making up the body of that while loop").
>
> But I don't understand most of your cases, perhaps the descrip-
> tions are a bit to vague...
>
> > My first initial guess was case 3. Ideas?
>
> Why do you think so? Could you post an example of what's
> meant there?

I thought about it. I think a do/while loop would be preferable if I
had something like the following

if(some_condition) {
if (some_other_condition)
}

while(I_dont_what_should_go_here) {
if(some_condition) {
if (some_other_condition)
}
}

Maybe it would be case 4? I don't know. Someone I know emailed me this
question. When I asked them for clarification, they just told me it
was the question the teacher had asked them.
From: Jens Thoms Toerring on
Chad <cdalten(a)gmail.com> wrote:
> 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
> >
> > You use a do loop if you want the body of the loop to be executed
> > at least once, a while loop (or for loop) otherwise. So if you
> > find yourself writing
> >
> > if ( some_condition )
> >     do {
> >         <loop body>
> >     } while ( some_condiion );
> >
> > then you should have used a while loop instead. But if you
> > find that you wrote
> >
> > <loop body>
> > while ( some_condtion ) {
> >     <loop body>
> >
> > }
> >
> > you should have used a do loop. That seems to fit your case 4
> > (modulo the "top of the while loop" bit perhaps, it should be
> > "when there's a block of code directly before the while loop that
> > is identical to the code making up the body of that while loop").
> >
> > But I don't understand most of your cases, perhaps the descrip-
> > tions are a bit to vague...
> >
> > > My first initial guess was case 3. Ideas?
> >
> > Why do you think so? Could you post an example of what's
> > meant there?

> I thought about it. I think a do/while loop would be preferable if I
> had something like the following

> if(some_condition) {
> if (some_other_condition)
> }

> while(I_dont_what_should_go_here) {
> if(some_condition) {
> if (some_other_condition)
> }
> }

Mmm, this doesn't really help my understanding. How would you
write that with a do loop? And what about the 'if' before and
after the loop mentioned for case 3? I only see nested 'if's
within the while loop here...

> Maybe it would be case 4? I don't know. Someone I know emailed me this
> question. When I asked them for clarification, they just told me it
> was the question the teacher had asked them.

Unfortunately, some teachers pose stupid/unanswerable questions
(perhaps when they don't know enough about what they're supposed
to teach;-). But then there are also students that don't manage
to even restate the questions they were asked in any intelligible
way... As far as I can see the "question" can't be answered with
the information given, as stated it's much too vague. Or just take

> > > Which one of the following case is preferred to use do-while loop
> > > rather than while loop.
....
> > > Case 5:when there is an 'if' statement embedded at the top of a for-
> > > loop

How did we suddenly got a for loop into the mix when the question
is about do versus while loops? The whole things simply doesn't
seem to make much sense.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt(a)toerring.de
\__________________________ http://toerring.de
From: Ben Bacarisse on
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
>>
>> You use a do loop if you want the body of the loop to be executed
>> at least once, a while loop (or for loop) otherwise. So if you
>> find yourself writing
>>
>> if ( some_condition )
>>     do {
>>         <loop body>
>>     } while ( some_condiion );
>>
>> then you should have used a while loop instead. But if you
>> find that you wrote
>>
>> <loop body>
>> while ( some_condtion ) {
>>     <loop body>
>>
>> }
>>
>> you should have used a do loop. That seems to fit your case 4
>> (modulo the "top of the while loop" bit perhaps, it should be
>> "when there's a block of code directly before the while loop that
>> is identical to the code making up the body of that while loop").
>>
>> But I don't understand most of your cases, perhaps the descrip-
>> tions are a bit to vague...
>>
>> > My first initial guess was case 3. Ideas?
>>
>> Why do you think so? Could you post an example of what's
>> meant there?
>
> I thought about it. I think a do/while loop would be preferable if I
> had something like the following
>
> if(some_condition) {
> if (some_other_condition)
> }
>
> while(I_dont_what_should_go_here) {
> if(some_condition) {
> if (some_other_condition)
> }
> }

I can't see any connection between this is a do ... while loop.

> Maybe it would be case 4? I don't know. Someone I know emailed me this
> question. When I asked them for clarification, they just told me it
> was the question the teacher had asked them.

Ah! Given that all the answers are wrong, we need to find the least
wrong. (4) looks like a good option. The "duplicated block of code" is
probably meant to be "a duplicate of the while loop body". I.e.:

S
while (C) S

which is, indeed, the same as

do S while (C);

In the same way, a case can be made for (1) if we take the wording to be
very loose. If the test in the 'if' and the 'while' are the same then
we do get something like the do ... while above:

if (C)
while (C) S

but this is much further from the truth since side-effects in C can mess
up the equivalence.

I'd go for (4) if I really, really, had to choose one.

--
Ben.