Help understanding while loop

I'm reviewing an example from an online course that I'm taking on basics for C++. The program takes in a loan balance and payment amount and calculates the amount of months it will take you to pay off your loan. I understand most of the program but I am having difficulties figuring out how exactly the program is calculating the months when there is no math formula calculating that (see while loop). Here is the code:

1
2
3
4
5
6
	while(balance > 0)
	{
		months = months+1; //or months++
		balance = balance - payment; //or balance = -=payment
	}
	cout <<"It will take you "<<months<<" months to pay off your loan."



closed account (E0p9LyTq)
while creates a loop of variable turns, based on each loop through what value balance contains.

Look closely at what lines 3 & 4 do.
Hello new2c,

Line 3 can be written as "months++;".

Line 4 could be written as "balance -= payment;", not what you have in the comment.

Line 6 is missing a semi-colon.

Hope that helps,

Andy
Topic archived. No new replies allowed.