"For" not functionning

Discussion in 'C Programming' started by Daniel.C, Mar 2, 2008.

  1. Daniel.C

    Daniel.C Guest

    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
    Daniel.C, Mar 2, 2008
    #1
    1. Advertising

  2. Daniel.C

    Bartc Guest

    "Daniel.C" <> wrote in message
    news:47caccae$0$17300$...
    > 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
    Bartc, Mar 2, 2008
    #2
    1. Advertising

  3. Daniel.C

    Doug Miller Guest

    In article <47caccae$0$17300$>, "Daniel.C" <> wrote:
    >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.
    Doug Miller, Mar 2, 2008
    #3
  4. Daniel.C

    Daniel.C Guest

    "Bartc" <> a écrit dans le message de news:
    w3Ayj.16664$...
    >
    > "Daniel.C" <> wrote in message
    > news:47caccae$0$17300$...
    >> 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


    I'll kick my ass with pleasure. Thanks a lot !
    Daniel
    Daniel.C, Mar 2, 2008
    #4
  5. Daniel.C

    Daniel.C Guest

    "Doug Miller" <> a écrit dans le message de news:
    DfAyj.11490$...
    > In article <47caccae$0$17300$>, "Daniel.C"
    > <> wrote:
    >>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
    Daniel.C, Mar 2, 2008
    #5
  6. Daniel.C wrote:
    [...]
    > for (fahr = 300; fahr <= 0; fahr = fahr - 20)

    ^^^
    That's backwards. fahr is never less than 0/
    [...]
    Martin Ambuhl, Mar 2, 2008
    #6
    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. Roxanne
    Replies:
    0
    Views:
    1,227
    Roxanne
    Jul 4, 2003
  2. Andy Elmhorst
    Replies:
    2
    Views:
    480
    Bassel Tabbara [MSFT]
    Jul 8, 2003
  3. Kristian Domke
    Replies:
    11
    Views:
    503
    George Sakkis
    Jan 23, 2008
  4. Stephanie Stowe
    Replies:
    2
    Views:
    213
    Peter X
    Apr 7, 2004
  5. Ruby Freak

    To be not, or not to be not?

    Ruby Freak, Sep 23, 2008, in forum: Ruby
    Replies:
    2
    Views:
    135
    Thomas B.
    Sep 23, 2008
Loading...

Share This Page