Question about the formula Mosh used

In ‘Java Part 1’ in the ‘clean code’ section ep 7 minute (1:10), he used a formula to calculate the remaining balance. Can’t we subtract the payment from the principal directly in a for loop? Like why we need a formula ?

Welcome in here.
First thank you to provide clear information on the part you are talking about.
I did not take that course so I don’t know the code itself, but from what you say, I see there is a matter of loop and formula.

I’d say the formula is a nicer solution for :

  • Readability: It is simpler to read one line and it is cleaner.
  • Performance: If you can compute something directly without looping through a data set, this is preferred.

Now I may be off, but that’s my guess for now.

Good luck.

1 Like

You’re welcome! And thank you for replying fast.
You have a valid point about what you said, that’s a good way of thinking.
Appreciate your effort both ways.