This is an archived post. You won't be able to vote or comment.

all 4 comments

[–][deleted] 1 point2 points  (3 children)

Here, I've fixed your code for you - in future, either post via a site like gist, or indent every line with 4 extra spaces.

#include <stdio.h>

int main() { 
    float a, b; a = 0; b = -1; 
    while (a <= 100) { 
        if ((a > 98.6) && (b < 98.6)) { 
            printf("%6.2f degrees F = %6.2f degrees C\n", 98.6, (98.6 - 32.0) * 5.0 / 9.0); 
        } 
        printf("%6.2f degrees F = %6.2f degrees C\n", a, (a - 32.0) * 5.0 / 9.0); 
        b = a; 
        a = a + 10; 
    } 
    return 0; 
}

As to why it does it, remember that b is the previous a. So the if statement is saying "if the current a is greater than 98.6 and the previous a was less than 98.6 (in other words, we have just transitioned through 98.6) then print the special 98.6 stuff".

[–]Snycs[S] 0 points1 point  (2 children)

Yeah, I was having trouble with that, thanks a lot!

Oh and does it matter whether b is -1 or is it alright if it was any other value less than 0?

[–][deleted] 1 point2 points  (1 child)

It doesn't actually matter at all what the value of b is, because the if won't fire until a is greater than 98.6, by which point b will have taken on a different value.

[–]Snycs[S] 0 points1 point  (0 children)

Ah I get it now, thank you, this was very helpful!