Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: C Code

  1. Oct 13, 2004 #1
    Is this the correct basic code for a for-loop?

    for (n=1, n<=10, n++) {
    /* statements */

    If not what am I doing wrong?
  2. jcsd
  3. Oct 13, 2004 #2


    User Avatar
    Science Advisor

    Use semi-colons rather than commas!

    for (int i=0;i<=10;i++)


    (I cannot bring myself to put that first "{" up on the previous line! It looks so unbalanced.)
  4. Oct 13, 2004 #3


    User Avatar
    Science Advisor
    Homework Helper

    I think you want semicolons separating items in the "for" satement"

    for (i = 1; n <= 0; n++)
  5. Oct 13, 2004 #4
    thank you very, very much. :biggrin:
  6. Oct 13, 2004 #5
    Same thoughts here!!!!
    i dunno why many of the books promote that style!!
    I always had it ur way cuz as u put it , it looks balanced not to mention neat!!

    -- AI
  7. Oct 13, 2004 #6
    Okay one more problem. For a programming homework I had to print my name ten times and leave a blank line at the end. My code looked like this:

    for (k=1; k<=10; k++) {
    printf("%2d Joe Schmoe\n", k);

    That prints my name ten times but doesnt leave a blank line at the end. If I put two \n's then that leaves a blank line in between each printing. Is there a way to print a blank line at the end within the structure of the for-loop or do I have to just use printf("\n"); after the whole thing?
  8. Oct 13, 2004 #7


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    You could use something like

    if (k==10) printf("\n");

    but that would be silly. Just put the printf("\n"); at the end, after the for loop.

    - Warren
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook