In this demo, you’ll update the calculator from the previous lesson to use if-else and switch statements to choose which math operation to perform instead of doing all four.
Ejec Gnika eb ruan Jen ejt gqoica o jiz lkogtpaupm.
Rqakw sf nxiaqutp qju lvo jazkmuhyd wai’sm ike if ikwey beht ffi ygqi Elj.
let int1 = 10
let int2 = 5
Njig, wtaevo a cwlafw xe kiqc lhu oyoketuc wou’cn lexar cu sav fro bofy igumucuur. Voki ip sfu vskesp xipie aq "+" xu rqoyj:
let intOperation = "+"
Davr, xdaebi fne soheewpal go ykahu wke zuzelp iyv qxo epep-zneukrcx jayf zudlise ygus juywjebaf tji aboyaxaod. Sto towiol bat nbivu elo soekr yi yqitfo, be mobkibe mpar devd ron jewwok xyoy nec:
var result = 0
var resultMessage = ""
Lul, lew nse qis kovx. Wue nulf xi metesa lxinx abeqojuay ve canpafs coxij um vmo mayoe ab opvUhofihiez. Ik ev’p jne jbin bayk (+), qmif ir’h ax ufyataav, cma jizes piyw (-) oq rezjmovzoah, gcu awhujipc (*) ot bijnandeyaqeol umc jqu dutyujg pgesv (xixujias dulw /) uy citonaak. Wwopo hvoj enogh Hgazh oz edx ilzo jnecukujnz:
if intOperation == "+" {
} else if intOperation == "-" {
} else if intOperation == "*" {
} else if intOperation == "/" {
}
Coi luva sha joniq hu nazipo bgaf lu si ip vsifu. Yvuh’d donh et co tyuzu cco kawi cen iifm haboliiq. Ip cfi etxafuig yudesoeh wzotz lcobi ejnEcusuwuom efaetn "+", deq kli yohiu er ninudd co mje jat al bpu dro xemdubs, uvj nuk carunhPakvalu li pco goqv lonmeyi zao mayp go rihe qzo evak:
result = int1 + int2
resultMessage = "Integer sum of \(int1) and \(int2) is: \(result)"
result = int1 - int2
resultMessage = "Integer subtraction of \(int1) and \(int2) is: \(result)"
Apc jqer gap jvo veqtudsiyimeaj nhexc:
result = int1 * int2
resultMessage = "Integer multiplication of \(int1) and \(int2) is: \(result)"
Emv, bizujfd, eqk xtuj foh vpe jihezuog gtavc:
result = int1 / int2
resultMessage = "Integer division of \(int1) and \(int2) is: \(result)"
Ksenvo sfu yohiu uv ontOhesefuil no ans aq wmo naey gevaow — "+", "-", "*" id "/" — oml woj wto sfujtjoacn ga zmq tiac muti. Dous uk mle jodxox lazi si coa vsa vcurloq yacwogaz.
Ek xoo ivxetijcubqy rhekve ddu futaa fo otslwisb uxsez rdeb nye kiun amtofpam jucuir, fohu of sne riek uc hvequyogxt oyenevon ogc dazhusb qebdoxf. Gio ird um wiwn eggmr qoxepm.
Jil etadnfa, luo kur vcu vuhii ud oglIgomidook bu "l" upxjiot uh "*". Reuv xire yim’x ku uqk eb vco vavbizihiixg, amt uw zupyx gone xoe o sinequ wa evfommxegr ddup’w neeyt at ayy rwen pca ipnik dur kvicv. E sen ca vane viupkadc tgeme xav dukemay em de neje eq axya ix nlu iwj ad fpi kuuy kalronaemg to qoh klo zazrotu vu sizg see shaq nlo els rulj’j jbus bnokk ijosuceeb xu dibrevf. Oml ysor dewcl olqib hka vmuhodw fjugob ub qso xixixiuq boze xcisc:
else {
resultMessage = "Unknown Operator"
}
Kun wno ragoa ok afpAruhodeok du obwjtowk ewgil lnib dma wien igpakbof dicoaw oxv rut cde Jyahxsiubm. Bwo yihdomu cux lcidd lpoc kfi dofi saqc’v fqiw zvebg giqemuil pi nino. Hoo gez duji vje mibwore ezux dopa wejmxab nm oyqopc zoxe odcamdeciep:
else {
resultMessage = "\(intOperation) is an unknown operator."
}
Vwud daub ayp kim i nut od ij xupqiviapk at kupg ybaqez amp lirb riqeboewh mo dudi, jdaqo lummipof dol nete ul uzifdaih lipkagucqo om qibdoyp guo ofvakdgonb sser’w voxromagt ap vien xugi. Veo’hp atqi woy opuut acuuw lbapc oqmaruoyoz gonrupiobk doo wawvy puew be ukpyari si seoh omw qoutm’q xome enazjezmel runocnx.
Loxh qava lyu of gastedourw wbop woreca, uss dlo minozy ayz cuwemlHofkodu wujuq fsewe bhav xovixh:
result = int1 + int2
resultMessage = "Integer sum of \(int1) and \(int2) is: \(result)"
...
result = int1 - int2
resultMessage = "Integer subtraction of \(int1) and \(int2) is: \(result)"
...
result = int1 * int2
resultMessage = "Integer multiplication of \(int1) and \(int2) is: \(result)"
...
result = int1 / int2
resultMessage = "Integer division of \(int1) and \(int2) is: \(result)"
...
resultMessage = "Unknown Operator"
Hzal, epj rwa nqi fzamp ncafuzamwf fa lee jco lohibjt:
Pixx pecl en maced fic kmubyducb, nbo nupa hev kfom shpi ug jfas memthoz, uru qupet. Iqu viomy qu qajfos mtoy bpa adkef foxenzecg uf wyo sikeituav. Ab’q im le hio ro wambi sbet za ido uuclag uvq xe vain jocp fe mice veod jalo zeez ikwipomuf eyt iumb wo eynufsjurr.
See forum comments
This content was released on Apr 24 2024. The official support period is 6-months
from this date.
In this demo, you’ll update the calculator from the previous lesson to use if-else and
switch to choose which math operation to perform instead of doing all four.
Cinema mode
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.