Blitz:Loops

From Amiga Coding
Jump to: navigation, search

For...Next

For...Next loops are basic counting loops where a variable is incremented (or decremented) each time to loop is repeated, from a start value until it reaches a defined end value. Both the start and the end values can be defined, as can the increment. Only integer values should be used. An example loop is shown below:

For i=1 To 5
  NPrint "This is line ",i
Next i

This example will produce the following output:

This is line 1
This is line 2
This is line 3
This is line 4
This is line 5

the variable i starts at 1 and the loop is executed until the Next i statement is reached. At this point, i is incremented by 1, and if it is less than or equal to 5, the loop is repeated. Otherwise program flow continues with the next statement after the Next i. Note that because the variable value is checked at the end of the loop, the loop will always be executed at least once, even if the variable starts off already exceeding the To value.

(Traditionally i is used as the count variable for loops, with j used for the second nested loop, k for the third and so on. But any integer variable can be used for the loop variable.)

Step

By default, the loop counting variable is incremented by 1 each time the loop executes. This step value can be modified by using the Step keyword:

For i=1 To 10 Step 2
  NPrint "This is line ",i
Next i

This example will produce the following output:

This is line 1
This is line 3
This is line 5
This is line 7
This is line 9

i is incremented by 2 each loop, starting at 1. When i contains 9 and the program reaches Next i, it is incremented to 11. This is greater than the To value of 10, so the loop is finished and the program carries on.

Step can also be used to make a loop decrement:

For i=5 To 1 Step -1
  NPrint "This is line ",i
Next i

This example will produce the following output:

This is line 5
This is line 4
This is line 3
This is line 2
This is line 1

Repeat...Until

Repeat...Until loops are similar to Do...Loop Until and similar loops in other BASIC dialects. Unlike a For...Next loop, there is no counter variable. Instead, the loop is executed and at the end an expression is evaluated. If the expression is true, the loop is finished and the program continues with the statement following the Until statement. If the expression is false, the loop is repeated. For example:

Repeat
  NPrint "Please enter the correct number to continue"
  num = Edit(2)
Until num = 55
NPrint "The correct number was entered"

This example will keep on printing the message and waiting for a number until the number 55 is entered by the user. Once 55 is entered, the program carries on and prints the "correct" message.

These loops can be used to create an infinite loop, i.e. one that never ends, although it is unusual to have a situation where you never want your program to finish:

Repeat
  NPrint "This is annoying"
Until 0

0 will never be true as it is the numerical equivalent of false (the keyword False can also be used here), so the message will be printed again and again forever, or at least until you turn your computer off or halt it with the debugger.

While...End While

While...End While loops are similar to Repeat...Until loops, except that the condition is checked at the start of the loop. This means that if the condition starts off as false, the loop is skipped completely and the code segment inside the loop is not executed. This is very useful for looping through a list of operations where there is a possibility the list might contain zero items. For example:

NPrint "Enter a value to count to"
limit = Edit(4)
count = 1
While count < limit
  count = count + 1
  NPrint "Count ", count
End While

This program counts from 1 to the value entered by the user. This functionality could also be achieved using a For...Next loop, however this code will not print any output if a value of 1 or less is entered, because it only executes when count is less than limit.

Like Repeat...Until loops, infinite loops can easily be created by simply using

While 1
  NPrint "Annoying!"
End While

Blitz can alternatively use the WEnd statement in the place of End While. Execution-wise there is no difference between the two.