Seriously struggling with C

Discussion in 'C Programming' started by RG, Feb 20, 2006.

  1. RG

    RG Guest

    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
    RG, Feb 20, 2006
    #1
    1. Advertising

  2. RG

    Guest

    Hi!
    I am not an expert in programming but I do like programming as it is
    "mentally stimulating." The first thing that I will tell you is don't
    compare yourselves with others, once you start doing that, you are
    going to feel worse.
    Try seeing programming like a game. First, try and build your own
    solution from a mathematical perspective, without taking the
    constraints of C programming. I wont say that it will be easy, but give
    it a shot. Try to write an alogorithm, I dont use them much, but it
    does help to understand the direction in which the program flows. So
    once you have your solution try writing it in your own words. And then
    in the end use the syntax of the programming language and try it out in
    your editor.
    I dont how much this will help you. I am a freshman myself, and I am
    just telling you the way I see it. Just take it as a challenge and I am
    sure you will be fine.
    Take care and Good Luck.
    Priyam
    , Feb 20, 2006
    #2
    1. Advertising

  3. 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,


    one cause of your problems may be that there is no definitive right or
    wrong answer. Anything you can do with a while() you can do with a
    for(),
    and the functionality of a for() can be duplicated with a while() and a

    couple of other statements. C's for() is very powerful (some might
    argue,
    too powerful). I tend to classify loops into two main types.

    - a fixed known number of iterations (times around the loop), for these
    I
    use for()
    - an iterate until some condition is met

    // loop n times
    for (i = 0; i < n; i++)
    do_something ();

    I'd just learn this as a piece of boilerplate code to drop in when you
    need a
    known number of iterations.

    // loop while some condition holds
    while (condition)
    do_something();

    You can pretty much do anything with those two.
    There is also a while at the end:-

    do {
    do_something()
    }
    while (condition);

    but it practice this is far less useful. note it always does at least
    one iteration.

    A C idiom for a loop that goes "forever"

    for (;;) // never stops
    {
    do_something();
    if (exit_condition)
    break;
    do_something_else();
    }

    this can sometimes be useful, I'd avoid it until you are comfortable
    with the first two forms.

    Where C can be confusing is when the "cleverness" of the for() is
    exploited.

    // walk a linked list
    for (i = head; i != NULL; i = i->next)
    flop_node (i->data);

    If this confuses you, use a while()

    for (i = 1, j = 1; i < k, j < l; i++, j + 2)
    something_scary (i, j);

    if this confuses you... chop the program listing into little pieces and
    feed it
    to the author of the code :)

    Keep It Simple is the first lore of programming

    > 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.



    --
    Nick Keighley

    The fscanf equivalent of fgets is so simple
    that it can be used inline whenever needed:-
    char s[NN + 1] = "", c;
    int rc = fscanf(fp, "%NN[^\n]%1[\n]", s, &c);
    if (rc == 1) fscanf("%*[^\n]%*c);
    if (rc == 0) getc(fp);
    Nick Keighley, Feb 20, 2006
    #3
  4. 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.


    Please, provide an example of something you don't grasp. The while loop
    is probably the easiest loop construct to understand.

    Example:

    /* Show the contents of the array a of length len. */
    k = 0;
    while (k < len) {
    printf("%d\n", a[k]);
    k++;
    }
    /* Just by looking at the loop guard, we know that k >= len here
    (actually k == len). */


    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.
    August Karlstrom, Feb 20, 2006
    #4
  5. "RG" <> wrote in message
    news:...
    > 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
    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
    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
    is when the block of code in the loop needs to execute at least once. When
    the code 'needs to execute at least once,' you use the do while loop. The
    last flow control constructs are procedures, functions, and switches.
    Procedures and functions aren't usually used for looping. The switch
    statement is useful any time the data or a counter you've created doesn't or
    won't increment or decrement by one.

    The next step is to 'see' how a for loop works when it is written as a while
    loop:

    i=0;
    while(i<10)
    {
    /* continue starts here for while() */
    /* ...code body... */
    /* continue starts here for for() */
    i++;
    }

    That is the same as this for loop:

    for (i=0; i<10; i++)
    {
    /* ...code body... */
    }

    The continue statement in a while continues execution at the top of the
    while. But, for the for loop, execution continues before the third
    expression (i++).


    Rod Pemberton
    Rod Pemberton, Feb 20, 2006
    #5
  6. RG

    Guest

    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


    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++.

    Bill Hanna
    , Feb 20, 2006
    #6
  7. "Rod Pemberton" <> writes:
    > "RG" <> wrote in message
    > news:...
    >> 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
    > 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.

    > 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.

    --
    Keith Thompson (The_Other_Keith) <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.
    Keith Thompson, Feb 20, 2006
    #7
  8. writes:
    > 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.

    >
    > 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.

    --
    Keith Thompson (The_Other_Keith) <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.
    Keith Thompson, Feb 20, 2006
    #8
  9. "Keith Thompson" <> wrote in message
    news:...
    > "Rod Pemberton" <> writes:
    > > "RG" <> wrote in message
    > > news:...
    > >> 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
    > > 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.

    > > 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.

    Rod Pemberton
    Rod Pemberton, Feb 20, 2006
    #9
  10. Keith Thompson wrote:
    > "Rod Pemberton" <> writes:

    [about the while-statement]
    >>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.


    I agree. Moreover, the advantage of the "while" statement over the C
    "for" statement is its clear semantics. But hey, how many C programmers
    care about that ;-).


    August

    --
    "C is what happened when Algol and Fortran got drunk and spent the
    weekend together." -- Scott More (from the thread "Java or C++" in
    comp.programming 2005.10.19)
    August Karlstrom, Feb 20, 2006
    #10
  11. "Rod Pemberton" <> writes:
    > "Keith Thompson" <> wrote in message
    > news:...
    >> "Rod Pemberton" <> 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.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Feb 20, 2006
    #11
  12. Keith Thompson wrote:
    > 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.
    August Karlstrom, Feb 20, 2006
    #12
  13. RG

    Ian Collins Guest

    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.
    Ian Collins, Feb 20, 2006
    #13
  14. RG

    Pedro Graca Guest

    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>
    Pedro Graca, Feb 20, 2006
    #14
  15. 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.
    Ben Bacarisse, Feb 21, 2006
    #15
  16. 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.
    Ben Bacarisse, Feb 21, 2006
    #16
  17. RG

    John Bode Guest

    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.
    John Bode, Feb 21, 2006
    #17
  18. On 2006-02-20, Rod Pemberton <> wrote:
    >
    > "RG" <> wrote in message
    > news:...
    >> 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.
    Richard G. Riley, Feb 21, 2006
    #18
  19. RG

    pete Guest

    Richard G. Riley wrote:
    >
    > On 2006-02-20, Rod Pemberton <> 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
    pete, Feb 21, 2006
    #19
  20. RG

    Ed Jensen Guest

    Keith Thompson <> 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!
    Ed Jensen, Feb 21, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. clintonG
    Replies:
    9
    Views:
    504
    clintonG
    Sep 5, 2005
  2. Bret Casanova
    Replies:
    11
    Views:
    535
    Thomas G. Marshall
    Aug 9, 2004
  3. rf
    Replies:
    14
    Views:
    703
    ApicalSoft
    Nov 20, 2004
  4. Retlak

    CSS is seriously broken

    Retlak, Jan 26, 2004, in forum: XML
    Replies:
    5
    Views:
    379
    Mike Partridge
    Jan 27, 2004
  5. lagunasun

    seriously, i need a good book,

    lagunasun, Mar 20, 2005, in forum: C++
    Replies:
    2
    Views:
    346
    Bradley
    Mar 20, 2005
Loading...

Share This Page