Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell - if statement condition that produces multiple results

Tags:

powershell

Please consider this silly example:

if (1..3) { "true" }

The above produces the output true.

My question: How does the if statement handle a case like this where multiple values are output by the conditional? Is the "true" output the result of the "3" (the last case)? Or is some other logic at work? Thanks.

like image 576
Sabuncu Avatar asked Nov 24 '25 01:11

Sabuncu


1 Answers

The observed behavior is explained (to some extent) in this blog post. Basically, if an expression evaluates to 0 it's interpreted as false, otherwise as true. Examples:

0      => False
1      => True
"0"    => True (because it's a string of length 1)
""     => False (because it's a string of length 0)
@()    => False
@(0)   => False (this one's a little surprising)
@(0,1) => True
@("0") => True
like image 130
Ansgar Wiechers Avatar answered Nov 26 '25 16:11

Ansgar Wiechers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!