NASutils Knowledge BaseUnofficialPolski

BASIC · lesson 11 of 30 · Decisions and loopsLeave or skip a turn

break leaves a loop and continue starts its next turn. do repeats until a break.

Programming on the NAS › BASIC in 30 lessons

11-loop-control.yab Download 11-loop-control.yab

rem break leaves the loop, continue starts its next turn
for disk = 1 to 8
  if disk = 3 continue
  if disk = 6 break
  print "checking disk ", disk
next disk
print "stopped before disk 6; disk 3 was left out"

sum = 0
do
  input "Number, 0 ends: " n
  if n = 0 break
  sum = sum + n
loop
print "Sum: ", sum

Terminal

$ yabasic 11-loop-control.yab
checking disk 1
checking disk 2
checking disk 4
checking disk 5
stopped before disk 6; disk 3 was left out
Number, 0 ends: 12
Number, 0 ends: 30
Number, 0 ends: 0
Sum: 42

Exercise

Let the loop also end after five numbers.