# Small Basic: Multiplication Table Example

In this example, you’ll create a program that displays the multiplication table for numbers 1 through 5. See the complete program in the Listing. Trace through the program to understand how it works! (Tracing is where you write down the values on a paper and follow the logic of the code.)

Listing:

1 ' Multiplication.sb

2 ' Displays the multiplication table

3

4 TextWindow.WriteLine("   1   2   3  4   5  ")

5 TextWindow.WriteLine("   -------------------")

6 For N = 1 To 5

7   TextWindow.Write( N + " |")

8   For M= 1 To 5

9     TextWindow.CursorLeft = 4 * M

10     TextWindow.Write( N*M)

11   EndFor

12   TextWindow.WriteLine("")

13 EndFor

Output:

1   2   3   4    5

-----------------------

1 | 1   2   3   4    5

2 | 2   4   6   8    10

3 | 3   6   9   12  15

4 | 4   8   12 16  20

5 | 5   10 15 20  25

Did you trace through the steps of the code? Good. Now let's do it again, together...

Lines 4-5 are pretty basic, you're just writing the first few lines of the table. Line 6 begins a For loop with another nested For loop. The outer loop runs five times, to build the 5 rows in the output above. Line 7 begins each row with the number of that row (1-5). Line 8 starts the nested loop, to write 5 more columns in each row. Line 9 moves the cursor over to write each number. Line 10 writes the multiplied number. The nested loop ends on Line 11. Line 12 moves the cursor down to the next line for the next row to start. The outer loop ends on Line 13, and it moves back up to Line 6 to complete the For loop a total of five times.

Small and Basically yours,

- Ninja Ed & Majed Marji

Tags