Counters and Accumulators

When you see a statement such as the following, what do you think?

number = number + 1

Your first impression might be that the statement is not possible because nothing can be equal to itself plus one. This works in programming, because a programming equals symbol acts like a left-pointing arrow. The assignment statement says “take whatever is on the right side of the equals symbol, evaluate it, and put it in the variable to the left.”

When your program reaches the statement just shown, it adds 1 to the variable named number. If number is 7 to begin with, it now is 8. After it adds 1 and gets 8, it then stores 8 in number, replacing the 7 that was originally there. The final result is one more than the initial value. ...

Get Absolute Beginner's Guide to Programming, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.