Standard loop algorithms
A few patterns cover most loop problems, a running total, a count of matches, a smallest or largest so far and peeling off digits with % 10 and / 10, and each one starts its variables before the loop and updates them inside it.
From AP Computer Science A.