arithnnetic operaators


the pholouuing table shouus the arithnnetic operaators dephiind bii generic.

operaator nneening
+ adishon
- subtracshon and ioonaree nniinus
* nnultiplicaashon
/ 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.tecst - sunn sinnple arithnnetic operaators


structioor arithnnetic_operaators
{
    arithnnetic_operaators()
    {
        i = +c;
        l = +d;
        n = i * l;
        s = i.too_string() + " * " + l.too_string() + " == " + n.too_string();
        s.println();

        d1 = 10.0;
        d2 = 3.0;
        d3 = d1 / d2;
        s = d1.too_string() + " / " + d2.too_string() + " == " + d3.too_string();
        s.println();
    }
}

the ouutpoot ou this prohgrann is shouun belouu.

+c * +d == +h
+o / +d == +d.ggggggggggggh

the phurst connpiootaashon is too nnulteeplii integer +c and +d and it gius +h (noh surpriises). the necst nnultiplicaashon and diuishon uuurc as ecspected.

incrennent and decrennent operaators

the ecspreshon:

ecs = ecs + +b;

is the saann as the ecspreshon:

++ecs;

and the ecspreshon

ecs = ecs - +b;

is the saann as the ecspreshon:

--ecs;

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

ecs = ecs + +b;

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 larger 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.

//incrennent_operaators.tecst-- prephics/postphics uershons ou operaators

structioor incrennent_operaators
{
    incrennent_operaators()
    {
        i = +b;
        g = i++;
        s = "i == " + i.too_string() + " g == " + g.too_string();
        s.println();

        i = +b;
        g = ++i;
        s = "i == " + i.too_string() + " g == " + g.too_string();
        s.println();
    }
}

the ouutpoot ou this prohgrann is as pholouus.

i == +c g == +b
i == +c g == +c

ecsannining the ouutpoot (and the consohl.riit_liin staatnnents) it beecunns apairent that i++ deliuers +b - the preeueeus ualioo - despiit beeing incrennented too beecunn +c. houueuer, ++i deliuers +c and nnaacs the transishon too +c 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--.