Boolean algebra

Hy guys !

Could anybody bring me the light, please ?

For me, both functions are equivalent. However, the outcome is different for the last call…

My bad ? Any explanation ?

Thanks.

JC

##############################################################
function evaluate_1
{
if (($tst1 -eq ‘a’ -or $tst1 -eq ‘b’)){
Write-Output ‘success_1’;
}
}

function evaluate_2
{
if (!((!$tst1 -eq ‘a’) -and (!$tst1 -eq ‘b’))){
Write-Output ‘success_2’;
}
}

$tst1=‘a’;
evaluate_1
evaluate_2
$tst1=‘b’;
evaluate_1
evaluate_2
$tst1=‘c’;
evaluate_1;
evaluate_2;

###############################################################

success_1
success_2
success_1
success_2
success_2

JCG,
Welcome to the forum. :wave:t3:

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

Guide to Posting Code - Redux <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

They are not.

You misplaced the parenthesis in your second function.

function evaluate_2 {
    if (-not(-not ($tst1 -eq 'a') -and -not ($tst1 -eq 'b'))) {
        'success_2';
    }
}
1 Like

when sharing any code please remember to format it as code.
You can use the “Preformatted Text” button from the toolbar.
Guide to Posting Code

from a quick glance at the code you said “for me both functions are equivalent”. What do you mean by this precisely as both functions are not equivalent.
Your first function is comparing string text.
Your second function is casting the variables as booleans by putting a ! in front of them, and also flipping the boolean so if the variable contains anything it’s treated as $false. At that point it’s no longer about what value $tst1 has, just that it does have value. From looking at your tests it always has value so !$tst1 will always be equivalent to $false.

Same thing for the second half of your condition in evaluate_2: as long as $tst1 has value your -eq comparison will return $false.
so we’ve essentially got:

($false) -and ($false)

false and false equals false, so we can shorten that to just $false.
but then you wrapped that and put another not in front of it

(-not($false))

which evaluates as true.

What part of this is boolean algebra?

1 Like

function evaluate_1
{
if (($tst1 -eq ‘a’ -or $tst1 -eq ‘b’)){
Write-Output ‘success_1’;
}
}

function evaluate_2
{
if (!((!$tst1 -eq ‘a’) -and (!$tst1 -eq ‘b’))){
Write-Output ‘success_2’;
}
}

function evaluate_3
{
if (!(!($tst1 -eq ‘a’) -and !($tst1 -eq ‘b’))){
Write-Output ‘success_3’;
}
}
<##>
$tst1=‘a’;
evaluate_1
evaluate_2
evaluate_3
<##>
$tst1=‘b’;
evaluate_1
evaluate_2
evaluate_3
<##>
$tst1=‘c’;
evaluate_1;
evaluate_2;
evaluate_3;

success_1
success_2
success_3
success_1
success_2
success_3
success_2

This time it’s ok…
Thanks for your quick reply, and sharing your skill :wink:

“What part of this is boolean algebra?”

I thought it was. If not, feel free to tell me what it is :wink: You’re very welcome.

Nevertheless, thank you greyOut and Olaf for clarifying…


function evaluate_1
{
    if (($tst1 -eq 'a' -or $tst1 -eq 'b')){
        Write-Output 'success_1';
    }
}

function evaluate_2
{
    if (!((!$tst1 -eq 'a') -and (!$tst1 -eq 'b'))){
        Write-Output 'success_2';
    }
}

function evaluate_3
{
    if (!(!($tst1 -eq 'a') -and !($tst1 -eq 'b'))){
        Write-Output 'success_3';
    }
}
<##>
$tst1='a';
evaluate_1
evaluate_2
evaluate_3
<##>
$tst1='b';
evaluate_1
evaluate_2
evaluate_3
<##>
$tst1='c';
evaluate_1;
evaluate_2;
evaluate_3;



success_1
success_2
success_3
success_1
success_2
success_3
success_2

Sorry… Preformatted Text :wink:

You know, you can edit your existing post and fix the formatting. You didn’t have to create a new one. :smirking_face:

And BTW: I highly recommend to read

And BTW #2: Your function evaluate_2 is still wrong … or at least it’s not doing what you probably think it does. You may carefully read Courtneys reply again. :point_up:

oh I should have clarified I have no idea what boolean algebra is. I looked it up and after a brief read I think you’re correct, what you’re trying to do is boolean algebra, but evaluate_2 is still wrong.

function evaluate_2
{
    if (!((!$tst1 -eq 'a') -and (!$tst1 -eq 'b'))){
        Write-Output 'success_2';
    }
}

When you put ! (-not operator) in front of a variable definition it simply converts that object to a boolean. Example

PS> $tst1 = 'a'
PS> $tst1
a
PS> !$tst1
false

$tst1 is a string object. Casting it explicitly as a boolean would simply look at it and say “is there value here?” and return true. The opposite, from the -not/! operator would return false.
In your function however you’re trying to do a comparison of the value of $tst1 but by putting the -not operator right up against your variable you’ve essentially changed your comparison to false -eq 'b' which will never be true.

I’m not sure I know the exact outcome you’re trying to go for in evaluate_2 but would a -ne comparison be easier?

function evaluate_2 {
    if (($tst1 -ne 'a') -and ($tst1 -ne 'b')) {
        Write-Output 'success_2'
    }
}

This is testing to see if $tst1 is not ‘a’ and not ‘b’ in which case it will evaluate as true.

2 Likes