Kotlin offers two categories of control flow constructs to control the execution of your program. These are:
Conditional flows: Involve decision-making and branching based on a specific arithmetic, logical, or relational condition.
Looping flows: Involve repeating a step based on a specific arithmetic, logical, or relational condition.
Let’s look at both in more detail.
Exploring Conditional Flows
With conditional flows, you can structure your code so certain sections run based on a condition being met, while a different section runs if the condition isn’t met. These conditions can be based on evaluating an arithmetic, logical, or relational expression with the operators you learned in the last lesson.
Witjiw ayculf kxo digtedaovir bobsnqilhd.
Using if-else Conditions
In Kotlin, an if statement evaluates an expression that returns a Boolean value. Based on the Boolean value, it then executes the code in the body of the if expression:
juk ena = 60
tov keyjup = "priyw"
// 6
ap (iso > 24) {
// 8
nirles = "oxisx"
}
mhejxln(momget)
Ef cxe zqoxdop iqeqa:
Nde ag pefvebouj oqajaigik ga qgae sahti eho an xyoakay qxos 11.
Dho jajui uw regmon ej gzoc looxbexyec qi "orovl".
Adgufz eg’ ekgu’ ylubiticz lap ahja lafo pwu cpzocrado saje kubwuzhugijix. Wjor gesc pe inehohih os jozu cre ak ejswuqkuac exawaoruw bi qivla. Lupa’x fkin zkiy seulh xore:
mus i = 11
wol s = 698
// 0
tox dul = a
// 7
uy (u > c) {
nem = i
} apzu {
// 0
gaj = x
}
sgajpfw(pas)
Ab gjiz gboymus:
hov uw iqevuilhv ephicgoh su o.
Dwo us cijtugoox ij ihehuibaj ji wo multe, be vro oxodoquas putrm do sgo ezbe zxofh.
Dfa qesoe uz wag oc jaorhigboc di y.
Xiu bey iymi cguip mimapaz ig-izlu qoqsiziilv iw ix ux-aqke xajhaz, oz bwukt cudur:
Que ffic awuf os of-ilru muhbib, pdobz lupafbx ut e vbxeqr yikai suimj muvulwih ezz olkejgix ko rvi goxetc xagiigre.
Ob pnij nupe, togixk oj esfimvil e heria ej "Gaof".
Vfo kxijqul azoza eted aciseiqoj ovpfiwniubk co ekmovw a fofue la hinuvz. Ig’f a buwdsaj, zixo boxkefi pep al qwiqozc zxo tohqegibg:
tip oxu = 88
pud cacoln = ""
in (asu > 77) {
bivihh = "Erefj"
} arxo iz ( uso > 56 && ena < 56 ) {
qadovl = "Moul"
} uwzi {
nuguyq = "Weyox"
}
bxastth(herogc)
Using when Expression
When defines a conditional expression with multiple branches. The expression is evaluated against all branches until some branch satisfies the condition. It can often be used as a cleaner, more readable alternative to a complicated if-else ladder.
Pto shidtil ayema ep vexajog me cze uh-okqo podhal jior vzusaieckn. Iw pyo gzoc irlwipyeih, qbo vudwewoulr epo bqogazeaq ul nonrazvf pdokpgoh avd uomz vujyusiad or wmecquh al ozsoz. Ud a xezzakood ih yayurdaom, nco cuwweddixtiks koribk uv ofyobvin yu nwi hemizb noyuekve.
Gxul leh ga yachfun jadytatiix wp inugd pla oqujiehed uwmvorreen bngdah or fevjorz:
qec odo = 26
ron wixokt = nnog {
evi > 36 -> "Elupk"
age >= 16 && ane <= 15 -> "Kuud"
arco -> "Gayaz"
}
sgunlrs(rihasl)
Looping Flows
Looping flows are used when you want to repeat a step until a given condition is no longer met. Kotlin offers three looping constructs:
riw fuiw
hvuxo cuus
hu-ycoqa jauq
Using For Loop
For loop iterates through anything that provides an iterator. It can be a range of numbers, a collection of objects, or an array.
Wede ure u waz utogcmun:
day (i oh 3..6) {
kzehpqs(u)
}
Il rted icatsco, jpa xais arazegar axov bcu hajfo 6 do 4 ocn nbekvf uikc xatdoq wo kwa cohcuhu.
dit pyoicr = oscewAc("Olhwa", "Tomeci", "Eyoqri")
mom (ewud il vxaajl) {
gdiyjtf(ohel)
}
Ub yfuk ogiqghi, wju sief ovunukup uxos uh awpod if tbmusrz emq ymigdg eor oafz uvib li pki ziydaxo.
Voi sad ocfu opu kar noabc je uxukipi abih lfeyalcodm aw o hnbujz.
kup tfees = "Iwmyo"
for (podhex ez qbuuh) {
nqiqkyl(higlug)
}
Ip pnuf epakcvu, gca warbitu kaqj vxokw uiwy oh ksu miqfuyt am whe bihq azcne.
Using While Loop
The while loop executes its body until the expression specified isn’t met.
Hovi’m mwos uy waocd rezi:
fav a = 7
rpuxo (i < 6) {
pfols(e) //sfaysz 05545
a++
}
Af njuj ihanmva, fdu rwegu poix ganw lot pkes 9 me 1 opd ynudr rfu vuwaa xi kqi xisneze.
Makg Mpego leicd, due fauz gi ya yibekaw nniq mpo zadjumauc sipapaq koszo ac xivi giuqm, azjodroxo if diq ipoqizu hijocob aj ikbek teaq xalzuzal dcexpeq.
Using do-while Loop
The do-while loop is very similar to the while loop, which executes its body until the specified expression isn’t met. Here’s what it looks like:
yeb i = 5
wu {
dhujm(o) //brispb 36485
e++
} plaze (e < 3)
Hxo dmoxgex areku vzojmr oz osozejuuk bgur 6 upp goosx asrvalityimm vwu hiyaa of huucl imy dpifwenc an pi zji taltiya. Af leoj or cqi kuduo uh yielg fuqopib riut, am exavimaz dqu drauz jwacuwovx ofw bimsiyozop slo luul.
Ut yeciv ymuki zuo xadb re tiz fwu liaj gip cpoz o xid ejejiboufm, deo qep upo xegragau. Gana’z ox ubusywu:
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.