NASutils Knowledge BaseUnofficialPolski

BASIC · lesson 10 of 30 · Decisions and loops · bonusRepeat with loops

for counts, while tests first, repeat tests last.

Programming on the NAS › BASIC in 30 lessons

10-loops.yab Download 10-loops.yab

rem for: you know how many times
for i = 1 to 5
  print i, " ";
next i
print

for i = 10 to 0 step -5
  print i, " ";
next i
print

rem while: the test comes first
left = 100
while left > 20
  left = left / 2
  print "half is ", left
wend

rem repeat: the body runs at least once
tries = 0
repeat
  tries = tries + 1
  print "try ", tries
until tries = 3

Terminal

$ yabasic 10-loops.yab
1 2 3 4 5 
10 5 0 
half is 50
half is 25
half is 12.5
try 1
try 2
try 3

Exercise

Count from 20 down to 2 in steps of 3.