Function while ()?

This program should convert degrees Fahrenheit to 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 original comments, when I try to add a formula for virginium to the body of the main() function, I get -17.8 for each Fahrenheit value. The result is as follows:

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

etc. etc. However, when I put the Celsius formula in 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 is this happening?

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

 #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; } } 
+6
source share
5 answers

When you set the value of a variable, the actual value is calculated and then stored in the variable. However, you do not store the formula for the variable. Thus, at startup

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

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

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

+7
source

Welcome to SO. You come across one of the main flow control operators present in every programming language.

In principle, any computer program is a sequence of instructions, and you can provide that the computer executes them one at the top of the page.

Logically, this means that each command is executed only once, if there is no way to tell the program to "go back" and do something else. This is what the while statement does (this is not a function). In English, the while statement (usually called 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 contains a group of statements that are executed only once (each thing before while ).

Then the statements inside the while loop are repeated with fahrenheit each time, using the loop: 0, 5, 10, 15, ..., 95, 100. When you add 5, the last time, fahrenheit is 105, so the program exits the loop and continues (in this case, terminating the main() function and exiting the program.

If you calculate celsius outside the loop, this happens only once. Then each time through the loop it gives the calculated value, even if the fahrenheit constantly changing.

When you move the calculations inside the while , celsius recounted at each pass through the loop, using the new fahrenheit value and thus creating different results.

Hope this helps

+3
source

This is 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 */ celsius = (fahrenheit - 32.0) * 5.0/9.0; /* Works here */ outside the block, while it will be repeated only once (and this will be when fahrenheit = 0.0), but if you put celsius = (fahrenheit - 32.0) * 5.0/9.0; /* Works here */ celsius = (fahrenheit - 32.0) * 5.0/9.0; /* Works here */ inside the block, and celsius will change its value to fahrenheit, and the condition fahrenheit <= max true.

+2
source

Code:

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

assigns to a variable celsius (fahrenheit - 32.0) * 5.0/9.0

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

If you put the code inside a while loop, it will reassign celsius every time the while loop locks. Fahrenheit changes inside this loop, so every time a while loop is executed, both Fahrenheit and Celsius will be different if you reassign the target c celsius = (fahrenheit - 32.0) * 5.0/9.0; inside a while loop.

+2
source

You cannot expect the fahrenheit value to change outside the loop when converting a single value:

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

The only time a value with a change occurs is if you change the fahrenheit value. Out of cycle. This is not happening.

Also, comparing int with a float never a good idea. The reason is that the floating point number stored in the IEEE-754 single-point floating point format is not an exact representation of the value provided for storage. (for example, 32.2 is not saved in the floating point format exactly 32.2). Although you can make a relative comparison:

 while(fahrenheit <= max) 

Better practice:

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

You change the fahrenheit here in a loop, and then you can expect the celsius value to change accordingly.

Regarding floating point comparisons, you'll want to read the floating point guide - what every programmer should know .... The problem is that you are not comparing what you consider yourself.

-2
source

Source: https://habr.com/ru/post/979530/


All Articles