PlayBasic TUTORIALS


     New to programming and need some help getting started ? - Well, then our tutorial section will help kick start your programming adventure. If you're looking for more Source Code / Tutorials & Media ? - Then remember to visit the PlayBasic Resource board on our forums.

Found #1 item in Next category


 FOR NEXT control loops in PlayBasic

By: Kevin Picone Added: January 7th, 2023

Category: All,Loops,For,Next,Learn to Code

 FOR NEXT control loops in PlayBasic

  A FOR NEXT control loop is a type of loop that allows you to repeat a block of code a specific number of times. Here is an example of how to use a FOR NEXT loop in BASIC:

PlayBasic Code:
FOR i = 1 TO 10
  PRINT i
NEXT i

COMMANDS USED: PRINT |


This FOR NEXT loop will print the numbers from 1 to 10. Let's break down the code:

   The FOR statement initializes a loop variable i to the value of 1.
   The TO keyword specifies the end value of the loop variable, which is 10 in this case.
   The block of code between the FOR and NEXT statements is executed as long as the loop variable i is less than or equal to 10.
   The PRINT statement prints the value of the loop variable i.

   The NEXT statement increments the loop variable i by 1 and checks if it is still less than or equal to 10. If it is, the loop continues. If not, the loop ends.

Here is the output of this FOR NEXT loop:

1
2
3
4
5
6
7
8
9
10


You can also specify a step value for the loop variable, like this:
PlayBasic Code:
FOR i = 1 TO 10 STEP 2
  PRINT i
NEXT i

COMMANDS USED: PRINT |


This FOR NEXT loop will print the numbers from 1 to 10, incrementing the loop variable i by 2 each time. The output of this loop will be:

1
3
5
7
9


You can also specify a step value that is negative, like this:

PlayBasic Code:
FOR i = 10 TO 1 STEP -2
  PRINT i
NEXT i

COMMANDS USED: PRINT |


This FOR NEXT loop will print the numbers from 10 to 1, decrementing the loop variable i by 2 each time. The output of this loop will be:

10
8
6
4
2


Here are some additional details about the FOR NEXT loop in BASIC:

   The FOR NEXT loop is a pretest loop, which means that the loop condition (the value of the loop variable) is checked before the loop is executed. If the loop condition is FALSE, the loop is skipped and the program continues with the next statement after the NEXT statement.

   The loop variable i is a local variable that is only accessible within the loop. It is created when the loop starts and is destroyed when the loop ends.

   The STEP keyword specifies the amount by which the loop variable is incremented or decremented each time the loop iterates. If you omit the STEP keyword, the default step value is 1.

   You can nest FOR NEXT loops, which means you can have a FOR NEXT loop inside another FOR NEXT loop. Here is an example of a nested FOR NEXT loop:

PlayBasic Code:
FOR i = 1 TO 3
  FOR j = 1 TO 3
    PRINT str$(i) +" "+str$( j)
  NEXT j
NEXT i

COMMANDS USED: PRINT | STR$ |


This nested FOR NEXT loop will print all possible combinations of the numbers from 1 to 3. The output of this loop will be:

1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3


   You can use the EXIT FOR statement to exit a FOR NEXT loop prematurely. For example:

PlayBasic Code:
FOR i = 1 TO 10
  IF i = 5 THEN EXITFOR
  PRINT i
NEXT i

COMMANDS USED: PRINT |
This FOR NEXT loop will print the numbers from 1 to 4, then exit the loop when the loop variable i reaches 5. The output of this loop will be:

1
2
3
4




Viewing Page [1] of [0]





Looking for More Tutorials?:



 

 
     
 
       

(c) Copyright 2002 / 2024 Kevin Picone , UnderwareDesign.com  - Privacy Policy   Site: V0.99a [Alpha]