LB Booster
IDE and Compiler >> Compiler >> I wrote a wrong code! http://lbb.conforums.com/index.cgi?board=compiler&action=display&num=1439092445 I wrote a wrong code!
Post by SarmedNafi on Aug 9th, 2015, 03:54am
The following code which I wrote is wrong.
But it is accepted by LBB and run it in wrong way, so a wrong result was displayed.
Code:
'code------
A=4
if A=2 then else print "3"
print "2"
else
if A=4 then
print "4"
end if
end if
wait
'code-----
I hope Richard will take care of it at next release.
With all thanks to Richard Regards, Sarmed
Re: I wrote a wrong code!
Post by Richard Russell on Aug 9th, 2015, 07:11am
I hope Richard will take care of it at next release.
It would be quite difficult I think for a compiler to catch errors like this where the error stretches over multiple statements and is more due to faulty logic rather than a syntax error.
I believe this is why it is suggested that when constructs such as IF... THEN... ELSE... are used, that you do not mix the use of single line statements with multiple line ones. IE either use IF... THEN... ELSE... on one line or use IF... THEN ... ELSE ... END IF
but do not mix them. For one thing, it will make your program easier to read and debug later and I think the separate line form is to be preferred for this reason.
In your particular example, SELECT CASE might be a better choice instead of using nested IF... THEN... ELSE... constructs.
Code:
A=4
SELECT CASE A
CASE 2
PRINT "2"
CASE 3
PRINT "3"
CASE 4
PRINT "4"
CASE else
PRINT "something else"
END SELECT