Jump to content
IGNORED

Creating a FOR ... TO loop that can change its TO limit?


notwhoyouthink

Recommended Posts

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?

Edited by notwhoyouthink
Link to comment
Share on other sites

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

  • Like 2
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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 by Willsy
Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...