BBC BASIC for Windows
Programming >> BBC BASIC language >> IF < less than not working
http://bb4w.conforums.com/index.cgi?board=language&action=display&num=1395204139

IF < less than not working
Post by Wendell on Mar 19th, 2014, 04:42am

(DIV)
mun=RND(10)
ned=RND(10)
IF mun<ned THEN (DIV)
PRINT mun
PRINT ned


WHY does the IF statement not working ?
Re: IF < less than not working
Post by rtr on Mar 19th, 2014, 09:35am

on Mar 19th, 2014, 04:42am, Wendell wrote:
WHY does the IF statement not working ?

You forgot the GOTO:

Code:
      IF mun<ned THEN GOTO (DIV) 

I know we keep telling you not to use GOTOs, but you can't simply omit them! This is the way to avoid the GOTO:

Code:
      REPEAT
        mun=RND(10)
        ned=RND(10)
      UNTIL mun>=ned
      PRINT mun
      PRINT ned 

I'm sure you will agree that it's easier to understand than your version, the automatic indentation emphasises the structure, and there are no GOTOs!

As has been recommended previously you really should work through the Beginners' Tutorial to learn the basics; find it in the Help menu or here:

http://bb4w.com/tutorial/

Richard.

Re: IF < less than not working
Post by DDRM on Mar 21st, 2014, 09:54am

Hi Wendell,

Even with the GOTO your code still fails, because the label you have chosen (DIV) is a reserved keyword, which causes problems for the interpreter. Note that it is coloured yellow rather than blue in the IDE. If you change it to Div it will work.... if you insist on using GOTOs! :-)

But in this particular case, wouldn't SWAP be a better option, even than Richard's REPEAT...UNTIL?

Code:
      mun=RND(10)
      ned=RND(10)
      IF mun<ned THEN SWAP mun,ned
      PRINT mun
      PRINT ned
 


Hope that's helpful.

D


Re: IF < less than not working
Post by rtr on Mar 21st, 2014, 10:58am

on Mar 21st, 2014, 09:54am, DDRM wrote:
Code:
      mun=RND(10)
      ned=RND(10)
      IF mun<ned THEN SWAP mun,ned
      PRINT mun
      PRINT ned 

The reason I'm not keen on that solution is that it cannot be generalised. For example suppose the OP decides that he wants 'ned' to be less than 'mun', rather than less than or equal to 'mun'. Making the following modification to your code doesn't work:

Code:
      IF mun<=ned THEN SWAP mun,ned 

Instead it would be necessary to return to the REPEAT...UNTIL solution to ensure 'mun' and 'ned' are unequal.

Of course it all depends on whether that change is a likely requirement or not, but assuming the program generates fractions ('ned' being the denominator and 'mun' the numerator) it doesn't seem too improbable to me.

Richard.