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
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
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.