Thread: Help Please, Beginner Question, rewrite loop

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    2

    Help Please, Beginner Question, rewrite loop

    Hello, I'm really new to C programming, just started about a week and a half ago.

    I worked with Visual Basic 6.0 last year, but i've forgotten alot since then. It appears my previous experience isn't much of a help with the higher languages.

    But to the question.

    How would I write a loop equivalent to the FOR loop below without using && or ||?

    It's from the ANSI C book by Brian Kernighhan, Dennis Ritchie.

    Code:
    #include <stdio.h>
    
    /* easy version, with && and || */
    
    main()
    {
    int i;
    int lim;
    int c; 
    int s[100];
    
          for (i=0; i<lim-1 && (c=getchar()) != '\n' && c != EOF; ++i)
            s[i] = c;
            
    }

  2. #2
    Registered User
    Join Date
    Dec 2006
    Posts
    2
    FYI, I'm pretty much learning on my own entirely (no teacher). I'd like to have a jump start before I hit college. I'm going to work through most of the C book, then move onto a reputable book for C++.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    Use while and your code does not work, think about what your using and comparing.
    You rant and rave about it, but at the end of the day, it doesn't matter if people use it as long as you don't see.
    People are free to read the arguments, but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
    Eventually, this "fast and loose" approach of yours will bite you one too many times, then you'll figure out the correct way to do things. - Salem

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    Code:
    int i = 0
    while (i < lim-1)
    {
        c = getchar
        if c == '\n' break
        if c == EOF break
        s[i] = c
        i++
    }
    can't just copy and paste this, but here an outline of how it could be done

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You also need to initilize lim.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop question
    By JuzMe in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2009, 08:39 AM
  2. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-29-2008, 11:12 PM
  3. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  4. for loop question
    By cdalten in forum C Programming
    Replies: 4
    Last Post: 03-20-2006, 09:42 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 08:39 PM