Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
FOR...NEXT Statement Programming Example
◄Example► ◄Contents► ◄Index► ◄Back►
──────────────────────────────────────────────────────────────────────────────
'This example uses FOR...NEXT loops to print the first 11 columns of Pascal's
'Triangle, in which each number is the sum of the number immediately above it
'and the number immediately below it in the preceding column.
DEFINT A-Z
CLS 'Clear screen.
CONST MAXCOL = 11
DIM A(MAXCOL, MAXCOL)
PRINT "Pascal's Triangle"
FOR M = 1 TO MAXCOL
A(M, 1) = 1: A(M, M) = 1'Top and bottom of each column is 1.
NEXT M
FOR M = 3 TO MAXCOL
FOR N = 2 TO M - 1
A(M, N) = A(M - 1, N - 1) + A(M - 1, N)
NEXT N
NEXT M
Startrow = 13 'Go to the middle of the screen.
FOR M = 1 TO MAXCOL
Col = 6 * M
Row = Startrow
FOR N = 1 TO M
LOCATE Row, Col: PRINT A(M, N)
Row = Row + 2 'Go down 2 rows to print next number.
NEXT N
PRINT
Startrow = Startrow - 1 'Next column starts 1 row above
NEXT M 'preceding column.