REPEAT..... UNTIL Loops - Tutorial 003
Type in or Select/Copy/Paste the following small program into the BB4W
editing window:
MODE8
OFF
PRINT' "Two Dice"'
REPEAT
d1=RND(6)
d2=RND(6)
sum=d1+d2
COLOUR RND(15)
PRINTTAB(12);d1;" + ";d2;" =
";sum
delay=INKEY(100)
UNTIL sum=12
PRINT ' "Yippee"
PRINT ' ' ' "Press SPACE to go again"
halt=GET
RUN
NOTES: follow the asterisks.
(For more information on each command see the BB4W Help File on the editing
window).
MODE8 /*16 colour mode
OFF /* Switches off the flashing cursor
PRINT' "Two Dice" ' /* the apostrophe after
PRINT and at the end is equivalent shorthand to another PRINT statement
REPEAT /* Everthing between here and the
UNTIL statement is repeated until the stated condition after UNTIL comes
true.
d1=RND(6) /* RND(6) produces
randomly the whole numbers 1,2,3,4,5,6
d2=RND(6)
sum=d1+d2
COLOUR RND(15) /* COLOUR n selects the
text colour
PRINTTAB(12);d1;" + ";d2;"
= ";sum /* the semicolons prevent spaces occurring - try removing one and
see the result
delay=INKEY(100) /*INKEY waits
here for 100 centiseconds i.e. for one second otherwise it all ahppens much
too quickly
UNTIL sum=12 /* waits for two sixes
to be thrown
PRINT ' "Yippee"
PRINT ' ' ' "Press SPACE to go again"
halt=GET /*GET waits for a key to be pressed
RUN /* After the key is pressed the program
is loaded again and re-run. (The MODE statement clears the screen)
Exercise:
Modify the program to throw 3 dice and halt only when the score is 18.
Email your program and I'll publish it on this site.
Next Tutorial
Richard Weston's Homepage