notwhoyouthink Posted December 22, 2017 Share Posted December 22, 2017 (edited) 1 Z=102 FOR A = 1 TO Z3 IF TEST-CONDITION=1 THEN 4 ELSE 74 Z=Z+1 !MODIFY THE LOOP LIMIT TO RUN A EXTRA TIME 5 DO SOMETHING 6 GOTO 87 DO SOMETHING DIFFERENT 8 NEXT A What i want is a loop that changes itself to run extra times if needed, based on whether a certain conditional variable is 1 or 0.I am fairly certain something like this can be done in languages like C, but is it possible in Basic/XB? Edited December 22, 2017 by notwhoyouthink Quote Link to comment Share on other sites More sharing options...
+OLD CS1 Posted December 22, 2017 Share Posted December 22, 2017 The ending constraint is evaluated at the time the FOR statement is executed. I am pretty certain the stepping value is, as well. However, you can manipulate the counter variable. For instance: 10 Z=10 20 FOR A=1 TO Z 25 PRINT A 30 A=A+2 40 NEXT A This will print 1, 4, 7, then 10. And this: 10 Z=10 20 FOR A=1 TO Z 30 PRINT A 40 IF A<>5 OR Z=0 THEN 60 50 A=A-4 :: Z=0 60 NEXT A Will print 1,2,3,4,5,2,3,4,5,6,7,8,9,10 EDIT: Yes, the stepping value is evaluated at the time of FOR execution, as well: 10 Z=20 15 I=2 20 FOR A=1 TO Z STEP I 25 I=1 30 PRINT A 40 IF A<>5 OR Z=0 THEN 60 50 A=A-4 :: Z=0 60 NEXT A Prints 1,3,5,3,5,7,9,11,13,15,17,19 2 Quote Link to comment Share on other sites More sharing options...
+OLD CS1 Posted December 22, 2017 Share Posted December 22, 2017 You could manipulate a FOR/NEXT loop to be a DO/WHILE loop like this: 10 FOR A=-1 TO 0 20 REM DOING SOMETHING 30 REM DOING SOMETHING MORE 40 A=TESTCONDITION 50 NEXT A 0 (zero) is "false," and any non-zero value is "true," but the evaluation of a condition is -1 for "true." Therefore, setting the counter variable in 40 to a true/false -1/0 condition (more like a DO/UNTIL) will control the loop without the use of IF/THEN, GOTO, or ON GOTO. 1 Quote Link to comment Share on other sites More sharing options...
Willsy Posted December 22, 2017 Share Posted December 22, 2017 (edited) 1 Z=10 2 FOR A = 1 TO Z 3 IF TEST-CONDITION=1 THEN 4 ELSE 7 4 Z=Z+1 !MODIFY THE LOOP LIMIT TO RUN A EXTRA TIME 5 DO SOMETHING 6 GOTO 8 7 DO SOMETHING DIFFERENT 8 NEXT A What i want is a loop that changes itself to run extra times if needed, based on whether a certain conditional variable is 1 or 0. I am fairly certain something like this can be done in languages like C, but is it possible in Basic/XB? No need to modify z. For an extra blast around the loop just decrement a by one. Though what you have done looks okay to me. Both a and z will be evaluated by NEXT. Edited December 22, 2017 by Willsy Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.