IF Statement

rjandsam

Member
Hi,
in the following statement if location does not=27 the code continues from the serout point, the only way I can get it to run as expected is to change it to the code listed beneath.
I know its probably me but I was just after an explanation as to why.

Rich

if location=27 then gosub homeLCD serout TXLCD, baudLCD64, (#Address," Shut ",Count1,Count2,Count3,Count4,Count5,Count6,Count7,Count8,cr,lf)
return

if location=27 then let location=27 gosub homeLCD serout TXLCD, baudLCD64, (#Address," Shut ",Count1,Count2,Count3,Count4,Count5,Count6,Count7,Count8,cr,lf)
return
endif
 

BESQUEUT

Senior Member
Hi,
in the following statement if location does not=27 the code continues from the serout point, the only way I can get it to run as expected is to change it to the code listed beneath.
I know its probably me but I was just after an explanation as to why.

Rich

if location=27 then gosub homeLCD serout TXLCD, baudLCD64, (#Address," Shut ",Count1,Count2,Count3,Count4,Count5,Count6,Count7,Count8,cr,lf)
return

if location=27 then let location=27 gosub homeLCD serout TXLCD, baudLCD64, (#Address," Shut ",Count1,Count2,Count3,Count4,Count5,Count6,Count7,Count8,cr,lf)
return
endif
I do not understand why the second example run...
It is always better to write multi lines :
Code:
[color=Black]MySubroutine:
   [/color][color=Blue]if [/color][color=Black]location[/color][color=DarkCyan]=[/color][color=Navy]27 [/color][color=Blue]then
      gosub [/color][color=Black]homeLCD
      [/color][color=Blue]serout [/color][color=Black]TXLCD, baudLCD64, [/color][color=Blue]([/color][color=Black]#Address,[/color][color=Red]" Shut "[/color][color=Black],Count1,Count2,Count3,Count4,Count5,Count6,Count7 ,Count8,[/color][color=Blue]cr[/color][color=Black],[/color][color=Blue]lf)
   endif
return[/color]
 

rjandsam

Member
Hi Besqueut,

You are correct it is my poor layout skills it compiles fine when laid out correctly.

Thank you

Rich
 

AllyCat

Senior Member
Hi,

I do not understand why the second example run...
Probably because of the inclusion of the LET (which basically behaves as a "Newline" character) and the ENDIF. The {LET} location = 27 is pointless because that is already the value (it just gave you something to attach the LET to).

But only the OP can know if the cr , lf is doing anything useful. Or whether the RETURN needs to be before or after the ENDIF (Yes it probably will make a difference), or maybe somewhere else?

Cheers, Alan.
 

hippy

Technical Support
Staff member
It all comes down to the -

if location=27 then gosub homeLCD serout

That is all treated as a single line IF statement, so no END IF required or allowed.

Inserting a LET or pretty anything else where the GOSUB is stops it being a single line IF statement, turns it into a multi-line IF statement which requires an END IF.

The compiler tries to figure out what is meant when the code has not been written how it arguably should be but that is influenced by exactly what is written. Things which may look like they will be handled the same way often will not be seen the same way by the compiler. Using a separate line for each statement, adding colons when multiple commands are used on the same line would have made the compiler see what one wanted -

Code:
if location=27 then gosub homeLCD 
serout TXLCD, baudLCD64, (#Address," Shut ",Count1 ... )
return
Code:
if location=27 then 
  gosub homeLCD 
  serout TXLCD, baudLCD64, (#Address," Shut ",Count1 ... )
  return
endif
 

BESQUEUT

Senior Member
I would avoid writing a RETURN in an IF statement...
When condition is not reached, the GOSUB statement is never unstacked, and that may cause unpredictable behaviour...
Just after the GOTO statement, this is a good way to write unreadable code...
 

hippy

Technical Support
Staff member
LET is entirely optional. Some people like it, some don't.

It tends to be used in teaching where it makes sense to consider each line a command with the first word describing what sort of command the line is.
 

techElder

Well-known member
I like LET, because its a good place to hide all those things people can't find. :D

Code:
MAX 	limit value to be no greater than a maximum value
MIN 	limit value to be no lower a minimum value
AND (or &) 	bitwise AND
OR (or |) 	bitwise OR (typed as SHIFT + \ on UK keyboard)
XOR (or ^) 	bitwise XOR (typed as SHIFT + 6 on UK keyboard)
NAND 	bitwise NAND
NOR 	bitwise NOR
ANDNOT (or &/) 	bitwise AND NOT (NB this is not the same as NAND)
ORNOT (or |/) 	bitwise OR NOT (NB this is not the same as NOR)
XORNOT (or ^/) 	bitwise XOR NOT (same as XNOR)
<< 	shift left
>> 	shift right
*/ 	multiply (returns middle word of result)
SIN 	sine of angle (0 to 65535) in degrees (value * 100 is returned)
COS 	cosine of angle in degrees (value * 100 is returned)
SQR 	square root
INV 	invert
NCD 	encoder (2n power encoder)
DCD 	decoder (2n power decoder)
BINTOBCD 	convert binary value to BCD
BCDTOBIN 	convert BCD value to binary
REV 	reverse a number of bits
DIG 	returns a single digit of a value
NOB 	count number of set bits (X2 only)
ATAN 	calculate the arctan of a value (result 0-45 degrees) (X2 only)
 

BESQUEUT

Senior Member
I like LET, because its a good place to hide all those things people can't find. :D

Code:
MAX 	limit value to be no greater than a maximum value
MIN 	limit value to be no lower a minimum value
AND (or &) 	bitwise AND
OR (or |) 	bitwise OR (typed as SHIFT + \ on UK keyboard)
XOR (or ^) 	bitwise XOR (typed as SHIFT + 6 on UK keyboard)
NAND 	bitwise NAND
NOR 	bitwise NOR
ANDNOT (or &/) 	bitwise AND NOT (NB this is not the same as NAND)
ORNOT (or |/) 	bitwise OR NOT (NB this is not the same as NOR)
XORNOT (or ^/) 	bitwise XOR NOT (same as XNOR)
<< 	shift left
>> 	shift right
*/ 	multiply (returns middle word of result)
SIN 	sine of angle (0 to 65535) in degrees (value * 100 is returned)
COS 	cosine of angle in degrees (value * 100 is returned)
SQR 	square root
INV 	invert
NCD 	encoder (2n power encoder)
DCD 	decoder (2n power decoder)
BINTOBCD 	convert binary value to BCD
BCDTOBIN 	convert BCD value to binary
REV 	reverse a number of bits
DIG 	returns a single digit of a value
NOB 	count number of set bits (X2 only)
ATAN 	calculate the arctan of a value (result 0-45 degrees) (X2 only)
+1 .
 

Pongo

Senior Member
LET is entirely optional. Some people like it, some don't.

It tends to be used in teaching where it makes sense to consider each line a command with the first word describing what sort of command the line is.
Right! Most languages that kids will use in future life use different operators for assignment "=" and equality "==", it's good to start them out thinking that way.
 
Top