Thread: So another simple program just help me with this logic

1. So another simple program just help me with this logic

Code:
```#include <stdio.h>

int main ()
{
int day = 1;
float amount = .01;

while (day <= 31){
printf ("Day:%d \t Amount:\$%.2f\n", day, amount);
amount *= 2;
day++;
}
printf ("-----------------------------------------------------------\n");

day = 1;
amount = .01;
while (day <= 31){
printf ("Day:%d \t Amount:\$%.2f\n", day, amount);
amount * 2;
day++;
}
return 0;
}```
So yeah it's two kind of examples. Just curious about the logic behind

Code:
`amount *= 2;`
and

Code:
`amount * 2;`
Why did it not multiply in the 2nd while loop while in the first it multiplied. I mean it's somewhat the same right? What amount *= 2 is saying to amount * 2? Just confused.

2. Recall that:
Code:
`amount *= 2;`
is logically equivalent to:
Code:
`amount = amount * 2;`

3. Originally Posted by laserlight
Recall that:
Code:
`amount *= 2;`
is logically equivalent to:
Code:
`amount = amount * 2;`
So amount * 2 is just multiplying the variable right? Or no? I jsut dont get it.

4. Code:
`amount * 2;`
The result of this will be amount multiplied by 2 but it won't be assigned to anything. If you want the value of amount to be multiplied by two then you need to assign the result to amount which you could do with
Code:
`amount = amount * 2;`
In this case though you are assigning to the same variable you are multiplying so you can instead use
Code:
`amount *= 2;`

5. Thanks that helped bro! Alright new thread XD

Popular pages Recent additions