BBC BASIC for Windows
« IF < less than not working »

Welcome Guest. Please Login or Register.
Apr 5th, 2018, 10: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: IF < less than not working  (Read 684 times)
Wendell
New Member
Image


member is offline

Avatar




PM


Posts: 28
xx IF < less than not working
« Thread started 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 ?
User IP Logged

rtr
Guest
xx Re: IF < less than not working
« Reply #1 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.
User IP Logged

DDRM
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 321
xx Re: IF < less than not working
« Reply #2 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

User IP Logged

rtr
Guest
xx Re: IF < less than not working
« Reply #3 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.
User IP Logged

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