Avoiding Off-by-One Error
- Look at a scenario with simple values:
initial balance: $100
interest rate: 50%
after year 1, the balance is $150
after year 2 it is $225, or over $200
so the investment doubled after 2 years
the loop executed two times, incrementing years each time
Therefore: years must start at 0, not at 1.
- interest rate: 100%
after one year: balance is 2 * initialBalance
loop should stop
Therefore: must use <
- Think, don't compile and try at random