cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Kyle_Gibson
Level 3

Handling of Boolean Logic Operators [bug?]

Is this a boolean logic bug? Or am I going crazy?

Summary:
The operators || && appear to evaluate EVERY function, regardless of whether they needed to be evaluated or not. This is in stark contrast to nearly every other language.

Here is the code that I've created to come to this conclusion, please try it out and let me know what results you get.

#ifdef WTF_BUG
prototype A(BOOL);
prototype B(BOOL);
prototype C(BOOL);

function A(ret)
begin
MessageBox("A", INFORMATION);
return ret;
end;

function B(ret)
begin
MessageBox("B", INFORMATION);
return ret;
end;

function C(ret)
begin
MessageBox("C", INFORMATION);
return ret;
end;

#endif

function OnBegin()
begin

// This first part is meant to determine whether boolean logic is sane at all in the first place, and also that the functions A B C are operating as expected (input->output)
#ifdef WTF_BUG
if A(TRUE) == TRUE then
MessageBox("true is true", INFORMATION);
endif;

if !A(TRUE) == FALSE then
MessageBox("!true is false", INFORMATION);
endif;

if A(FALSE) == FALSE then
MessageBox("false is false", INFORMATION);
endif;

if !A(FALSE) == TRUE then
MessageBox("!false is true", INFORMATION);
endif;

// this section is meant to test the logic operators

if A(TRUE) == TRUE || B(FALSE) == TRUE || C(TRUE) == TRUE then
MessageBox("should Only see A", INFORMATION);
endif;

if (A(FALSE) == TRUE || B(TRUE) == TRUE) || C(FALSE) == TRUE then
MessageBox("should only see A B", INFORMATION);
endif;

if A(FALSE) == TRUE && B(TRUE) == TRUE && C(TRUE) == TRUE then
MessageBox("Should not see this message", INFORMATION);
else
MessageBox("Should only see A", INFORMATION);
endif;

if A(TRUE) == TRUE && B(FALSE) == TRUE && C(TRUE) == TRUE then
MessageBox("Should not see this message", INFORMATION);
else
MessageBox("Should only see A B", INFORMATION);
endif;

if A(FALSE) == TRUE || B(FALSE) == TRUE || C(FALSE) == TRUE then
MessageBox("Should not see this message", INFORMATION);
else
MessageBox("Should see A B C", INFORMATION);
endif;

if A(FALSE) == TRUE && B(FALSE) == TRUE && C(FALSE) == TRUE then
MessageBox("Should not see this message", INFORMATION);
else
MessageBox("Should only see A", INFORMATION);
endif;

abort;
#endif


In EVERY case, each function is evaluated and then the result from each function is used to determine whether or not the condition is true.

Please tell me I am doing something wrong.
Labels (1)
0 Kudos
(3) Replies
RobertDickau
Flexera Alumni

It is odd, compared to C/C++, that boolean operators don't short-circuit in InstallScript, but that is how they're documented to work; please see the caution in the help topic "Logical Operators (&&, ||, !)".
0 Kudos
Kyle_Gibson
Level 3

Thanks Robert! It's a feature, ha.

Oddly enough that is the very help page that I had open when I went to look for the document you suggested. Apparently my eyes avoid the giant bold emphasis caution block.

It is definitely documented, but could they really do more to make it stand out? 😄
0 Kudos
RobertDickau
Flexera Alumni

Maybe we can get the caution paragraph to blink or something, ha ha?
0 Kudos