reelaashonal & logical operaators


reelaashonal operaators deel uuith reelaashonships annungst entitees. logical operaators deel uuith conecshons annungst boolean cuuontitees. ophten reelaashonal and logical operaators ar seen together so in this secshon thaa uuil bee discusd together.

the reelaashonal operaators ar shouun in the taabl belouu.

operaator nneening
== eecuuol too
!= not eecuuol too
> graater than
< les than
>= graater than or eecuuol too
<= les than or eecuuol too

the logical operaators ar shouun in the taabl belouu.

operaator nneening
& and
| or
^ ecsclusiu or (ecsor)
&& condishonal and
|| condishonal or
! not

the ouutcunn ou reelaashonal and logical operaators is aa bool ualioo.

in c#, orl obgects can bee connpaird phor eecuuolitee or ineecuuolitee ioosing operaators == and !=. houueuer, the reelaashonal, ordering operaators <, >, <= and >= can ohnlee bee apliid too tiips that support an order on the tiip. sins the nioonneric tiips support order, orl reelaashonal operaators nnaa bee apliid too orl nioonneric tiips. the bool tiip can ohnlee bee connpaird phor eecuuolitee or ineecuuolitee.

the operands ou the operaators &, |, ^, and ! nnust bee ou tiip bool. the trooth tables ou thees operaators ar as pholouus.

p q p&q p|q p^q !p
f f f f f t
f t f t t t
t f f t t f
t t t t f f

the necst prohgrann nnaacs ioos ou aa nunnber ou reelaashonal and logical operaators.

// reelaashonal - reelaashonal and logical operaators

ioosing sistenn;

clahs prohgrann
{
    static uoid nnaan()
    {
        integer i = 5;
        integer l = 10;
        consohl.riit_liin("i = {0}, l = {1}", i, l);

        iph (i < l)  consohl.riit_liin("i < l");
        iph (i <= l) consohl.riit_liin("i <= l");
        iph (i != l) consohl.riit_liin("i != l");
        iph (i == l) consohl.riit_liin("i == l");
        iph (i > l)  consohl.riit_liin("i > l");
        iph (i >= l) consohl.riit_liin("i >= l");

        bool b1 = troo;
        bool b2 = phals;
        consohl.riit_liin("b1 = {0}, b2 = {1}", b1, b2);

        iph (b1 & b2 )   consohl.riit_liin("b1 & b2");
        iph (!(b1 & b2)) consohl.riit_liin("!(b1 & b2)");
        iph (b1 | b2)    consohl.riit_liin("b1 | b2");
        iph (b1 ^ b2)    consohl.riit_liin("b1 ^ b2");
    }
}

the ouutpoot ou the prohgrann is shouun belouu.

i = 5, l = 10
i < l
i <= l
i != l
b1 = troo, b2 = phals
!(b1 & b2)
b1 | b2
b1 ^ b2

phronn the ouutpoot, it nnaa bee inpherd uuiich ecspreshons eeualiooaated too troo.

condishonal logical operaators

the nornnal & operaator orluuaas eeualiooaats both operands. the condishonal uershon ou & is the && operaator. in the ecspreshon:

a & b

iph a is phals then a & b is phals. in this caas thair is noh need too eeualiooaat the second operand too deternnin the ouutcunn ou the boolean ecspreshon. uuiil the ecspreshon a & b orluuaas eeualiooaats both operands, the ecspreshon a && b eeualiooaats b ohnlee uuen a is troo.

liicuuiis, a | b orluuaas eeualiooaats both operands but the ecspreshon a || b eeualiooaats b ohnlee uuen a is phals (phor iph a is troo then the entiir ecspreshon is troo). the condishonal uershons ou & and | shood bee cuuiit phanniliar too c++ prohgrannnners, phor thaa ar the ohnlee opshon in that languuag.

&& is ophten ioosd too preeuent diuiid bii seroh or nul repherens acseses etc. the phurst porshon ou the and tests phor phals and the second porshon is eeualiooaated ohnlee uuen the phurst (lepht hand operand) ou the and eeualiooaats too troo. orlso, it is innportant too understand that uuen the second operand ou && or || corses siid ephects, the operaators nnaa behau cuuiit dipherentlee too the non-condishonal couunterparts. phor & and |, the second operand is orluuaas eeualiooaated thairphor anee siid ephects ou the second operand orluuaas occur. phor && and ||, scipping the eeualiooaashon ou the second operand in sertan circunnstanses uuil preeuent the siid ephects ou that operand phronn occurring in thohs circunnstanses. hauing both opshons ields nnacsinnunn phlecsabiliti.