Issue when RETURN is on same line as IF statement

Archives Forums/BlitzMax Bug Reports/Issue when RETURN is on same line as IF statement

GreenVertical(Posted 2015) [#1]
There seems to be an issue with the IF statement in a function when the Return statement is on the same line (with the two statements separated by a semicolon).
It seems that the ELSE bit fails to get executed under these conditions.
The below example illustrates the issue.
(working on Windows)

Strict
Local ANum:Int=4; Local BNum:Int=11
Print "ANum " + TestIfBigNumber(ANum)
Print "BNum" + TestIfBigNumber(BNum)

Function TestIfBigNumber:String(n:Int)
Local Result:String
If N>=10 Then Result="Is big." Else Result="Is small"; Return Result
EndFunction

This leads to the output:
ANum Is small
BNum

However with the Return on the next line the code executes correctly

Strict
Local ANum:Int=4; Local BNum:Int=11
Print "ANum " + TestIfBigNumber(ANum)
Print "BNum" + TestIfBigNumber(BNum)

Function TestIfBigNumber:String(n:Int)
Local Result:String
If N>=10 Then Result="Is big." Else Result="Is small"
Return Result
EndFunction

Leads to the output:
ANum Is small
BNumIs big.

Obviously its not a problem as long as one ensures the Return statement is on its own line, but given this issue am wondering if the semicolon statement separator causes problems in other contexts...


Henri(Posted 2015) [#2]
Hi,

what if you put semicolon before else ?

-Henri


GreenVertical(Posted 2015) [#3]
Then the error reappears. The ELSE component fails to get executed.

Strict
Local ANum:Int=4; Local BNum:Int=11
Print "ANum " + TestIfBigNumber(ANum)
Print "BNum" + TestIfBigNumber(BNum)

Function TestIfBigNumber:String(n:Int)
Local Result:String
If N>=10 Then Result="Is big." ; Else Result="Is small" Return Result
EndFunction

Output:
ANum Is small
BNum


col(Posted 2015) [#4]
You're right in that it fails to get executed, that's what's supposed to happen.

Else Result="Is small" Return Result


is all part of the Else condition even with the semi colon before the Return. You're expecting the Return to NOT be part of the If.Then.Else ?
Single lines statements can cause all kinds of headaches including a lack of clarity of your code.


GreenVertical(Posted 2015) [#5]
I just assumed that the ';' and a carriage return were equivalent as far as the complier was concerned. It surprised me that it produced this behaviour - as this suggests that they are treated differently. I think I will just have one statement per line in future and avoid use of the ';' altogether.


col(Posted 2015) [#6]
Yeah I understand what you mean. I was caught out many a time. The semi colon can be used to separate statements which, as you can also see, isn't always needed, but it's mainly used to join multiple statements within the context of a decision or condition - a good example is what you have above :O)


TomToad(Posted 2015) [#7]
Think of a single line if statement as a multi line with the space or semi colon between commands as an implied return and the actual return as an implied End If. So your example becomes
If N>=10 Then 
    Result="Is big." 
Else 
    Result="Is small"
    Return Result
End If

That was the way that early BASIC worked. There wasn't an actual code block, so if you needed more than one command to be executed in an If, then you had to put it all on one line.


dw817(Posted 2016) [#8]
I never use the ";" GreenVertical. In fact, when I see it in code I copy to look at, I start to assume the code is BlitzBASIC as that is their key for remark.

I use the ; a bit now, but only if the code is especially legible with its use.

Now in an IF statement a ; after the first statement is NOT an ELSE (any way you do it). No, it is an ADDITION to the code IF the condition is true.

Forces you to write legible code; and I thought my code was messy ...

I stick with:
If a=0 Then b=0
If b=0
  c=0
  d=0
Endif
In other words, if I can have =ONE= command that meets TRUE with IF, I'll use THEN.

If not, I use If and Endif taking no less than 4-lines of code. That may seem extraneous, but it's a lot easier to read later.

I haven't seen If and Endif on the same line until now. I think you're pushing it, GV. :)