1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C: Simple for loop is not working

  1. Aug 21, 2012 #1
    C: Simple "for" loop is not working

    1. The problem statement, all variables and given/known data

    I'm taking an online course (yay lynda!) in C/C++, and I am trying to replicate a course example that prints "Hello, World!" five times, using a simple "for" loop.

    2. Relevant equations

    for(int i = 1; i <= 5; ++i){...}

    3. The attempt at a solution

    // working.c by Bill Weinman <http://bw.org/> [Broken]
    #include <stdio.h>

    int main( int argc, char ** argv ) {
    for(int i = 1; i <= 5; ++i) {
    int x = printf("Hello, World!\n") * 5;
    printf("printf returned %d\n", x);
    printf("%d", i);
    return 0;
    }
    }



    My program runs, but only with 1 iteration of the loop. For reading ease, I've attached a screenshot taken in Eclipse.

    4. Other Potentially Relevant Information

    I previously have had trouble with a dummy program that required user input. It had been a long day, so I kind of closed my eyes and pretended it was gonna be fine, but I never solved that problem, either.

    5. Further Work

    Uninstall and reinstall everything?? I'd really rather not, but it's all I've got right now.

    6. Thank you!!
     

    Attached Files:

    Last edited by a moderator: May 6, 2017
  2. jcsd
  3. Aug 21, 2012 #2

    Borek

    User Avatar

    Staff: Mentor

    Re: C: Simple "for" loop is not working

    Code (Text):

    #include <stdio.h>

    int main( int argc, char ** argv ) {
        for(int i = 1; i <= 5; ++i) {
            int x = printf("Hello, World!\n") * 5;
            printf("printf returned %d\n", x);
            printf("%d", i);
            return 0;
        }
    }
    Use [noparse]
    Code (Text):
     
    [/noparse] tags to show your code.

    Think about position of the return statement. What it does?
     
  4. Aug 21, 2012 #3
    Re: C: Simple "for" loop is not working

    Thanks, that's much cleaner!

    The return 0 statement sets the value of the main function (?) to zero. Does setting the whole main function=0 somehow mess with the value of my counter?
     
  5. Aug 21, 2012 #4

    Borek

    User Avatar

    Staff: Mentor

    Re: C: Simple "for" loop is not working

    What does the "return" alone do?

    As its name implies.
     
  6. Aug 21, 2012 #5

    Mark44

    Staff: Mentor

    Re: C: Simple "for" loop is not working

    A return statement in main returns control to whoever called main, which is usually the operating system.
     
  7. Aug 21, 2012 #6
    Re: C: Simple "for" loop is not working

    Return brings you back to what comes next. (I know that's convoluted, but it's the only way I can think of to express that return generally moves you along, but as its name implies, return can also bring you back to the beginning.)

    But why would the program encounter the return line at all before the loop has run all of its iterations?
     
  8. Aug 21, 2012 #7

    Borek

    User Avatar

    Staff: Mentor

    Re: C: Simple "for" loop is not working

    Compare:

    Code (Text):
    for (i = 0;i != 5;i++)
    {
       printf("%s","Hello World\n");
       return;
    }
    and

    Code (Text):
    for (i = 0;i != 5;i++)
    {
       printf("%s","Hello World\n");
    }
    return;
    Do you see the difference?
     
  9. Aug 21, 2012 #8
    Re: C: Simple "for" loop is not working

    Oh. My. Gosh. I put the line that ends my program INSIDE my for loop!! No wonder it ends after 1 iteration!

    Thank you!!!

    (My apologies for exclamation point abuse; I get overly excited by understanding stuff.)
     
  10. Aug 21, 2012 #9

    Borek

    User Avatar

    Staff: Mentor

    Re: C: Simple "for" loop is not working

    We all did such things at some point, welcome to the club :wink:
     
  11. Aug 21, 2012 #10
    Re: C: Simple "for" loop is not working

    :smile:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C: Simple for loop is not working
  1. C++- while-loop (Replies: 7)

Loading...