BBC BASIC for Windows
« New to BBC Basic - Need help with Compound IF »

Welcome Guest. Please Login or Register.
Apr 5th, 2018, 9:53pm



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

If you require a dump of the post on your message board, please come to the support board and request it.


Thank you Conforums members.

BBC BASIC for Windows Resources
Online BBC BASIC for Windows documentation
BBC BASIC for Windows Beginners' Tutorial
BBC BASIC Home Page
BBC BASIC on Rosetta Code
BBC BASIC discussion group
BBC BASIC for Windows Programmers' Reference

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: New to BBC Basic - Need help with Compound IF  (Read 507 times)
AndrewLindsay
New Member
Image


member is offline

Avatar




PM


Posts: 4
xx New to BBC Basic - Need help with Compound IF
« Thread started on: May 3rd, 2016, 02:55am »

Dear Group,

I'm new to BBC basic. I've found a couple of samples of things on Rosetta Code using BBC and I'm trying to understand them.

I've purchased a license to be able to support this wonderful language, but I am at a bit of a loss with the following code snippet.

Code:
          IF p% >= 0 IF p% < w% IF q% >= 0 IF q% < h% IF m&(p%,q%) < &80 THEN
            IF p% > x% IF m&(p%,q%) AND 1 EXIT FOR
            IF q% > y% IF m&(p%,q%) AND 2 EXIT FOR
            IF x% > p% IF m&(x%,y%) AND 1 EXIT FOR
            IF y% > q% IF m&(x%,y%) AND 2 EXIT FOR
          ENDIF
 


What does this look like in expanded/multiline IF statements. I really struggle to see what is being achieved here, and would like to be able to break this open into a more readable version.

Or, if this is already 'readable' could somebody please explain in pseudocode exactly how this little block is interpreted.

I'm sort of guessing that 'ALL' the conditions in the outer IF statements (the compound If statement on the first line) need to be met.

Best regards

Andrew
User IP Logged

AndrewLindsay
New Member
Image


member is offline

Avatar




PM


Posts: 4
xx Re: New to BBC Basic - Need help with Compound IF
« Reply #1 on: May 3rd, 2016, 03:21am »

Back again, while I'm looking at things I don't quite understand, what do the following statements do?
Code:
      w% = DIM(m&(),1)
      h% = DIM(m&(),2)
 


Any assistance is appreciated.
User IP Logged

michael
Senior Member
ImageImageImageImage


member is offline

Avatar




PM


Posts: 335
xx Re: New to BBC Basic - Need help with Compound IF
« Reply #2 on: May 3rd, 2016, 03:22am »

Hi Andrew,
The snippet was probably part of a FOR ... NEXT loop and shows a way to exit it prematurely depending on the conditions shown.
Quote:
You must not exit a FOR...NEXT loop with a GOTO (see the sub-section on Program Flow Control for more details). You can force a premature end to the loop with the EXIT statement:
FOR I=1 TO 20
X=A^I
IF X>1000 THEN EXIT FOR
PRINT I,X
NEXT


I have worked out some ideas on my forum also, It has links for learning BBC Basic Programming the easy lessons.

http://programming.conforums.com/index.cgi

Feel free to try out my tools and use them to toy with the language.
My latest buttons, text and input tools are now ready to use also (and they are professionally done..)
I am still working on more tools for beginners.
« Last Edit: May 3rd, 2016, 03:27am by michael » User IP Logged

I like making program generators and like reinventing the wheel
AndrewLindsay
New Member
Image


member is offline

Avatar




PM


Posts: 4
xx Re: New to BBC Basic - Need help with Compound IF
« Reply #3 on: May 3rd, 2016, 03:23am »

I think I've answered my first question.

Does this expanded code look correct?
Code:
          IF ((p >= 0) AND (p < w) AND (q >= 0) AND (q < h) AND (m(p,q) < &H80)) THEN
            IF ((p > x) AND (m(p,q) AND 1)) THEN
                EXIT FOR
            END IF
            IF ((q > y) AND (m(p,q) AND 2)) THEN
                EXIT FOR
            END IF
            IF ((x > p) AND (m(x,y) AND 1)) THEN
                EXIT FOR
            END IF
            IF ((y > q) AND (m(x,y) AND 2)) THEN
                EXIT FOR
            END IF
          END IF  


Best regards
Andrew
User IP Logged

michael
Senior Member
ImageImageImageImage


member is offline

Avatar




PM


Posts: 335
xx Re: New to BBC Basic - Need help with Compound IF
« Reply #4 on: May 3rd, 2016, 03:31am »

It appears that there is hexadecimal in the equations, I am guessing for speed. I haven't yet seen EXIT FOR used like that yet as I am still learning. I see no FOR.. NEXT loop.

What are you trying to achieve?
User IP Logged

I like making program generators and like reinventing the wheel
michael
Senior Member
ImageImageImageImage


member is offline

Avatar




PM


Posts: 335
xx Re: New to BBC Basic - Need help with Compound IF
« Reply #5 on: May 3rd, 2016, 03:34am »

I am trying to figure this out correctly
It appears you are missing a FOR ... NEXT loop in your snippet.
Also you are using floating variables. They are ok but are more memory intensive and don't stick with whole numbers.
A variable like a if given 0.26 would retain it and larger integers. a% if given 0.26 would retain 0
Or a% if given 1.25 would retain 1
(whole numbers and less memory for storing the info)
Quote:
A statement which causes a premature exit from a FOR...NEXT, REPEAT...UNTIL or WHILE...ENDWHILE loop.
EXIT FOR causes execution to continue after the matching NEXT statement, EXIT REPEAT causes execution to continue after the matching UNTIL statement and EXIT WHILE causes execution to continue after the matching ENDWHILE statement. Typically you would use EXIT when a situation occurs which necessitates leaving the loop 'part way through':

FOR I% = start% TO finish%
...
IF bool% THEN EXIT FOR
...
NEXT I%
REPEAT
...
IF bool% THEN EXIT REPEAT
...
UNTIL condition%
WHILE condition%
...
IF bool% THEN EXIT WHILE
...
ENDWHILE

In the case of EXIT FOR an optional loop variable can be specified, causing execution to continue after the NEXT statement which matches that variable:
FOR I% = start% TO finish%
FOR J% = first% TO last%
...
IF bool% THEN EXIT FOR I%
...
NEXT
NEXT
REM Execution continues here

You can EXIT from a loop even when inside a nested loop of a different kind:
REPEAT
FOR J% = first% TO last%
...
IF bool% THEN EXIT REPEAT
...
NEXT
UNTIL FALSE
REM Execution continues here

« Last Edit: May 3rd, 2016, 03:41am by michael » User IP Logged

I like making program generators and like reinventing the wheel
AndrewLindsay
New Member
Image


member is offline

Avatar




PM


Posts: 4
xx Re: New to BBC Basic - Need help with Compound IF
« Reply #6 on: May 3rd, 2016, 03:52am »

Yes, there is a FOR..NEXT loop around it. But I wasn't having any problems with that, so I left it out.

This is a snippet from a Maze creating and solving algorithm.
https://rosettacode.org/wiki/Maze_solving
User IP Logged

DDRM
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 321
xx Re: New to BBC Basic - Need help with Compound IF
« Reply #7 on: May 3rd, 2016, 08:11am »

Hi Andrew,

Referring back to your original post
Code:
IF p% >= 0 IF p% < w% IF q% >= 0 IF q% < h% IF m&(p%,q%) < &80 THEN
 

You are allowed to omit the THEN part of the statement (if the statment is all on one line), and you can have multiple IF...THEN statements one after the other on one line. If they don't go onto more lines, then you don't need the ENDIF.

The advantage of using multiple IFs like this (rather than having multiple conditions linked by AND as you suggest below) is that if the first test fails the program will drop through to the next statement (missing out any blocks which are part of the (last) IF statement). That saves time (the other IF statements don't need to be evaluated) AND it can be helpful if some of the subsequent IF tests would crash (for example, the first test might be a flag to indicate that some parameter was not 0 - which could cause a divide by 0 error in a subsequent test).

The disadvantages are that it is harder to read, and you can't use ELSE clauses for any of the IFs except the last one.

If you're a beginner, my advice would be to stick to the normal grammar (include THENs), and don't concatenate the IFs, until you are comfortable that you know what will happen.

I've had only a transient glance at the maze-solving code, so treat this with a pinch of salt...

EXIT FOR allows you to jump out of a block - in this case, the FOR...NEXT loop that this code is embedded in.

The hex expression (&80) that Michael comments on may be there because looking at it hex may make more sense -for example, does each 4 bit element indicate the walls of one cell? I can imagine that &8 might indicate a left wall, &4 a top wall, and &C both, for example. I'm not sure it's intrinsically faster.

Best wishes,

D
User IP Logged

hellomike
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 46
xx Re: New to BBC Basic - Need help with Compound IF
« Reply #8 on: May 3rd, 2016, 8:07pm »

Hi Andrew,

For the remaining question about the DIM().

The array m&() (which represents the maze) has two dimensions. With the assignments:
Code:
      w% = DIM(m&(),1)
      h% = DIM(m&(),2) 


w% will contain the size of the first dimension (1) and h% the size of the second dimension (2).

In other words, the width and the height of the maze.

Hope it makes sense.

Regards,

Mike
User IP Logged

michael
Senior Member
ImageImageImageImage


member is offline

Avatar




PM


Posts: 335
xx Re: New to BBC Basic - Need help with Compound IF
« Reply #9 on: May 7th, 2016, 12:22am »

if you scroll up and see the original program and the correction that was posted, you will see my point about variable use.
p% was changed to p
q% was changed to q
and so on
I know why people prefer to code this way. Its because typing p is easier than p%, simple as that. And adding % or any other symbol would add confusion to a new programmer.
Again, I don't devalue the use of proper variables for speed and efficiency, I am just making a point.
OH and using a variable like p% apparently is faster than p according to Richards post on his forum.
Also if you use p it wont protect you from receiving a decimal result, so that is one thing to consider.
But, say you are dealing with making a program for managing sales, you would want to use p or a variable like it.
Quote:
IF ((p >= 0) AND (p < w) AND (q >= 0) AND (q < h) AND (m(p,q) < &H80)) THEN
IF ((p > x) AND (m(p,q) AND 1)) THEN
EXIT FOR
END IF
IF ((q > y) AND (m(p,q) AND 2)) THEN
EXIT FOR
END IF
IF ((x > p) AND (m(x,y) AND 1)) THEN
EXIT FOR
END IF
IF ((y > q) AND (m(x,y) AND 2)) THEN
EXIT FOR
END IF
END IF
« Last Edit: May 7th, 2016, 12:28am by michael » User IP Logged

I like making program generators and like reinventing the wheel
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls