Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Sorry for the NOOB question..

Reply
Thread Tools

Sorry for the NOOB question..

 
 
Charlie Gordon
Guest
Posts: n/a
 
      09-14-2007
"Keith Thompson" <(E-Mail Removed)> a écrit dans le message de news:
http://www.velocityreviews.com/forums/(E-Mail Removed)...
> "Charlie Gordon" <(E-Mail Removed)> writes:
>> "Lafatus" <(E-Mail Removed)> a écrit dans le message de news:
>> (E-Mail Removed) om...
>>>I wasn't really planning on using a do/while, I wanted to use a while
>>> loop but reached that point where I was trying everything and was
>>> getting frustrated that I couldn't complete such a simple program.

>>
>> As a rule of thumb, do/while loops are more difficult master than regular
>> while or far loops.

>
> Let me guess, "far loops" are a DOS-specific extension that allows the
> code for the loop body to span more than one memory segment, right?


You too remember the Pharlap days

> (In case anyone is confused, that was a joke; "far loops" is a typo
> for "for loops".)


yes, many typos in this post (un loupé)

As a rule of thumb, do/while loops are more difficult to master than regular
while or for loops.

--
Chqrlie.


 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      09-14-2007
CBFalconer <(E-Mail Removed)> writes:
[...]
> You should also realize that google is not the messageing system,
> it is only a rather poor form of message reader, with which you can
> interface via http. It is so bad, and attracts so many Usenet
> ignorant newbies, that many readers simply ban all messages
> originating on google. So you would be well advised to get a
> proper Usenet message reader, such as Thunderbird.


That's a trifle overstated. It is possible to post properly to Usenet
using Google Groups. (But I think the folks who manage to do so are
mostly people who have used real newsreaders in the past.)

The previous poster erred in posting a followup with no context, but
that's not Google's fault; the misfeature that encouraged this was
corrected some time ago, and it's a mistake that can easily be made
with a real newsreader as well.

See <http://cfaj.freeshell.org/google/>.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      09-15-2007
Charlie Gordon wrote:
>

.... snip much banditry ...
>
> yes, many typos in this post (un loupé)
>
> As a rule of thumb, do/while loops are more difficult to master
> than regular while or for loops.


Why should you think that? "do { something() } while(foo);" is no
more complex than "while (foo) {something())". The only difference
is that the foo test is applied after execution in the do/while
loop, and before execution in the while loop.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-15-2007
Keith Thompson wrote:
> CBFalconer <(E-Mail Removed)> writes:
> [...]
>> You should also realize that google is not the messageing system,
>> it is only a rather poor form of message reader, with which you
>> can interface via http. It is so bad, and attracts so many Usenet
>> ignorant newbies, that many readers simply ban all messages
>> originating on google. So you would be well advised to get a
>> proper Usenet message reader, such as Thunderbird.

>
> That's a trifle overstated. It is possible to post properly to
> Usenet using Google Groups. (But I think the folks who manage to
> do so are mostly people who have used real newsreaders in the past.)


But finding out how to post properly through Google will not get
anyone past the traps set to eliminate all google posters. A
proper newsreader will.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      09-15-2007
"CBFalconer" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...
> Charlie Gordon wrote:
>>

> ... snip much banditry ...
>>
>> yes, many typos in this post (un loupé)
>>
>> As a rule of thumb, do/while loops are more difficult to master
>> than regular while or for loops.

>
> Why should you think that? "do { something() } while(foo);" is no
> more complex than "while (foo) {something())". The only difference
> is that the foo test is applied after execution in the do/while
> loop, and before execution in the while loop.


My own experience proofreading millions on lines of C code tells me that.
It is not a matter of complexity... it is just more error prone.
do/while loops written by average programmers tend to have more bugs than
regular for or while loops.
do/while loops produced by newbies tend to be always wrong.
A great classic is this:

int c;
/* skip to end of line */
do {
c = getc(fp);
} while (c != '\n');

Of course regular while loops can be bogus too:

while (!feof(fp)) {
fgets(buf, sizeof buf, fp);
/* do something or naught */
}

Please do the test, take any project at random to which you can access the
source code and examine the do/while loops, you will find bugs!

--
Chqrlie


 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-15-2007
"Charlie Gordon" <(E-Mail Removed)> writes:

> "CBFalconer" <(E-Mail Removed)> a écrit dans le message de news:
> (E-Mail Removed)...
>> Charlie Gordon wrote:
>>>

>> ... snip much banditry ...
>>>
>>> yes, many typos in this post (un loupé)
>>>
>>> As a rule of thumb, do/while loops are more difficult to master
>>> than regular while or for loops.

>>
>> Why should you think that? "do { something() } while(foo);" is no
>> more complex than "while (foo) {something())". The only difference
>> is that the foo test is applied after execution in the do/while
>> loop, and before execution in the while loop.

>
> My own experience proofreading millions on lines of C code tells me
> that.


....and my experience teaching programming leads to the same
conclusion. There seems to be something seductive about do ... while.
So much so that I just dropped the form from lecture notes as soon as
I saw the problem.

It may be that it reads naturally. It might also be that one of the
rare cases when it is correct:

do {
print prompt;
get reply;
} while (reply not suitable);

crops up in beginners programs more often than production code and the
idea sticks.

<snip>
> A great classic is this:
>
> int c;
> /* skip to end of line */
> do {
> c = getc(fp);
> } while (c != '\n');


Yes, but that is not wrong because of the do .. while. You probably
intended to leave out the declaration of c (or at least to add
.... suggesting more intervening code) so that this code comes after
some other input that might already have set c to '\n' (at least that
is the form of this bug I've seen repeatedly in student code).

--
Ben.
 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      09-15-2007
"Ben Bacarisse" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...
> "Charlie Gordon" <(E-Mail Removed)> writes:
>
>> "CBFalconer" <(E-Mail Removed)> a écrit dans le message de news:
>> (E-Mail Removed)...
>>> Charlie Gordon wrote:
>>>>
>>> ... snip much banditry ...
>>>>
>>>> yes, many typos in this post (un loupé)
>>>>
>>>> As a rule of thumb, do/while loops are more difficult to master
>>>> than regular while or for loops.
>>>
>>> Why should you think that? "do { something() } while(foo);" is no
>>> more complex than "while (foo) {something())". The only difference
>>> is that the foo test is applied after execution in the do/while
>>> loop, and before execution in the while loop.

>>
>> My own experience proofreading millions on lines of C code tells me
>> that.

>
> ...and my experience teaching programming leads to the same
> conclusion. There seems to be something seductive about do ... while.
> So much so that I just dropped the form from lecture notes as soon as
> I saw the problem.


I'm glad to see I'm not alone in the desert.

> It may be that it reads naturally. It might also be that one of the
> rare cases when it is correct:
>
> do {
> print prompt;
> get reply;
> } while (reply not suitable);
>
> crops up in beginners programs more often than production code and the
> idea sticks.
>
> <snip>
>> A great classic is this:
>>
>> int c;
>> /* skip to end of line */
>> do {
>> c = getc(fp);
>> } while (c != '\n');

>
> Yes, but that is not wrong because of the do .. while. You probably
> intended to leave out the declaration of c (or at least to add
> ... suggesting more intervening code) so that this code comes after
> some other input that might already have set c to '\n' (at least that
> is the form of this bug I've seen repeatedly in student code).


That's a good point, but there is worse: this loop will run forever if the
stream ends without a new-line.
The classic idiom is safer:

while ((c = getc(fp)) != EOF && c != '\n')
continue;

or if EOF is a condition worth reporting:

while ((c = getc(fp)) != '\n') {
if (c == EOF) {
report_error("unexpected end of file);
break;
}
}

For some reason, people tend to overlook special cases when they program
even the simplest tasks with do/while loops.

--
Chqrlie.


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-15-2007
Ben Bacarisse <(E-Mail Removed)> writes:
> "Charlie Gordon" <(E-Mail Removed)> writes:
>
>> "CBFalconer" <(E-Mail Removed)> a écrit dans le message de news:
>> (E-Mail Removed)...
>>> Charlie Gordon wrote:
>>>>
>>> ... snip much banditry ...
>>>>
>>>> yes, many typos in this post (un loupé)
>>>>
>>>> As a rule of thumb, do/while loops are more difficult to master
>>>> than regular while or for loops.
>>>
>>> Why should you think that? "do { something() } while(foo);" is no
>>> more complex than "while (foo) {something())". The only difference
>>> is that the foo test is applied after execution in the do/while
>>> loop, and before execution in the while loop.

>>
>> My own experience proofreading millions on lines of C code tells me
>> that.

>
> ...and my experience teaching programming leads to the same
> conclusion. There seems to be something seductive about do ... while.
> So much so that I just dropped the form from lecture notes as soon as
> I saw the problem.

[...]

I learned Pascal before I learned C. In Pascal, I used
REPEAT
...
UNTIL <condition>;
(similar to C's do-while) much more than I used
WHILE <condition> DO
BEGIN
...
END;

In C, I think the last time I used a do-while loop was in my IOCCC
entry.

Pascal's I/O model, unlike C's, encourages test-at-the-bottom loops.
(Well, C's I/O model seems to encourage such loops too, but not in
correct code.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
user923005
Guest
Posts: n/a
 
      09-17-2007
On Sep 15, 1:10 pm, Keith Thompson <(E-Mail Removed)> wrote:
> Ben Bacarisse <(E-Mail Removed)> writes:
> > "Charlie Gordon" <(E-Mail Removed)> writes:

>
> >> "CBFalconer" <(E-Mail Removed)> a écrit dans le message de news:
> >> (E-Mail Removed)...
> >>> Charlie Gordon wrote:

>
> >>> ... snip much banditry ...

>
> >>>> yes, many typos in this post (un loupé)

>
> >>>> As a rule of thumb, do/while loops are more difficult to master
> >>>> than regular while or for loops.

>
> >>> Why should you think that? "do { something() } while(foo);" is no
> >>> more complex than "while (foo) {something())". The only difference
> >>> is that the foo test is applied after execution in the do/while
> >>> loop, and before execution in the while loop.

>
> >> My own experience proofreading millions on lines of C code tells me
> >> that.

>
> > ...and my experience teaching programming leads to the same
> > conclusion. There seems to be something seductive about do ... while.
> > So much so that I just dropped the form from lecture notes as soon as
> > I saw the problem.

>
> [...]
>
> I learned Pascal before I learned C. In Pascal, I used
> REPEAT
> ...
> UNTIL <condition>;
> (similar to C's do-while) much more than I used
> WHILE <condition> DO
> BEGIN
> ...
> END;
>
> In C, I think the last time I used a do-while loop was in my IOCCC
> entry.
>
> Pascal's I/O model, unlike C's, encourages test-at-the-bottom loops.
> (Well, C's I/O model seems to encourage such loops too, but not in
> correct code.)


To me, the choice is clear:

1. If you always want to perform the code _at least_ once, then use
do {} while(cond);
2. If you may not want to perform the code even one time (because
cond is false), then use while (cond) {} ;

I don't see any reason why one is easier or harder to understand than
the other. To me the complexity is identical.

 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      09-18-2007
> On Sep 15, 1:10 pm, Keith Thompson <(E-Mail Removed)> wrote:
>> Ben Bacarisse <(E-Mail Removed)> writes:
>>> "Charlie Gordon" <(E-Mail Removed)> writes:
>>>> "CBFalconer" <(E-Mail Removed)> a écrit dans le message de news:
>>>> (E-Mail Removed)...
>>>>> Charlie Gordon wrote:
>>>>>>
>>>>>> As a rule of thumb, do/while loops are more difficult to master
>>>>>> than regular while or for loops.

>>
>>>>> Why should you think that? "do { something() } while(foo);" is no
>>>>> more complex than "while (foo) {something())". The only difference
>>>>> is that the foo test is applied after execution in the do/while
>>>>> loop, and before execution in the while loop.

>>
>>>> My own experience proofreading millions on lines of C code tells me
>>>> that.

>>
>>> ...and my experience teaching programming leads to the same
>>> conclusion. There seems to be something seductive about do ... while.
>>> So much so that I just dropped the form from lecture notes as soon as
>>> I saw the problem.

>>
>> [...]
>>
>> I learned Pascal before I learned C. In Pascal, I used
>> REPEAT
>> ...
>> UNTIL <condition>;
>> (similar to C's do-while) much more than I used
>> WHILE <condition> DO
>> BEGIN
>> ...
>> END;
>>
>> In C, I think the last time I used a do-while loop was in my IOCCC
>> entry.
>>
>> Pascal's I/O model, unlike C's, encourages test-at-the-bottom loops.
>> (Well, C's I/O model seems to encourage such loops too, but not in
>> correct code.)

>
> To me, the choice is clear:
>
> 1. If you always want to perform the code _at least_ once, then use
> do {} while(cond);
> 2. If you may not want to perform the code even one time (because
> cond is false), then use while (cond) {} ;
>
> I don't see any reason why one is easier or harder to understand than
> the other. To me the complexity is identical.


The debate is not so simple.
The example I gave is typical of erroneous uses do/while:

people write

do {
operation;
maybe_more_operations;
} while (cond);

where they should write

while (operation, cond && other_cond) {
maybe_more_operations;
}

or

for (; {
operation;
if (!cond)
break;
if (!other_cond)
break;
}

definitely the do/while *seems* simpler and more straightforward, but
it tends to be bogus with conditions ignored or tested at the wrong
place.

--
Chqrlie.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Here a noob, there a noob.... JimDoire MCSE 0 04-10-2008 07:23 PM
sorry ... total noob rollover debug Matt Javascript 4 07-18-2005 07:05 PM
Sorry...it's back to IE for me... :-( Drude Firefox 14 01-22-2005 04:10 AM
Sorry Sorry Sorry but what camera for me - Real Esate Phil Digital Photography 19 12-28-2003 05:28 PM
Oops, sorry. Let me rephrase that. I CAN'T play WMV videos with Mozilla 1.5 Madison Lapierre Firefox 1 12-21-2003 07:00 PM



Advertisments