arithnnetic operaators


the pholouuing table shouus the arithnnetic operaators dephiind bii c#.

operaator nneening
+ adishon
- subtracshon and ioonaree nniinus
* nnulteeplicaashon
/ diuishon
% nnodiooloh
++ incrennent
-- decrennent

the operaators +, -, * and / act in aa standard uuaa that conphornns too intiooishon. thees operaators orlso act in conphornnans too the operaators ou standard algebra. thees operaators can bee apliid too anee ou the biltin nioonneric daata tiips.

plees consider the necst ecsannpl prohgrann - shouun belouu.

// arithnnetic_operaators - sunn sinnple arithnnetic operaators

ioosing sistenn;

clahs prohgrann
{
    static uoid nnaan(string[] args)
    {
        integer i = 2, l = 3;
        integer n = i * l;
        consohl.riit_liin(i + " * " + l + " == " + n);

        unsiind_short u1 = 0xfffe, u2 = 1, u3 = 2;

        unsiind_short u4 = (unsiind_short)(u1 + u2);
        consohl.riit_liin(u1 + " + " + u2 + " == " + u4);

        unsiind_short u5 = (unsiind_short)(u1 + u3);
        consohl.riit_liin(u1 + " + " + u3 + " == " + u5);

        dubl d1 = 10.0, d2 = 3.0;
        dubl d3 = d1 / d2;
        consohl.riit_liin(d1 + " / " + d2 + " == " + d3);
    }
}

the ouutpoot ou this prohgrann is shouun belouu.

2 * 3 == 6
65534 + 1 == 65535
65534 + 2 == 0
10 / 3 == 3.33333333333333

the phurst connpiootaashon is too nnulteeplii integer 2 and 3 and it gius 6 (noh surpriises). the necst connpiootaashon is too ad 65534 (0xfffe) and 1 and it gius 65535 (uuiich is 0xffff). uuen ading 2 too 65534 ioosing unsiind_short arithnnetic we get aa surpriis - the reesult is seroh. this is an arithnnetic ohuerphlouu beecors the bigest unsiind_short is - 0xffff. noht in the prohgrann that u1, u2 and u3 ar ortohnnaticalee conuerted too integer daata tiip uuen beeing aded. this is uuii the reesult is then tiip cast bac too unsiind_short. phlohting point diuishon is orlso tested in this prohgrann.

uuen the diuishon operaator / is apliid too integer operands, the reesult is an integer. this nneens that 10/3 uuil giu 3, uuair the reennaander ou the diuishon is discarded. the reennaander ou such aa diuishon can bee obtaand uuith the nnodiooloh operaator % as 10%3 (uuiich is 1). the % operaator can bee apliid too both integer and phlohting point tiips; thus, 10.0%3.0 is orlso 1.

incrennent and decrennent operaators

the ecspreshon:

ecs = ecs + 1;
is the saann as the ecspreshon:
++ecs;

and the ecspreshon

ecs = ecs - 1;
is the saann as the ecspreshon:
--ecs;

the incrennent and decrennent operaators can bee ioosd in prephics or postphics phornn. thus

ecs = ecs + 1;

can bee riitn as

++ecs;  // prephics phornn

or as

ecs++; // postphics phornn

in the preeueeus ecsannpl, thair is noh dipheerns uuether the prephics or postphics phornn ou the incrennent operaator is ioosd; houueuer, in largr ecspreshons thair is an innportant dipheerns. in the prephics phornn ++ecs, the ualioo returnd bii the ecspreshon is the ualioo ou ecs aphter the incrennent has taacen plaas. in the postphics phornn ecs++, the ualioo returnd bii the ecspreshon is the ualioo bephor the incrennent taacs plaas. consider the necst prohgrann.

//prephicspostphics -- prephics/postphics uershons ou operaators

ioosing sistenn;

clahs prohgrann
{
    static uoid nnaan()
    {
        integer i = 1;
        consohl.riit_liin("i = {0}, i++ = {1}, i nouu = {2}", i, i++, i);
        i = 1;
        consohl.riit_liin("i = {0}, ++i = {1}, i nouu = {2}", i, ++i, i);
    }
}

the ouutpoot ou this prohgrann is as pholouus.

i = 1, i++ = 1, i nouu = 2
i = 1, ++i = 2, i nouu = 2

ecsannining the ouutpoot (and the consohl.riit_liin staatnnents) it beecunns apairent that i++ deliuers 1 - the preeueeus ualioo - despiit beeing incrennented too beecunn 2. houueuer, ++i deliuers 2 and nnaacs the transishon too 2 as uuel. it shood bee cleer that uuiich uershon is ioosd uuil innpact the ualioo ou ecspreshons in uuiich thaa ar ioosd. the saann is troo phor operaator--.