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