Is there a way to simulate a Repeat/Until loop in a BASIC language without it, and without using a GOTO statement?
For BASIC languages with command separators, can this be done on a single line without an IF/THEN statement?
Retrocomputing Stack Exchange is a question and answer site for vintage-computer hobbyists interested in restoring, preserving, and using the classic computer and gaming systems of yesteryear. It only takes a minute to sign up.Sign up to join this community
What we do is create a FOR/NEXT loop with the special property that we set the STEP rate to zero, so that it will run forever.
To exit the loop, we set the counter equal to the ending value of the FOR loop.
10 FOR X = 0 TO 1 STEP 0 20 INPUT "WHAT IS YOUR NAME?"; X$ 30 IF LEN(X$) > 0 THEN X = 1 40 NEXT X
For this to work on a single line, the BASIC needs to support boolean values as a number, so that we can use Boolean Math in place of the IF/THEN statement.
Support for this varies from one BASIC to the next, with some having a True value equal to 1 and others with True equal to -1. The following code checks to see which way this works and compensates for it.
10 N = 1 : IF (1 = 1) = -1 THEN N = -1 20 FOR X = 0 TO 1 STEP 0 : INPUT "WHAT IS YOUR NAME?"; X$ : X = (LEN(X$) > 0) * N : NEXT X
(Tested in Apple II Microsoft/FP BASIC).
EDIT: An even shorter way to do it, that should be compatible across most BASIC languages, as suggested by Jeff Zeitlin:
10 N=NOT 0 :FOR X=0 TO 1 STEP 0 :INPUT "WHAT IS YOUR NAME?"; X$ :X=(LEN(X$)>0)*N :NEXT X