Learn To Code: Control Structures and Loops in PlayBASIC

Started by kevin, January 01, 2023, 11:34:17 PM

Previous topic - Next topic

kevin

  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: [Select]
FOR i = 1 TO 10
PRINT i
NEXT i



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: [Select]
FOR i = 1 TO 10 STEP 2
PRINT i
NEXT i



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: [Select]
FOR i = 10 TO 1 STEP -2
PRINT i
NEXT i



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: [Select]
FOR i = 1 TO 3
FOR j = 1 TO 3
PRINT str$(i) +" "+str$( j)
NEXT j
NEXT i



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: [Select]
FOR i = 1 TO 10
IF i = 5 THEN EXITFOR
PRINT i
NEXT i



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


kevin

    REPEAT UNTIL LOOPS in PlayBASIC


 In PlayBASIC, the Repeat/Until control structure allows you to repeatedly execute a block of code until a certain condition is met. The syntax for the Repeat/Until structure is as follows:


Repeat
  ' code to be executed repeatedly
Until condition



 The code block between Repeat and Until will be executed repeatedly until the condition is met. When the condition is met, the loop will exit and control will be passed to the next line of code after the Until statement.

 Here is an example of a Repeat/Until loop in PlayBASIC:

PlayBASIC Code: [Select]
x = 0

Repeat
x = x + 1
Print x
Until x = 10

Print "Loop complete"
sync
waitkey



 In this example, the loop will repeat 10 times, printing the value of x each time. When x reaches 10, the Until condition will be met and the loop will exit. The message "Loop complete" will then be printed.


   Read PlayBASIC Help Files about LOOPS



kevin

  While / EndWhile Control Structures In PlayBASIC


    In PlayBASIC, the While/EndWhile control structure allows you to repeatedly execute a block of code as long as a certain condition is met. The syntax for the While/EndWhile structure is as follows:


While condition
  ' code to be executed repeatedly
EndWhile


   The code block between While and EndWhile will be executed repeatedly as long as the condition is met. When the condition is no longer met, the loop will exit and control will be passed to the next line of code after the EndWhile statement.

   Here is an example of a While/EndWhile loop in PlayBASIC:

PlayBASIC Code: [Select]
x = 0

While x < 10
x = x + 1
Print x
EndWhile

Print "Loop complete"
sync
waitkey



  In this example, the loop will repeat until x is no longer less than 10. The loop will print the value of x each time it is executed, and when x reaches 10 the While condition will no longer be met and the loop will exit. The message "Loop complete" will then be printed.


  Read PlayBASIC Help Files about LOOPS







kevin

  Can you explain what the DO / LOOP control structure is in PlayBASIC ?


   In PlayBASIC, the DO/LOOP control structure allows you to repeatedly execute a block of code a specific number of times or until a certain condition is met. There are two forms of the DO/LOOP structure: the DO/LOOP form and the DO/LOOP WHILE form.

  The DO/LOOP form has the following syntax:

DO
  ' code to be executed repeatedly
LOOP


  The code block between DO and LOOP will be executed repeatedly until the EXIT DO statement is encountered.

  Here is an example of a DO/LOOP loop in PlayBASIC:

PlayBASIC Code: [Select]
x = 0

DO
x = x + 1
Print x
If x = 10 Then ExitDO
LOOP

Print "Loop complete"
sync
waitkey



  In this example, the loop will repeat 10 times, printing the value of x each time. When x reaches 10, the Exit DO statement will be encountered and the loop will exit. The message "Loop complete" will then be printed.

  The DO/LOOP WHILE form has the following syntax:


DO
  ' code to be executed repeatedly
LOOP Optional While condition


 The code block between DO and LOOP will be executed repeatedly as long as the condition is met. When the condition is no longer met, the loop will exit and control will be passed to the next line of code after the LOOP statement.

 Here is an example of a DO/LOOP WHILE loop in PlayBASIC:

PlayBASIC Code: [Select]
x = 0

DO
x = x + 1
Print x
LOOP x < 10

Print "Loop complete"



 In this example, the loop will repeat until x is no longer less than 10. The loop will print the value of x each time it is executed, and when x reaches 10 the LOOP WHILE condition will no longer be met and the loop will exit. The message "Loop complete" will then be printed.

 So, in general, you might use a DO/LOOP loop when you want to repeat a block of code an unknown number of times, or when you want to repeat a block of code until a certain condition is met. On the other hand, you might use a DO/LOOP WHILE loop when you want to repeat a block of code a specific number of times, or when you want to repeat a block of code while a certain condition is true.


  PlayBASIC Tutorials