C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet130/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   126   127   128   129   130   131   132   133   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

&

 and 


|

. The only difference between the 

normal and short-circuit versions is that the normal operands will always evaluate each 

operand, but short-circuit versions will evaluate the second operand only when necessary.

Here is a program that demonstrates the short-circuit AND operator. The program 

determines if the value in 



d

 is a factor of 



n

. It does this by performing a modulus operation. 

If the remainder of 

n / d

 is zero, then 



d

 is a factor. However, since the modulus operation 

involves a division, the short-circuit form of the AND is used to prevent a divide-by-zero 

error.


// Demonstrate the short-circuit operators.

using System;

class SCops {

  static void Main() {

    int n, d;

    n = 10;

    d = 2;

    if(d != 0 && (n % d) == 0)

      Console.WriteLine(d + " is a factor of " + n);

    d = 0; // now, set d to zero

    // Since d is zero, the second operand is not evaluated.

    if(d != 0 && (n % d) == 0)

      Console.WriteLine(d + " is a factor of " + n);

    // Now, try the same thing without short-circuit operator.

    // This will cause a divide-by-zero error.

    if(d != 0 & (n % d) == 0)

      Console.WriteLine(d + " is a factor of " + n);

  }


}

To prevent a divide-by-zero error, the 



if

 statement first checks to see if 



d

 is equal to zero. 

If it is, then the short-circuit AND stops at that point and does not perform the modulus 

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   126   127   128   129   130   131   132   133   ...   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