can i haveitem and compare the result in same statement?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 687
Joined: Sun Feb 14, 2010 8:11 pm

can i haveitem and compare the result in same statement?

Post by Bugala » Mon Mar 19, 2018 8:56 pm

What I am doing is following, but is it safe?

Code: Select all

function maybehasargmaybenot(...)
If HaveItem(arg, 0) = true AND if arg[0] = "mytext"
   stuff
else
   otherstuff
endif
endfunction
Possible Problem here is the two IF statements.

If there is arg[0], then there is no problem at all. However, in case there isnt arg[0], which is many times the case in my real code, then I am afraid if there comes a problem.

First it checks if the item exists using "HaveItem", that works fine always, but then comes the AND part, in which it is actually trying the same item as HaveItem just checked, so in case that doesnt exist, that would mean crash.

But how does Hollywood work?

If i Have IF statement of IF state1 AND state2 AND state3 AND state4.

Then does Hollywood check all those state1, state2, state3 and state4, or does Hollywood start by checking first state1, then moving to state2 to state3, to state4?

Or does it do so that it first checks state1, and if state1 fails already, then it never goes on to check state2?

And also, can i trust it will always first check the state1, and not start from state3 for example?

xabierpayet
Posts: 160
Joined: Fri Feb 24, 2012 10:34 am

Re: can i haveitem and compare the result in same statement?

Post by xabierpayet » Tue Mar 20, 2018 12:21 am

function maybehasargmaybenot(...)
If HaveItem(arg, 0) = true AND arg[0] = "mytext"
stuff
else
otherstuff
endif
endfunction

2 if in the same line?... try only with one, you can use 500 AND/OR if you likes

Bugala
Posts: 687
Joined: Sun Feb 14, 2010 8:11 pm

Re: can i haveitem and compare the result in same statement?

Post by Bugala » Tue Mar 20, 2018 3:25 pm

point is not that can i use several IFs with AND, but point is that there are situations when the second IF crashes the program if it is executed. However, this crash situation only happens in situations when first IF fails. Hence I need to know if second IF is ever executed if first IF fails already.

bitRocky
Posts: 80
Joined: Fri Nov 14, 2014 7:01 pm

Re: can i haveitem and compare the result in same statement?

Post by bitRocky » Tue Mar 20, 2018 8:29 pm

I did some simple test, and the second statement is only called/checked if needed.

So, if you do something like this:

Code: Select all

If False And TestFunc()
    <do something>
EndIf
The TestFunc() isn't called, because the first part is False and therefore the statement can't be True.

Here is the small test script:

Code: Select all

a = 5
Function Chk()
	DebugPrint("Chk() called")
	Return(True)
EndFunction

DebugPrint("Test1: If ("..a.." = 2) And Chk()")
If (a = 2) And Chk()
	DebugPrint("Yes")
Else
	DebugPrint("No")
Endif

DebugPrint("\nTest2: If ("..a.." = 5) And Chk()")
If (a = 5) And Chk()
	DebugPrint("Yes")
Else
	DebugPrint("No")
Endif

Post Reply