"For" not functionning

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel.C

    "For" not functionning

    Hello.
    I'm a complete beginner and I cant go through the third exercise of my book
    ;-(((
    I wrote :
    #include <stdio.h>

    main()
    {
    int fahr;
    for (fahr = 300; fahr <= 0; fahr = fahr - 20)
    printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
    }

    There is no output. The reverse (from 0 to 300) is OK.
    I'm using MinGW with windows XP.

    What's wrong ?
    Thanks in advance.
    Daniel


  • Bartc

    #2
    Re: &quot;For&qu ot; not functionning


    "Daniel.C" <dcolardelleZZZ Z@free.frwrote in message
    news:47caccae$0 $17300$426a74cc @news.free.fr.. .
    Hello.
    I'm a complete beginner and I cant go through the third exercise of my
    book ;-(((
    I wrote :
    #include <stdio.h>
    >
    main()
    {
    int fahr;
    for (fahr = 300; fahr <= 0; fahr = fahr - 20)
    printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
    Try fahr>=0

    --
    Bart



    Comment

    • Doug Miller

      #3
      Re: &quot;For&qu ot; not functionning

      In article <47caccae$0$173 00$426a74cc@new s.free.fr>, "Daniel.C" <dcolardelleZZZ Z@free.frwrote:
      >Hello.
      >I'm a complete beginner and I cant go through the third exercise of my book
      >;-(((
      >I wrote :
      >#include <stdio.h>
      >
      >main()
      >{
      int fahr;
      for (fahr = 300; fahr <= 0; fahr = fahr - 20)
      printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
      >}
      >
      >There is no output. The reverse (from 0 to 300) is OK.
      >I'm using MinGW with windows XP.
      >
      >What's wrong ?
      What's wrong is that the second part of a for statement ( fahr <= 0, in this
      case) specifies the condition under which the loop is to continue running.
      You've told it to start out with fahr = 300, and subtract 20 from it,
      continuing for as long as the value of fahr is less than or equal to zero.

      Of course, fahr is already greater than zero when you start, so the condition
      under which the loop is to continue running is already false from the very
      beginning -- and the loop never executes.

      It appears to me that you've misunderstood the second part as specifying the
      condition under which the loop is to stop. That's not the case.

      --
      Regards,
      Doug Miller (alphageek at milmac dot com)

      It's time to throw all their damned tea in the harbor again.

      Comment

      • Daniel.C

        #4
        Re: &quot;For&qu ot; not functionning

        "Doug Miller" <spambait@milma c.coma écrit dans le message de news:
        DfAyj.11490$xq2 .3963@newssvr21 .news.prodigy.n et...
        In article <47caccae$0$173 00$426a74cc@new s.free.fr>, "Daniel.C"
        <dcolardelleZZZ Z@free.frwrote:
        >>Hello.
        >>I'm a complete beginner and I cant go through the third exercise of my
        >>book
        >>;-(((
        >>I wrote :
        >>#include <stdio.h>
        >>
        >>main()
        >>{
        > int fahr;
        > for (fahr = 300; fahr <= 0; fahr = fahr - 20)
        > printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
        >>}
        >>
        >>There is no output. The reverse (from 0 to 300) is OK.
        >>I'm using MinGW with windows XP.
        >>
        >>What's wrong ?
        >
        What's wrong is that the second part of a for statement ( fahr <= 0, in
        this
        case) specifies the condition under which the loop is to continue running.
        You've told it to start out with fahr = 300, and subtract 20 from it,
        continuing for as long as the value of fahr is less than or equal to zero.
        >
        Of course, fahr is already greater than zero when you start, so the
        condition
        under which the loop is to continue running is already false from the very
        beginning -- and the loop never executes.
        >
        It appears to me that you've misunderstood the second part as specifying
        the
        condition under which the loop is to stop. That's not the case.
        >
        --
        Regards,
        Doug Miller (alphageek at milmac dot com)
        >
        It's time to throw all their damned tea in the harbor again.
        Many thanks too. I understood it the wrong way. I'm improving myself ;-)))
        Daniel


        Comment

        • Martin Ambuhl

          #5
          Re: &quot;For&qu ot; not functionning

          Daniel.C wrote:
          [...]
          for (fahr = 300; fahr <= 0; fahr = fahr - 20)
          ^^^
          That's backwards. fahr is never less than 0/
          [...]

          Comment

          Working...