Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Seriously struggling with C

Reply
Thread Tools

Seriously struggling with C

 
 
Keith Thompson
Guest
Posts: n/a
 
      02-20-2006
"Rod Pemberton" <(E-Mail Removed)> writes:
> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> "Rod Pemberton" <(E-Mail Removed)> writes:

[...]
>> > This is my thought process on loops:
>> > 1) while - never use
>> > 2) for - use as much as possible
>> > 3) do while - only use when necessary
>> >
>> > I'll explain. The while loop only checks for a single statement
>> > being true.

>>
>> Condition, not statement.

>
> Expression, not condition? I doubt the OP is familiar with any language or
> definitions of ISO/ANSI specifications, so please don't criticize me for
> phrasing to his level of understanding.


Yes, the standard uses the word "expression", and a search of the
standard indicates that it doesn't refer to the expression in a while
statement as a "condition". But "condition" is a perfectly reasonable
term to use in this context. "Statement" is not; it's simply
incorrect. I see no basis for your assumption that the the word
"statement" is going to be clearer to the OP.

>> > It is very simple. So simple in fact that it really is useless.
>> > It duplicates functionality that exists in a for loop.
>> > Therefore, why use it?

>>
>> That's bad advice. Use while() whenever it's the most natural
>> construct to use. Arbitrarily transforming "while (condition)" into
>> "for (;condition" is foolish.

>
> Not using the braces of a compound-statement for while's and if-else's when
> there is a single statement is also the most natural construct to use. But,
> of course, as we've seen many times here, that leads to later problems.
> People forget to add the braces when a statement is converted to a
> compound-statement. The same holds true for the while statement. A while
> will frequently be converted to a for sometime in the future. At which
> point, the programmer is likely to introduce an error. Why not just do it
> manner which reduces the chance that an error, now or in the future, is
> introduced? I've seen you suggest this on many occasions. I find it odd
> that you'd take a contrary position now.


Personally, I always use braces on compound statements (except in rare
cases where it's clearer to put the whole thing on a single line).

I don't believe it's particularly likely that any particular while
loop will be changed to a for loop in the future. Consider the common
while ((c = getchar()) != EOF) { ... }
Do you really think that this:
for (;(c = getchar()) != EOF { putchar(c); }
is an improvement?

Just use whichever form is clearer. Very often, that's going to be a
while loop.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(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.
 
Reply With Quote
 
 
 
 
August Karlstrom
Guest
Posts: n/a
 
      02-20-2006
Keith Thompson wrote:
> (E-Mail Removed) writes:
>> Go to www.bloodshed.net and download their free C/C++ compiler.
>>Then use it to test out your code to learn how to program in C/C++.

>
> How is that useful? Since the OP is taking a course in C programming,
> it's safe to assume he already has access to a C compiler. If there's
> something about the bloodshed compiler that makes it particularly
> useful for beginners, you might want to say so.
>
> BTW, what do you mean by "C/C++ compiler"? There is no "C/C++" language.


He probably means "a compiler that can compile C or C++ source texts".


August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-20-2006
Rod Pemberton wrote:
>
>>That's bad advice. Use while() whenever it's the most natural
>>construct to use. Arbitrarily transforming "while (condition)" into
>>"for (;condition" is foolish.

>
>
> Not using the braces of a compound-statement for while's and if-else's when
> there is a single statement is also the most natural construct to use. But,
> of course, as we've seen many times here, that leads to later problems.
> People forget to add the braces when a statement is converted to a
> compound-statement. The same holds true for the while statement. A while
> will frequently be converted to a for sometime in the future. At which
> point, the programmer is likely to introduce an error. Why not just do it
> manner which reduces the chance that an error, now or in the future, is
> introduced? I've seen you suggest this on many occasions. I find it odd
> that you'd take a contrary position now.
>

Will it? I can't recall ever doing that. I've gone the other way and
used while in place of for where incrementing the loop counter became
conditional. I often find splitting the termination condition adds clarity.

Would you use for when doing something like reading to the end of a file?

--
Ian Collins.
 
Reply With Quote
 
Pedro Graca
Guest
Posts: n/a
 
      02-20-2006
RG wrote:
> This semester I have started a course in C programming. I was moving
> along fine until I reached to the topic of loops (feeling embarrassed
> among you elite programmers). My prof. would post program questions
> and the solutions online. For practice I would try to do the problems.
> I would reach to a certain point in the code, for example as far as
> error trapping, but when the loop arrives, like knowing whether to use
> for, while do, how to properly use the increment and decrements, and
> counters,I am just not proficient in it and the class is moving ahead.
> Eventually i would have to look at the solution and wondering to
> myself, the reason i could not think of it. What ticks me off is that
> other kids are getting this stuff easily, while I am having a hard
> time.Kindly advise me on what actions I shoul take. I would
> particularly like to have an idea of the thought process to engage in
> when given the programme to write.


"Practice makes perfect."

Do your textbook examples, get used to the various loop constructs, try
all of them for every example you see. After a while you'll have
developed a feeling for your preferred way to do it ... and, of course,
you can always change the loop to some other construct later.


Allow me, as a fellow newbie, to provide a few examples:


********
** Write a program that reads numbers from the keyboard
** until 0 is entered, and then outputs their sum.

I'd use a while(), as in

int getnumber(void); /* implementation left out for brevity */
int main(void) {
int sum = 0;
int number;

while ((number = getnumber()) != 0) {
sum += number;
}
printf("The sum is %d.\n", sum);
return 0;
}


********
** Write a program that reads ten numbers from the keyboard
** and then outputs their sum.

Here, I might try a for() loop (even though the control variable isn't
used inside the loop)

int getnumber(void); /* implementation left out for brevity */
int main(void) {
int n, sum = 0;

for (n = 0; n < 10; ++n) {
sum += getnumber();
}
printf("The sum is %d.\n", sum);
return 0;
}


********
** Write a function that takes an array and a number of elements
** to add, and returns their sum.

For this type of thing (where it doesn't matter if I start at the bottom
or at the top of the array) I really like to do it a bit more cryptic,
and avoid creating a new local variable:

int sum_elems(int array[], int parcels) {
int sum = 0;

while (parcels--) {
sum += array[parcels];
}
return sum; /* array[parcels-1] + ... + array[1] + array[0] */
}

compare with the for() version

int sum_elems(int array[], int parcels) {
int sum = 0;
int n;

for (n = 0; n < parcels; ++n) {
sum += array[n];
}
return sum; /* array[0] + array[1] + ... + array[parcels-1] */
}



As others have noted, the for() and the while() loop have (almost)
the same possibilities; the do ... while() loop is similar but with
the extra twitch that is executes at least once. I rarely use it,
and often code around it and use a while instead

do {
/* stuff that changes `done' */
} while (!done);

I might code as

done = 0;
while (!done) {
/* stuff that changes `done' */
}


But this is just me ... try all the different loop constructs for the
same problem, noticing which gave you more troubles in implementing so
that you can later choose the one you prefer more comfortably.

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-21-2006
On Sun, 19 Feb 2006 17:57:47 -0800, RG wrote:

> This semester I have started a course in C programming. I was moving along
> fine until I reached to the topic of loops


I used to teach programming to beginners and the symptoms you describe are
not uncommon. I think the key remark you make is:

> Eventually i would have to look at the solution and wondering to myself,
> the reason i could not think of it.


First, don't beat yourself up about not getting the solution because it is
very unlikely that you would get (exactly) the instructor's solution.
Give yourself credit if you have had some ideas that were along the same
sort of lines -- even if you had used a different kind of loop and you got
all the details wrong! Programming is very precise, and that can mean
that your attempts may seem further away from being solutions than they
actually are.

Secondly, read and learn from the solution. It sounds like you follow
them but simply can't get there yet yourself. Provided you do follow
them, set yourself a series of problems to modify each one in a number of
ways to learn how to make new loops out of the ones you have seen (process
an array from the other end, sum every other element rather than them all
or whatever). If you can't think of variations, try posting an example
here and asking for some mini-problems based on it. This will let you
explore the possibilities of various loops without having to generate the
whole program.

Third, get some help locally. Whilst you can get some help here, you will
get more if you can talk things over with someone in real time. If the
instructor is not available, ask some of the other students. They are
likely to be flattered and, if they are grow up enough, will be happy to
try to help you.

--
Ben.

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-21-2006
On Mon, 20 Feb 2006 23:17:08 +0000, Pedro Graca wrote:

> As others have noted, the for() and the while() loop have (almost) the
> same possibilities; the do ... while() loop is similar but with the extra
> twitch that is executes at least once. I rarely use it,


It has been widely noted (though not so widely that I could give you a
reference if you asked!) that "while" is more useful than "do" because it
handles degenerate cases naturally. It is unfortunate than the example of
prompting for input (much discussed in an earlier thread) is both a
natural use of "do" and beloved of introductory programming course
designers. It gives a false impression to beginners of how likely they
are to havce need of it.

--
Ben.

 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      02-21-2006
RG wrote:
> Greetings friends,
>
> This semester I have started a course in C programming. I was moving
> along fine until I reached to the topic of loops (feeling embarrassed
> among you elite programmers). My prof. would post program questions
> and the solutions online. For practice I would try to do the problems.
> I would reach to a certain point in the code, for example as far as
> error trapping, but when the loop arrives, like knowing whether to use
> for, while do, how to properly use the increment and decrements, and
> counters,I am just not proficient in it and the class is moving ahead.
> Eventually i would have to look at the solution and wondering to
> myself, the reason i could not think of it. What ticks me off is that
> other kids are getting this stuff easily, while I am having a hard
> time.Kindly advise me on what actions I shoul take. I would
> particularly like to have an idea of the thought process to engage in
> when given the programme to write.
> Thanks for your time and consideration.
>
> RG


It takes different people different amounts of time to learn certain
concepts. I know it's frustrating, *especially* when everyone else
seems to get it so easily, but keep plugging at it and eventually it
*will* make sense. I've been there; it took me a full two weeks longer
to grok the idea of a linked list than any of my classmates (not to
mention figuring out how to implement it in Fortran...). C's not the
easiest language to learn for novice programmers, anyway.

As for loops...

Generally, if you're iterating over a finite set of values or items
(either repeating an operation X times, or walking over an array or
other sequential structure), you would use a for loop. If you're
repeating an operation while some condition is true (or false), you'd
use a while loop. A do-while loop is a special case of a while loop,
where the operations in the loop are executed at least once.

 
Reply With Quote
 
Richard G. Riley
Guest
Posts: n/a
 
      02-21-2006
On 2006-02-20, Rod Pemberton <(E-Mail Removed)> wrote:
>
> "RG" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
>> Greetings friends,
>>
>> This semester I have started a course in C programming. I was moving
>> along fine until I reached to the topic of loops

><snip>
>> like knowing whether to use
>> for, while do, how to properly use the increment and decrements, and
>> counters,

><snip>
>> I would
>> particularly like to have an idea of the thought process to engage in
>> when given the programme to write.

>
> This is my thought process on loops:
> 1) while - never use


so you disagree with the most famous c line in history?

while(*dest++=*src++);


> 2) for - use as much as possible
> 3) do while - only use when necessary
>
> I'll explain. The while loop only checks for a single statement being true.
> It is very simple. So simple in fact that it really is useless. It


This is totally ridiculous. Many algorithms and program flows are
based on a single condition.

> duplicates functionality that exists in a for loop. Therefore, why use it?
> Many C programmers don't. while(1) becomes for(;. while (a<b) becomes
> for(;a<b .The for loop is a very powerful loop construct that can
> implement almost any loop. The only time you don't want to use a
> for loop


And, as such, can be overkill for simple solutions. What can be easier
to read than

while(numThingsToDo--)

????


There is nothing big and clever by using "for" to obfuscate your code
where the power of the "for" loop is not required.

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      02-21-2006
Richard G. Riley wrote:
>
> On 2006-02-20, Rod Pemberton <(E-Mail Removed)> wrote:
> >


> > This is my thought process on loops:
> > 1) while - never use

>
> so you disagree with the most famous c line in history?
>
> while(*dest++=*src++);
>
> > 2) for - use as much as possible
> > 3) do while - only use when necessary
> >
> > I'll explain.
> > The while loop only checks for a single statement being true.
> > It is very simple. So simple in fact that it really is useless.
> > It

>
> This is totally ridiculous.


I can't make any sense out of that either.

> Many algorithms and program flows are
> based on a single condition.
>
> > duplicates functionality that exists in a for loop.
> > Therefore, why use it?
> > Many C programmers don't.
> > while(1) becomes for(;. while (a<b) becomes
> > for(;a<b .The for loop is a very powerful loop construct that can
> > implement almost any loop. The only time you don't want to use a
> > for loop

>
> And, as such, can be overkill for simple solutions. What can be easier
> to read than
>
> while(numThingsToDo--)
>
> ????
>
> There is nothing big and clever by using "for" to obfuscate your code
> where the power of the "for" loop is not required.


K&R2
3.5 Loops--While and For
"Whether to use while or for
is largely a matter of personal preference."

Most of the loops I write are while loops.
I write do loops whenever I can,
but the appropriate situations don't come up as often
as they do for the while loops and for loops.

When I want to increment a variable from one value to another,
I use a for loop,
but a substantial portion of the for loops that I've written,
have been condensed from while loops.

--
pete
 
Reply With Quote
 
Ed Jensen
Guest
Posts: n/a
 
      02-21-2006
Keith Thompson <(E-Mail Removed)> wrote:
> I don't believe it's particularly likely that any particular while
> loop will be changed to a for loop in the future. Consider the common
> while ((c = getchar()) != EOF) { ... }
> Do you really think that this:
> for (;(c = getchar()) != EOF { putchar(c); }
> is an improvement?
>
> Just use whichever form is clearer. Very often, that's going to be a
> while loop.


How about this?

while (c = getchar(), c != EOF) { putchar(c); }

I meant this as a joke, but the more I look at it, the more I like it!
 
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
2.0 Processiing Model Seriously Flawed? clintonG ASP .Net 9 09-05-2005 02:59 PM
rf, thank you, I apologize seriously rf HTML 14 11-20-2004 02:57 AM
How do you use another class file... Seriously Bret Casanova Java 11 08-09-2004 02:28 AM
CSS is seriously broken Retlak XML 5 01-27-2004 06:59 PM
seriously ANNOYING problems with Mozilla Firebird 0.6.1 Astromannix Firefox 1 08-13-2003 09:03 AM



Advertisments