C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet122/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   118   119   120   121   122   123   124   125   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Arithmetic Operators

C# defines the following arithmetic operators:

Operator

Meaning


+

Addition


Subtraction (also unar y minus)

*

Multiplication



/

Division


%

Modulus


++

Increment

– –

Decrement



The operators 

+

,

,

*

, and 


/

 all work in the expected way. These can be applied to any built-

in numeric data type.

Although the actions of arithmetic operators are well known to all readers, a few special 

situations warrant some explanation. First, remember that when 

/

 is applied to an integer, 

any remainder will be truncated; for example, 10/3 will equal 3 in integer division. You can 

obtain the remainder of this division by using the modulus operator, 



%

. The 


%

 is also 

referred to as the 

remainder operator.

 It yields the remainder of an integer division. For 

example, 10 % 3 is 1. In C#, the 

%

 can be applied to both integer and floating-point types. 

63

CHAPTER


www.freepdf-books.com


64

 

P a r t   I :  



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

Thus, 10.0 % 3.0 is also 1. (This differs from C/C++, which allow modulus operations only 

on integer types.) The following program demonstrates the modulus operator:

// Demonstrate the % operator.

using System;

class ModDemo {

  static void Main() {

    int iresult, irem;

    double dresult, drem;

    iresult = 10 / 3;

    irem = 10 % 3;

    dresult = 10.0 / 3.0;

    drem = 10.0 % 3.0;

    Console.WriteLine("Result and remainder of 10 / 3: " +

                       iresult + " " + irem);

    Console.WriteLine("Result and remainder of 10.0 / 3.0: " +

                       dresult + " " + drem);

  }


}

The output from the program is shown here:

Result and remainder of 10 / 3: 3 1

Result and remainder of 10.0 / 3.0: 3.33333333333333 1

As you can see, the 

%

yields a remainder of 1 for both integer and floating-point operations.



Increment and Decrement

Introduced in Chapter 2, the 



++

 and the 



– –

 are the increment and decrement operators. As 

you will see, they have some special properties that make them quite interesting. Let’s begin 

by reviewing precisely what the increment and decrement operators do.

The increment operator adds 1 to its operand, and the decrement operator subtracts 1. 

Therefore,

x = x + 1;

is the same as

x++;

and


x = x - 1;

is the same as

x--;

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   118   119   120   121   122   123   124   125   ...   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