Task

Show a loop which prints random numbers (each number newly generated each loop) from 0 to 19 (inclusive).

If a number is 10, stop the loop after printing it, and do not generate any further numbers.

Otherwise, generate and print a second random number before restarting the loop.

If the number 10 is never generated as the first number in a loop, loop forever.



Related tasks

   Loop over multiple arrays simultaneously
   Loops/Break
   Loops/Continue
   Loops/Do-while
   Loops/Downward for
   Loops/For
   Loops/For with a specified step
   Loops/Foreach
   Loops/Increment loop index within loop body
   Loops/Infinite
   Loops/N plus one half
   Loops/Nested
   Loops/While
   Loops/with multiple ranges


 
PROGRAM "loopbreak"
 
IMPORT "xst" ' for XstGetSystemTime
 
DECLARE FUNCTION Entry()
 
' Pseudo-random number generator
' Based on the rand, srand functions from Kernighan & Ritchie's book
' 'The C Programming Language'
DECLARE FUNCTION Rand()
DECLARE FUNCTION SRand(seed%%)
 
FUNCTION Entry()
  XstGetSystemTime (@msec)
  SRand(INT(msec) MOD 32768)
  DO
    a%% = Rand() MOD 20
    PRINT FORMAT$("##", a%%);
    IF a%% = 10 THEN EXIT DO
    b%% = Rand() MOD 20
    PRINT FORMAT$(" ##", b%%)
  LOOP
  PRINT
END FUNCTION
 
' Return pseudo-random integer on 0..32767
FUNCTION Rand()
  #next&& = #next&& * 1103515245 + 12345
END FUNCTION USHORT(#next&& / 65536) MOD 32768
 
' Set seed for Rand()
FUNCTION SRand(seed%%)
  #next&& = seed%%
END FUNCTION
 
END PROGRAM
 