Steve3043
Steve3043

Reputation: 79

"while()" function?

This program is supposed to convert degrees Fahrenheit into degrees Celsius:

#include <stdio.h>
int main() {
    float fahrenheit, celsius;
    int max, min, step;

    max = 100;
    min = 0;
    step = 5;

    fahrenheit = 0.0;
    //celsius = (fahrenheit - 32.0) * 5.0/9.0; DOESN'T WORK HERE

    printf("\n");
    printf("This program converts fahrenheit into celsius \n");

    while(fahrenheit <= max) {
        celsius = (fahrenheit - 32.0) * 5.0/9.0; /* Works here */
        printf("%3.0f %6.2f\n", fahrenheit, celsius);
        fahrenheit = fahrenheit + step;
    }
}

As I noted in my source comments, when I try put the formula for celsius into the body of the main() function, I get -17.8 for every single fahrenheit value. The output looks like this -

0 -17.78
5 -17.78
10 -17.78
15 -17.78
20 -17.78
25 -17.78

and so on and so on. However, when I put the celsius formula into the while() function, I get the correct celsius values for each fahrenheit value. It looks like this:

0 -17.78
5 -15.00
10 -12.22
15  -9.44
20  -6.67

Why does that happen?

Here's the code that doesn't work. It's identical to the code above, except for the location of the celsius formula. (At least, I think it is.)

#include <stdio.h>
//this program is supposed to convert fahrenheit into celsius 
int main() {
    float fahrenheit, celsius;
    int max, min, step;

    max = 100;
    min = 0;
    step = 5;

    fahrenheit = 0.0;
    celsius = (fahrenheit - 32.0) * 5.0/9.0; 

    printf("\n");
    printf("This program converts fahrenheit into celsius \n");

    while(fahrenheit <= max) {
        printf("%3.0f %6.2f\n", fahrenheit, celsius);
        fahrenheit = fahrenheit + step;
    }
}

Upvotes: 6

Views: 532

Answers (5)

Patricio S
Patricio S

Reputation: 2130

That's because while is a loop that is used to repeat a block of code.

If you put celsius = (fahrenheit - 32.0) * 5.0/9.0; /* Works here */ outside the block of while it will iterate just once (and it's going to be when fahrenheit = 0.0), but if you put celsius = (fahrenheit - 32.0) * 5.0/9.0; /* Works here */ inside the block of while celsius will be changing his value as fahrenheit does, while the condition fahrenheit <= max is true.

Upvotes: 2

David C. Rankin
David C. Rankin

Reputation: 84622

You cannot expect the value of fahrenheit to change outside the loop as you are converting a single value:

(-32.0) * 5.0/9.0 = -17.8 (every time)

The only time the value with change is if you are changing the value of fahrenheit itself. Outside of the loop. That doesn't happen.

Additionally, comparing an int against a float is never a good idea. The reason being that a floating point number, stored in IEEE-754 Single Precision Floating Point Format is not an exact representation of the value provided for storage. (e.g 32.2 is not stored in floating point format as exactly 32.2) While you can make a relative comparison:

while(fahrenheit <= max) 

It is better practice to do:

int i = 0;
for (i = 0; i < max; i+=5) {
    fahrenheit += (float)i;
    celsius = (fahrenheit - 32.0) * 5.0/9.0;
...

You are changing fahrenheit here within the loop and you can then expect the value of celsius to change accordingly.

Regarding floating point comparison, you will want to read The Floating-Point Guide - What Every Programmer Should Know .... The problem is you are not comparing what you think you are.

Upvotes: -2

jspitkin
jspitkin

Reputation: 31

The code:

celsius = (fahrenheit - 32.0) * 5.0/9.0;

assigns the variable celsius with (fahrenheit - 32.0) * 5.0/9.0

Where you call the code outside the while loop, fahrenheit is 0 so celsius becomes -17.78

If you put the code inside the while loop, it will reassign celsius each time the while block loops. Fahrenheit changes inside this loop so each time the while loop executes, both fahrenheit and celsius will be different if you reassign celsius with celsius = (fahrenheit - 32.0) * 5.0/9.0; inside the while loop.

Upvotes: 2

kdopen
kdopen

Reputation: 8215

Welcome to SO. You are coming to grips with one of the basic flow control statements present in every programming language.

Basically, any computer program is a sequence of instructions and you can envisage the computer executing them one at a time from the top to the bottom of the page.

Logically, that means every instruction gets executed only once, unless there's a way to tell the program to "go backwards" and do something again. That's what the while statement (it's not a function) does. In english a while statement (usually called a while loop) does something like this

if a condition is true
    execute some instructions
    go back and check the condition and keep looping
else 
    continue after the loop

So, your main loop has a group of statements which are executed only once (every thing before the while).

Then the statements inside the while loop are repeated with fahrenheit taking on the a different value each time through the loop: 0, 5, 10, 15, ..., 95, 100. When you add 5 the last time, fahrenheit has the value 105, so the program leaves the loop and continues on (in this case, ending the main() function and exiting the program.

If you have the celsius calculation outside the loop, it only happens once. Then each time through the loop it prints out the value you calculated - even though fahrenheit is constantly changing.

When you move the calculation inside the while loop, celsius gets recalculated on each pass through the loop, using the new value of fahrenheit and thus producing a different results.

Hope this helps

Upvotes: 3

Kevin Ji
Kevin Ji

Reputation: 10499

When you set a value to a variable, the actual value is computed, and then stored to the variable. You do not store a formula for the variable, however. Thus, when you run

celsius = (fahrenheit - 32.0) * 5.0/9.0;

outside of the while loop, it uses the current value of fahrenheit (which is 0.0), and calculates the value for celsius, which is -17.78.

Inside the while loop, although fahrenheit changes, celsius will not, because there are no statements inside of the while loop to actually change the value of the variable. This is why you have to move the statement into the while loop, to make sure that the celsius value updates every time the fahrenheit value changes.

Upvotes: 7

Related Questions