GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Download
  • News
  • Community
  • Resources
  • Language Reference
  • SmallBASIC Manual

This view

  • Original page
  • New address

GeezerVison is showing smallbasic.github.io — SmallBASIC | FOR

Edit History

FOR

FOR counter = start TO end [STEP incr] … NEXT

Home > Reference > Language

FOR element IN array (or map) … NEXT

Defines a FOR/NEXT loop.

  • counter - A numeric variable to be used as the loop counter.
  • start - A numeric expression; the starting value of counter.
  • end - A numeric expression; the ending value of counter.
  • incr - A numeric expression; the value by which counter is incremented or decremented with each iteration of the loop. The default value is 1.
  • element - A variable to be used as the copy of the current element.
  • array - An array or map variable

FOR counter = start TO end [STEP incr] … NEXT

SmallBASIC begins processing of the FOR/NEXT block by setting counter equal to start. Then, if ‘incr’ is positive and counter is not greater than end, the commands between the FOR and the NEXT are executed. When the NEXT is encountered, counter is increased by ‘incr’, and the process is repeated. Execution passes to the command following the NEXT if counter is greater than end.

If increment is negative, execution of the FOR/NEXT loop is terminated whenever counter becomes less than end. FOR/NEXT loops may be nested to any level of complexity, but there must be a NEXT for each FOR.

FOR element IN array … NEXT

The commands-block will be repeated for LEN(array) times. Each time the ‘element’ will holds the value of the current element of the array. FOR/NEXT loops may be nested to any level of complexity, but there must be a NEXT for each FOR.

Example 1

for i = 1 to 9
    print i
next

' Output 1 2 3 4 5 6 7 8 9

Example 2

for i = 1 to 9 Step 2
    print i
next

' Output: 1 3 5 7 9

Example 3

for i = 9 to 1 Step -1
    print i
next

' Output 9 8 7 6 5 4 3 2 1

Example 4

a =  [12, 6, 23, -4]

for i in a
    print i
next

' Output 12 6 23 -4

Example 5

a =  ["car", "tree", "house"]

for i in a
    print i
next

' Output car tree house

Example 6

a = {x:1, s:"test1"}
b = {x:2, s:"test2"}
c = {x:3, s:"test3"}

MapVar = [a,b,c]

for i in MapVar    
    print i.x, i.s
next

' Output:   1 test1
'           2 test2
'           3 test3

Example 7

a = {x:1, y:2, s:"test1"}

for i in a
    print i + " = " + a(i)
next

' Output:   y = 2
'           x = 1
'           s = test1

Example 8: Changing counter inside the loop

for i = 1 to 10
    i = i + 1
    print i
next

' Output: 2 4 6 8 10

Example 8: Changing stop parameter inside the loop

e = 10

for i = 1 to e
    e = 5
    print i
next

' Output: 1 2 3 4 5
Code samples using FOR
000 hello.bas   001 3 ways to print hello 5 times.bas   002 numeric variables.bas   003 conditional branching.bas   004 loops.bas   005 challenge.bas   006 arrays+.bas   100lines.bas   2048.bas   3d block lettes.bas   3d rotating cube with message.bas   3d wire cube v1.bas   3d wire cube.bas   3d_palmx.bas   3d_torus.bas   3dtictac.bas   3dtorus.bas   3dttt.bas   3dttt.bas   4tune.bas   7gables.bas   A Rubens, Peter Paul Landscape.bas   agendus.bas   ai.bas   analog clock.bas   anball 1.0.bas   angel feather.bas   anomail.bas   Another center finder.bas  
Language
AND   AS   BAND   BG   BOR   BYREF   CALL   CASE   CATCH   CONST   DECLARE   DEF   DO   ELIF   ELSE   ELSEIF   END   END TRY   ENDIF   EQV   EXIT   FALSE   FI   FOR   FUNC   GOSUB   GOTO   IF   IFF   IMP   IN   LABEL   LET   LIKE   LOCAL   LSHIFT   MDL   MOD   NAND   NEXT   NOR   NOT   ON   OR   REM   REPEAT   RETURN   RSHIFT   SELECT   STEP   STOP   SUB   THEN   THROW   TO   TRUE   TRY   UNTIL   USE   USG   USING   WEND   WHILE   XNOR   XOR  
This page was last edited on Mon, 27 Feb 2023 23:19:20 +0100 | Markdown processed with pandoc 3.1.12.1
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.