C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet126/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   122   123   124   125   126   127   128   129   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

==

 and 


!=

.

However, the comparison operators, 



<

,

>

,

<=

, or 


>=

, can be applied only to those types 

that support an ordering relationship. Therefore, all of the relational operators can be 

applied to all numeric types. However, values of type 



bool

 can only be compared for 

equality or inequality since the 

true

 and 


false

 values are not ordered. For example, 



true > false

 has no meaning in C#.

www.freepdf-books.com



68

 

P a r t   I :  



T h e   C #   L a n g u a g e

For the logical operators, the operands must be of type 



bool

, and the result of a logical 

operation is of type 

bool

. The logical operators, 



&

,

|

,

^

, and 


!

, support the basic logical 

operations AND, OR, XOR, and NOT, according to the following truth table:

p

q



p & q

p | q


p ^ q

!p

False



False

False


False

False


True

True


False

False


True

True


False

False


True

False


True

True


True

True


True

True


True

False


False

As the table shows, the outcome of an exclusive OR operation is true when one and only 

one operand is true.

Here is a program that demonstrates several of the relational and logical operators:

// Demonstrate the relational and logical operators.

using System;

class RelLogOps {

  static void Main() {

    int i, j;

    bool b1, b2;

    i = 10;

    j = 11;

    if(i < j) Console.WriteLine("i < j");

    if(i <= j) Console.WriteLine("i <= j");

    if(i != j) Console.WriteLine("i != j");

    if(i == j) Console.WriteLine("this won't execute");

    if(i >= j) Console.WriteLine("this won't execute");

    if(i > j) Console.WriteLine("this won't execute");

    b1 = true;

    b2 = false;

    if(b1 & b2) Console.WriteLine("this won't execute");

    if(!(b1 & b2)) Console.WriteLine("!(b1 & b2) is true");

    if(b1 | b2) Console.WriteLine("b1 | b2 is true");

    if(b1 ^ b2) Console.WriteLine("b1 ^ b2 is true");

  }

}

The output from the program is shown here:



i < j

i <= j


i != j

!(b1 & b2) is true

b1 | b2 is true

b1 ^ b2 is true

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   122   123   124   125   126   127   128   129   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish