Ozapell Basic Learn Programming #3

The FOR NEXT loop is a simple way to loop through one or more instructions for a specified number of times.

Example:

FOR X=1 to 12

PRINTLINE X

NEXT X

In this example, the numbers 1 through 12 will be displayed when the program is run. The variable X is first assigned the value of 1. When the program reaches NEXT X, the value in X is increased to 2 and the current position in the program jumps back to the FOR statement.

In Ozapell Basic, FOR NEXT loops can also decrease in value.

For example,

FOR Y=12 to 1

PRINTLINE Y

NEXT Y

In this example, the numbers 12 through 1 will be displayed when the program is run. Ozapell Basic determines that each NEXT Y will decrease the value in Y based on the values in the FOR statement.

The values used in a FOR statement must be integers (whole numbers) including 0 and negative numbers.

Example:

FOR Z=-5 TO 5

PRINTLINE Z

NEXT Z

When the program is run, the numbers -5 through 5 will be displayed including the 0 in the middle.

Finally, it should be noted that the values provided in the FOR statement can also be variables when the range needed is not known until run-time. This would be useful when looping through a line of text where the length of the text isn’t known to the programmer.

Partial example:

FOR C=0 to L

NEXT C

In this partial example, the variable C will first be assigned the value of 0 and then loop until C reaches the value contained in the variable L.

Ozapell Basic on Steam

 

Advertisements
Ozapell Basic Learn Programming #3

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s