Skip to main content

11.12) while...end Statement: Examples


Ted has £10,000 and wants to invest it in a pension fund that pays a fixed interest rate of 3% per year. He is 20 years old. He has calculated that he will be able to retire once his investment reaches £1,000,000. When will Ted be able to retire?

[matlab]
>> balance=1E4;
>> year=20;
>> while (balance <1E6)
>> balance=(balance*1.03);
>> year=year+1;
>> end
>> display(['Ted can retire when he reaches ', num2str(year-1)]);
[/matlab]

Do you think Ted should get a job? If someone invested £10,000 for Ted in a 4% fixed rate account when he was born, could Ted retire on his investment? Use a for loop to calculate how much a loaf of bread would cost on his retirement if they cost 20p when he was born and the average inflation rate was 3%.