Break/continue in BASIC loops?

Discuss all aspects of programming here. From 8-bit through to modern architectures.
User avatar
Elminster
Posts: 1632
Joined: Wed Jun 20, 2012 8:09 am
Location: Essex, UK

Break/continue in BASIC loops?

Postby Elminster » Sat Jun 03, 2017 8:04 am

In other languages, and possibly other versions of BASIC, you can break or continue when inside a loop. Any bright ideas for doing this in Basic?

You can get a delayed break using a Boolean on an Until or setting the for/next value to the exit value, but no immediate break. Continue I can only think of gotos but not only is that bad but in a large program where it gets renumbered a lot it isn't that maintainable.

The reason I ask is when you have lots of loops with lots of ifs (again Basic 2-4 doesn't allow elseif or multi line ifs), the loops get a bit slow and you might have hit your criteria on first if and have another 10 to trawl through.

Do that a few thousand times and it all adds up.

dp11
Posts: 671
Joined: Sun Aug 12, 2012 8:47 pm

Re: Break/continue in BASIC loops?

Postby dp11 » Sat Jun 03, 2017 8:24 am

If condition then next ?

User avatar
lurkio
Posts: 1148
Joined: Tue Apr 09, 2013 11:30 pm
Location: Doomawangara
Contact:

Re: Break/continue in BASIC loops?

Postby lurkio » Sat Jun 03, 2017 11:40 am

Can you try and squeeze many IFs onto one line?:

Code: Select all

FOR ...

IF condition1 THEN action1:NEXT ELSE IF condition2 THEN action2:NEXT ELSE ...


:?:

User avatar
ctr
Posts: 93
Joined: Wed Jul 16, 2014 2:53 pm

Re: Break/continue in BASIC loops?

Postby ctr » Sat Jun 03, 2017 5:48 pm

If you exit a loop with a GOTO then the loop remains active. The terminator of the next loop will actually jump back to the previous still-active loop. An example might be clearer:

Code: Select all

   10FORI%=1TO20
   20PRINT"I ";I%
   30PROCLOOP
   40NEXT
   50END
   60DEFPROCLOOP
   70FORJ%=1TO6
   80PRINT"J ";J%
   90IF J%=3GOTO110
  100NEXT
  110ENDPROC

>RUN

I 1
J 1
J 2
J 3
J 4
J 5
J 6

No PROC at line 110


The J% loop exits when J%=3 but the J% loop is still active. So the NEXT on the I% loop jumps back to the J% loop! This is probably not what you want.

EDIT: Though you can rescue this example by changing line 40 to "NEXT I%".
Last edited by ctr on Sat Jun 03, 2017 6:38 pm, edited 1 time in total.

User avatar
1024MAK
Posts: 6676
Joined: Mon Apr 18, 2011 4:46 pm
Location: Looking forward to summer in Somerset, UK...

Re: Break/continue in BASIC loops?

Postby 1024MAK » Sat Jun 03, 2017 6:17 pm

ZX Spectrum BASIC maintains two system variables, one is the current line number, and the other is the current statement being processed on that line. When BREAK is pressed, execution of the BASIC program stops at the end of the current statement and you are returned to the command line.
No variables are affected by this. If you then enter Continue, program execution continues (based on the two system variables) at the next command.
However, with some commands (cassette tape and printer commands), or where an error caused program execution to stop, Continue repeats the statement that was interrupted in the program.

Is this the kind of thing you mean?

Mark
For a "Complete BBC Games Archive" visit www.bbcmicro.co.uk NOW!
BeebWiki‬ - for answers to many questions...

User avatar
lurkio
Posts: 1148
Joined: Tue Apr 09, 2013 11:30 pm
Location: Doomawangara
Contact:

Re: Break/continue in BASIC loops?

Postby lurkio » Sat Jun 03, 2017 6:26 pm

I think Elminster was referring to the "continue" statement as found in C and other languages:

https://en.wikipedia.org/wiki/Control_flow#Continuation_with_next_iteration

:?:

User avatar
Elminster
Posts: 1632
Joined: Wed Jun 20, 2012 8:09 am
Location: Essex, UK

Re: Break/continue in BASIC loops?

Postby Elminster » Sat Jun 03, 2017 7:30 pm

[quote="ctr"]If you exit a loop with a GOTO then the loop remains active. The terminator of the next loop will actually jump back to the previous still-active loop. An example might be clearer:
[/code]

Think you misunderstood, I never said to exit a loop with a goto, that wouldnt be a continue but a break.

i.e.

10 Repeat
20 do something goto 40
30 do something goto 40
40 somethign
50 until blah

so above skips X lines.

User avatar
Elminster
Posts: 1632
Joined: Wed Jun 20, 2012 8:09 am
Location: Essex, UK

Re: Break/continue in BASIC loops?

Postby Elminster » Sat Jun 03, 2017 7:31 pm

lurkio wrote:I think Elminster was referring to the "continue" statement as found in C and other languages:

https://en.wikipedia.org/wiki/Control_flow#Continuation_with_next_iteration

:?:


Yep. Continue as in skip the rest of the loop and start the next iteration.

User avatar
Elminster
Posts: 1632
Joined: Wed Jun 20, 2012 8:09 am
Location: Essex, UK

Re: Break/continue in BASIC loops?

Postby Elminster » Sat Jun 03, 2017 7:33 pm

lurkio wrote:Can you try and squeeze many IFs onto one line?:

Code: Select all

FOR ...

IF condition1 THEN action1:NEXT ELSE IF condition2 THEN action2:NEXT ELSE ...


:?:


Unfortunately a) you can only have max line length, and b) it becomes completely unreadable. i.e. confusing.

I have if statements getting on for half max line length already, and then 10 of those. Just looks like spegatti. Probably faster but so unreadable I might as well do it in assembly :)

Edit: Aware of the Next skip bit from reading the 'What doesnt ABC BASIC Comiler not like' Doc, and that was one of the things (i.e. one nexxt per for) but really the issue for me is being able to do a true break or continue (like you do in C for exmaple) within a Repeat/Until. Would make code sooo much faster, well in theory anyway.

User avatar
1024MAK
Posts: 6676
Joined: Mon Apr 18, 2011 4:46 pm
Location: Looking forward to summer in Somerset, UK...

Re: Break/continue in BASIC loops?

Postby 1024MAK » Sat Jun 03, 2017 8:06 pm

I may be being a bit slow here, but...

To exit a FOR - NEXT loop early, change the counter variable to a value greater than the FOR condition.
To start the loop again from a IF - THEN, add an extra NEXT. Or use a GOTO the line with the NEXT.

But if you need speed, either rewrite the program to do as much testing as possible before entering the loop, or if you really need all that testing inside the loop, use machine code.

Mark
For a "Complete BBC Games Archive" visit www.bbcmicro.co.uk NOW!
BeebWiki‬ - for answers to many questions...

User avatar
Elminster
Posts: 1632
Joined: Wed Jun 20, 2012 8:09 am
Location: Essex, UK

Re: Break/continue in BASIC loops?

Postby Elminster » Sat Jun 03, 2017 8:18 pm

1024MAK wrote:I may be being a bit slow here, but...

To exit a FOR - NEXT loop early, change the counter variable to a value greater than the FOR condition.
To start the loop again from a IF - THEN, add an extra NEXT. Or use a GOTO the line with the NEXT.

But if you need speed, either rewrite the program to do as much testing as possible before entering the loop, or if you really need all that testing inside the loop, use machine code.

Mark


Yes fairly sure I mentioned most of that.

a) Reseting counter to next value mentioend in start.
b) I know of adding extra NEXTs but forgot to put it in, but Lurkio mentioned it in a post
c) Goto's cant be used as explained in first post. i.e. bad and constant renumbering of programs screws it up
d) rewriting in Assembly means it is not in BASIC, and kind of a shame
e) cant test before loop, need to test in each iteration as every condition may apply on every iteration of the loop

It is all quite feasible to do in BASIC as most other high level languages/scripting supports it. Well C, Python, PHP etc. A pity you cant. Wondering whehter a remake of the BASIC Extensions is the way to go.

Edit: Just making sure I am wrestling the most possible from BASIC4

User avatar
Elminster
Posts: 1632
Joined: Wed Jun 20, 2012 8:09 am
Location: Essex, UK

Re: Break/continue in BASIC loops?

Postby Elminster » Sat Jun 03, 2017 8:26 pm

Weird sense of De Ja Vu. I wonder if we have had this conversation before.

User avatar
Elminster
Posts: 1632
Joined: Wed Jun 20, 2012 8:09 am
Location: Essex, UK

Re: Break/continue in BASIC loops?

Postby Elminster » Sat Jun 03, 2017 8:34 pm

Basic for windows does have, just looked, exit repeat, exit for and exit while (I.e. Break) but not continue (I.e. Skip rest of run through this iteration and start the next one)

User avatar
1024MAK
Posts: 6676
Joined: Mon Apr 18, 2011 4:46 pm
Location: Looking forward to summer in Somerset, UK...

Re: Break/continue in BASIC loops?

Postby 1024MAK » Sat Jun 03, 2017 8:48 pm

Well, if you move to the later versions of BASIC or BASIC like languages (I'm thinking of OPL here), you can do more fancy things with DO...UNTIL or WHILE...ENDWH loops including CONTINUE and BREAK.
SuperBASIC on the QL has REPeat...NEXT...EXIT...END REPeat. Where NEXT jumps to the statement after the REPeat and EXIT terminates the loop.
GFA BASIC (Atari ST and Amiga) also has various loops other than FOR...NEXT.

Keep in mind that BBC BASIC was advanced for 1981. But options for improvement were limited by it living in a 16k byte ROM.

Mark
For a "Complete BBC Games Archive" visit www.bbcmicro.co.uk NOW!
BeebWiki‬ - for answers to many questions...

User avatar
Elminster
Posts: 1632
Joined: Wed Jun 20, 2012 8:09 am
Location: Essex, UK

Re: Break/continue in BASIC loops?

Postby Elminster » Sat Jun 03, 2017 9:01 pm

Trying to stay within BASIC, in my other posts I have already looked at BASIC v on master (effectively 32k I think it was), and the basic extension ROM (additional 16k on top of language)

I am thinking if I did go down the assembly route I would look to 'fix' basic extension ROM. And make it do all I needed. Failed to find manual for this currently.

Language wise if push came to shove I would probably just switch to cross compiling C.

Btw JPH has some bits for tweaking IFs on his site but nothing for Loops that I have found yet. http://mdfs.net/Info/Comp/BBCBasic/ProgTips/

Mean while I shall stick to BASIC, until I finish code.

Other thing that occurs to me which is if you could do goto with labels instead of line numbers. Would be much more useful.

Edit: OPL not familiar with. Just BASIC, C, Python (learning), PHP, shell scripting is my limit.

User avatar
jgharston
Posts: 2658
Joined: Thu Sep 24, 2009 11:22 am
Location: Whitby/Sheffield

Re: Break/continue in BASIC loops?

Postby jgharston » Sat Jun 03, 2017 9:04 pm

Elminster wrote:In other languages, and possibly other versions of BASIC, you can break or continue when inside a loop. Any bright ideas for doing this in Basic?

Rewrite your code so it isn't needed.

Instead of, eg:
10 FOR A=1 TO 10
20 blah blah
30 IF test THEN GOTO 60
40 blah blah
50 NEXT A
60 ...

do:
10 A=1:REPEAT
20 blah blah
40 IF NOT test THEN blah blah
50 A=A+1:UNTIL A>10 OR test
60 ...

Vey often, if you find that you seem to be getting to a point of wondering how to break a programming structure to get something done, then you're more likely to be thinking through the task in the wrong way, and should rethink the task.

If might be more informative if you give some example code of what you're trying to do.

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.25
(C) Copyright J.G.Harston 1989,2005-2015
>_

User avatar
jgharston
Posts: 2658
Joined: Thu Sep 24, 2009 11:22 am
Location: Whitby/Sheffield

Re: Break/continue in BASIC loops?

Postby jgharston » Sat Jun 03, 2017 9:11 pm

I presume that in:
10 Repeat
20 do something goto 40
30 do something goto 40
40 somethign
50 until blah
lines 20 and 30 are conditional, as otherwise line 30 is never going to be executed. Would this be an appropriate example:

10 REPEAT
20 A=RND(10)
30 A=A+1:IF A=1 THEN PRINT "ONE":GOTO 70
40 A=A*2:IF A/2=A DIV 2 THEN PRINT "ODD":A=A+1:GOTO 70
50 IF A=6 THEN PRINT "FIVE":A=7:GOTO 70
60 IF A=2 THEN PRINT "SOME OTHER NUMBER"
70 UNTIL A=7
and you want to skip the intervening code because the variables are changed and falling through would change the terminating condition.

You rethink what you're trying to do:
10 REPEAT
20 A=RND(10)
30 PROCflibble
70 UNTIL A=7
...
120 DEFPROCfiblle
130 A=A+1:IF A=1 THEN PRINT "ONE":ENDPROC
140 A=A*2:IF A/2=A DIV 2 THEN PRINT "ODD":A=A+1:ENDPROC
150 IF A=6 THEN PRINT "FIVE":A=7:ENDPROC
160 IF A=2 THEN PRINT "SOME OTHER NUMBER"
170 ENDPROC

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.25
(C) Copyright J.G.Harston 1989,2005-2015
>_

User avatar
1024MAK
Posts: 6676
Joined: Mon Apr 18, 2011 4:46 pm
Location: Looking forward to summer in Somerset, UK...

Re: Break/continue in BASIC loops?

Postby 1024MAK » Sat Jun 03, 2017 9:17 pm

Elminster wrote:Edit: OPL not familiar with.

OPL - Organiser Programming Language, a BASIC like language, without line numbers, but with lots of modern structures (IF ELSE, ELSEIF, ENDIF, various loops and if you must, GOTO label) as used on Psion organisers like the Series 3, 3a, 3c, 5, 5mx etc...
It is also compiled, so is faster than an interpreted BASIC.

Mark
For a "Complete BBC Games Archive" visit www.bbcmicro.co.uk NOW!
BeebWiki‬ - for answers to many questions...

User avatar
Elminster
Posts: 1632
Joined: Wed Jun 20, 2012 8:09 am
Location: Essex, UK

Re: Break/continue in BASIC loops?

Postby Elminster » Sat Jun 03, 2017 9:18 pm

jgharston wrote:If might be more informative if you give some example code of what you're trying to do.


Was just hypertheical how can you do break and continures in BASIC, while maintaining reability and also not hitting the max line length.

Mainly coming from noticing that the ABC2/RISC BASIC compilers get a 10times speed increase on Repeats/For (but not very good on other things like maths). And wondering how you can speed them up generally without either a) using a compiler or b) resorting to machine code. Even going to Windows BASIC doesn give you everything. On other languages you have elseif, multistatement if, continue/break from loops etc.

User avatar
Elminster
Posts: 1632
Joined: Wed Jun 20, 2012 8:09 am
Location: Essex, UK

Re: Break/continue in BASIC loops?

Postby Elminster » Sat Jun 03, 2017 9:20 pm

1024MAK wrote:
Elminster wrote:Edit: OPL not familiar with.

OPL - Organiser Programming Language, a BASIC like language, without line numbers, but with lots of modern structures (IF ELSE, ELSEIF, ENDIF, various loops and if you must, GOTO label) as used on Psion organisers like the Series 3, 3a, 3c, 5, 5mx etc...
It is also compiled, so is faster than an interpreted BASIC.

Mark


Aha. Okay I know what you are talking about. Been a long time but I did have a psion 3c and an 5MX, dont recall doing much programming on them though. I was just trying to remember in a Beeb context.

User avatar
Elminster
Posts: 1632
Joined: Wed Jun 20, 2012 8:09 am
Location: Essex, UK

Re: Break/continue in BASIC loops?

Postby Elminster » Sat Jun 03, 2017 9:27 pm

I think what I am really trying to get at is. Usually you have a 'This is the Language definition and best practice'. But then you have 'the missing manual'. The manual that tell you the good stuff, like the Adv User Guide but just for BASIC. TEll you all the good stuff. And what you really need to know. I guess being only 16k of Language there isnt much language to have a missing manual for. Or I just havent spotted it yet?


Return to “programming”

Who is online

Users browsing this forum: No registered users and 3 guests