BASIC7.WPS

BASIC LANGUAGE PROGRAMMING

USING FOR NEXT LOOPS IN PROGRAMS

 

VARIABLE LOOPS USING THE INPUT STATEMENT

As you saw in the last exercise, we can set up FOR/NEXT

loops in a variety of ways. One of the more interesting

ways is to use the INPUT STATEMENT to give a value to a variable in a FOR/NEXT loop.

Here is an example of a simple program which counts up by 5s X times. X is the variable

you input from the keyboard:

CLS

INPUT X

FOR COUNT = 1 TO X

LET RESULT = RESULT +5

PRINT RESULT

NEXT COUNT

END

Type in this program a bit and play around with it so that you get a "feel" for

what the variable does in the program.

 

USING VARIABLES FROM AN INPUT STATEMENT FOR STEPPING

Another slightly more advanced use of variables in loops

is in stipulating both the number of times the loop

will run and the STEP or increment value of the step.

Here is an example of a program which loops X times jumping

up in increments of Y:

CLS

INPUT X

INPUT Y

FOR COUNT = 1 TO X STEP Y

LET RESULT = RESULT +5

PRINT RESULT

NEXT COUNT

END

Try running the program with X set to 100 and Y at 5!

 

 

 

 

 

 

 

ASSIGNMENT- Write programs to do the following:

1) Write a computer program that counts down to new years

day from the January 1st. Print a copy of the program

and the results when the program is run.

2) Write a program which counts from 100 to 1000 by 100s

Print a copy of the program and the results when the

program is run.

3) Write a program which prints a persons name and address

out 25 times. Print a copy of the program and the results

when the program is run.

 

4) Write a program which prints the 13 times table from 26

to 200. Print a copy of the program and the results

when the program is run.

 

5) Write a program which prints the word "HELLO" as many

times as the user wishes. (Use INPUT statement)

Print a copy of the program and the results when the

program is run.

6) Write a program which prints the 5 times table up to

a number the user wants. (Use INPUT statement)

Print a copy of the program and the results when the

program is run.

7) Write a program to that prints ANY times table the user

wants. It should also start and end at numbers the user

wants. Print a copy of the program and the results when

the program is run.

Copyright - Barry Boyle (bboyle@kawartha.net)