"For" not functionning

D

Daniel.C

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
 
B

Bartc

Daniel.C said:
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
 
D

Doug Miller

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.
 
D

Daniel.C

Doug Miller said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

A Newbie Question 7
K&R Ex 1-3 33
k&r2 16
prob w/the C prog. lang. book(ANSI C) 14
Weird Behavior with Rays in C and OpenGL 4
Help for newbie please 2
What's wrong ? 21
I'm not understanding something 23

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top