Logical Operators in PowerShell
Introduction to logical operators:
We have seen many conditions in if statement ,
like if($j -lt 10) ,But if we wanted to check multiple conditions at once
,for example $j -lt 10 and $i -lt 15 .So we have logical operators
to tackle these kind of situation. Logical operators combine two
or more expressions and statements together . In very simple words
if we wanted to convert multiple conditions in a single condition
than we can use logical operators .Let us call Logical Operator
with name of LO for syntax .
Syntax,
if(cond1 LO1 cond2 LO2 cond3){
Statement 1
Statement 2
...
}
So in above syntax (cond1 LO1 cond2 LO2 cond3) combined to
make one condition.In the above syntax
(cond1 LO1 cond2 LO2 cond3)=single condition . As it will return
single value combination of multiple conditions .
Example ,
($a -gt $b) =one condition, (($a -lt 20) -or ($b -lt 20))=one condition by two
combination and ($a -gt $b) -and (($a -lt 20) -or ($b -lt 20)) =one condition,
by a combination of all three we are getting one single condition .
So if all of these conditions will be true then only output
“all combined conditions are true” will be shown .
$a =14
$b=12
if(($a -gt $b) -and (($a -lt 20) -or ($b -lt 20))
){
Write-Output “all combined conditions are true”
}
In this example we are combining three conditions all ,($a -gt $b) =one condition,
(($a -lt 20) -or ($b -lt 20))=one condition,($a -gt $b) -and (($a -lt 20) -or ($b -lt 20)) =one condition,
by a combination of all three we are getting one single condition .
Here we can see we are combining all three conditions to form a single condition , their output will be single .
$a =20
$b=21
if(($a -gt $b) -and (($a -lt 20) -or ($b -lt 20))
){
Write-Output “all combined conditions are true”
}else{
Write-Output “all combined conditions are false”
}
List of Logical Operators :
let us discuss each with example in brief .
-and:-and is called as Logical and ,output of any logical and is True if $a and $b are True otherwise False ,below are some examples for logical and operators
$a -and $b //false (if both are false)
$a -and $b //false (if any one of them is false)
$a -and $b //true (if both of them are true).
So basically logical and operator true only when both true . Screen for execution of the above examples
are given below .
In general and operators used were we want all conditions should full fill .
For example suppose In a class teacher decided to allow for exams only
those students whose attendance is more than 100 and also they paid class fee .
So here both conditions need to full fill .
$attendance =101
$paid =”yes”
if($attendance -gt 100 -and $paid -eq “yes”){
Write-Output “Allow him for examination”
}
We can also test this program by passing different inputs value
of $attendance and $paid .
-or:Logical or ,False if $a and $b are False, otherwise True.
some examples are given below.
$a -or $b //false(if both are false)
$a -or $b //true (if any one of them is true)
$a -or $b //true (if both of them are true).
So basically logical and operator false only when both false . Screen for execution of the above examples are given below .
In general or operators are used when we want to consider any condition is true .like students having attendance more than 100 will get 5 marks extra or
the student who score more than 200 marks .
$attendance =101
$marks =201
if($attendance -gt 100 -or $marks -gt 200){
Write-Output “give 5 marks extra”
}
We can also test this program by passing different inputs value of
$attendance and $paid .
-xor:Logical exclusive or ,
True if either $a or $b is True,otherwise False.
(‘a’ -eq ‘A’) -xor (‘a’ -eq ‘z’) //true as one of them is true
(‘a’ -eq ‘A’) -xor (‘Z’ -eq ‘z’)//false as one of them is false
(‘a’ -eq ‘s’) -xor (‘Z’ -eq ‘p’) //false as both of them are false
Below screen show output of the above example ,
-not:Logical not ,True if $a is False, otherwise False.
-not (‘a’ -eq ‘a’) //false as output of expression is true
-not (‘v’ -eq ‘a’)// true as output expression is false
-not (‘v’ -eq ‘V’) //false as output expression is true
-not (‘V’ -eq ‘V1’) //true as output expression is false
Screen for above example is given below ,
!:! Operator is same as that of the the -not operator .
Simply ! operator converts true to false and false to true .
!(‘a’ -eq ‘a’) //false as output of expression is true
!(‘v’ -eq ‘a’)// true as output expression is false
!(‘v’ -eq ‘V’) //false as output expression is true
!(‘V’ -eq ‘V1’) //true as output expression is false
Screen for above example is given below ,
Note :In PowerShell always use $TRUE and $FALSE for true and false value ,
if you will use true and false they will not consider as Boolean value .
$a =false
$b=true
!$a //true
!($b) //true
$a=$false
$b=$true
!($a) //true
!($b) //false
Screen for above example is given below,
Some real world examples with mixing all operators together ,
Suppose our server and database is running and we want
implements certain check were it will check all the time if server
and database are running or not .
if($server -eq “running” -and $database -eq “running”){
Write-Output “server is running and database is running”
}elseif($server -eq “not running” -and $database -eq “running”){
Write-Output “server is not running and database running
}elseif($server -eq “running” -and $database -eq “not running”){
Write-Output “server is running and database not running”
}else{
Write-Output “server and database both are not running”
}
1st input:
$server =”not running”;
$database =”running”
2ND input:
$server =”running”;
$database =”not running”
Conclusion:
To conclude, Without logical operator our programing will be blank , because of logical operators only we are able to write situational code ,
we are able to deal with different conditions.
No comments:
Post a Comment