One of the things loops are good for is generating tabular data. For example, before computers were readily available, people had to calculate logarithms, sines and cosines, and other common mathematical functions by hand. To make that easier, there were books containing long tables where you could find the values of various functions. Creating these tables was slow and boring, and the result tended to be full of errors.
When computers appeared on the scene, one of the initial reactions was, βThis is great! We can use the computers to generate the tables, so there will be no errors.β That turned out to be true (mostly), but shortsighted. Soon thereafter computers and calculators were so pervasive that the tables became obsolete.
Well, almost. It turns out that for some operations, computers use tables of values to get an approximate answer, and then perform computations to improve the approximation. In some cases, there have been errors in the underlying tables, most famously in the table the original Intel Pentium used to perform floating-point division.
The sequence \t represents a tab character. A tab character causes the cursor to shift to the right until it reaches one of the tab stops, which are normally every eight characters. This helps the second column stay lined up even when the length of the number in the first column goes from 1 to 2 digits. If we used the string " " to space out the columns, the second column would jump over when the first column changed length.
When making a table, or doing any work with a loop, it is important to think about what variable really is the loop control variable. Say you are asked to print a table of investment income like the one below. The investment starts at $10,000 and grows by 20% until it reaches at least $100,000:
Stop and think about what is really controlling that loop? Does the problem specify how many months the process takes? No - it says that we want to stop when we hit $100,000+ not after X months. (Though you could do math to figure out the number of months.) So despite counting from 1 to 14, this isnβt really a counting loop. It is a sentinel value loop that is looking for the balance to become $100,000+. Here is what the program might look like:
There are multiple updates that need to happen each year. We need to increment years, calculate the interest based on the current balance, and then update the balance.
The loop does not print the last row of the table. After the updates are done, if we have hit the TARGET, the loop stops without printing the final value. So we do one last print statement after the loop to handle that row.
Instead of magic numbers for START_VALUE, GROWTH_RATE,and TARGET, we have constants. When values in a program should be reconfigurable, but do not come from input, it is a good idea to make them constants and declare them together in one place.
Doing this is better than putting an if inside the loop to do something special only in the first or last iteration. We could move the final output inside the loop above by doing: