Below is the code for displaying distance between time=0 till time=4 with interval of 0.5
I have several questions:
1) why do the /t=t+interval/ can not be placed before the /while/
2) if i put the /t=t+interval/ right befor the printf, it will display the distance untill time is 4.5 and not 4... why?
1) why do the /t=t+interval/ can not be placed before the /while/
if it was before the while it would be executed only once, but it needs to be executed on each pass through the loop.
2) if i put the /t=t+interval/ right befor the printf, it will display the distance untill time is 4.5 and not 4... why?
Think of the sequence of events.
• When is the printf() executed, and what will be the current value of t.
• when is the while loop condition tested, and what will be the current value of t.
Why CAN the //distance=0.5*a*t*t;// be placed AFTER the //printf// ???
If I think of the sequence, if it is placed after the printf, then the computer will get instruction to printf the distance without first knowing the formula for distance?
The first time through the loop, you're going to print garbage for distance.
Line 3, you declare distance, but do not initialize it.
Line 14, you attempt to print distance, but the value is garbage.
So yes, the calculation needs to be before the printf.