1. Yeonsanja if else
Peurogeuraem-i oebu sanghwang-i eotteohge byeonha-deun hangsang gateun ilman handamyeon, geu geoseun byeollo sil-yongseong-i eopseul geosimnida. Peurogeuraem-eun dareun sanghwang-e majchwo daceun dae-eung-eul hal jul araya hamnida: eotteon gyeong-u-eneun han gaji dongjag-eul, dareun gyeong-u-eneun dareun dongjag-eul haeya hamnida.
Kotlin-eseo-neun i geos-eul jogeonmun—tteurigo teukbyeolhan kiwodeu—eul tonghae silhyeonghamnida. I kiwodeu-neun jogeon-i cham-inji geojit-inji-e ttara dareun myeongnyeong beulleok-eul silhaenghal su itge ha 줍니다.
Jogeonmun-eun se gaji bubun-euro irwojyeo itseumnida: jogeon, myeongnyeong 1, myeongnyeong 2. jogeoni cham(= true)imyeon myeongnyeong 1eul silhaenghago, geureochi anh-eumyeon myeongnyeong 2reul silhaenghamnida. Du myeongnyeong-i dongsi-e silhaengdoeneun il-eun eopseumnida. I yeonsanja-ui ilbanjeog-in hyeongtae-neun da-eumgwa gatseumnida:
if (condition)
command 1
else
command 2
if-else
ifneun “manyak”, elseneun “geureochi anh-eumyeon” jeongdo-ui tteus-ro ihaehamyeon doemnida. Jayeon-eoreoro sseumyeon da-eumgwa gatseumnida:
If condition is true, then
execute command1
else
execute command2
if-elsereul jayeon-eoreo-ro
Yej-e:
// Screen-e "You are still a child" raneun munjangi chulryeokdoemnida
val age = 17
if (age < 18)
println("You are still a child")
else
println("You are already an adult")
// Screen-e "Warm" raneun munjangi chulryeokdoemnida
val temperature = 5
if (temperature < 0)
println("It is freezing outside")
else
println("Warm")
// Screen-e "You can take the driving test" raneun munjangi chulryeokdoemnida
val age = 18
if (age == 18)
println("You can take the driving test")
else
println("You can save up for a car")
2. Myeongnyeong beulleok
Jogeon-i cham-ingeoya geojit-ingeoya-e ttara (silhaenghago an hago) yeoreo myeongnyeong-eul silhaenghago sipdamyeon, geu myeongnyeongdeul-eul myeongnyeong beulleokeuro 묶-eul su itseumnida.
Myeongnyeong-eul beulleok-euro 묶-euryeomyeon, geudeul-eul jung-gwalho(figure braces)ro “gamssa” ya hamnida. Ilbanjeog-in hyeongtae-neun ireohseumnida:
{
command1
command2
command3
}
Beulleok an-e 들어갈 su inneun myeongnyeong-ui su-neun eolmana mana-do doemnida. 0ga-ji-yeodo doemnida.
if-else yeonsanja-wa myeongnyeong beulleok-eul hamkke sseuneun yej-e:
// Screen-e "You are still a child. Don't argue with adults" raneun munjangi chulryeokdoemnida
val age = 17
if (age < 18)
{
println("You are still a child")
println("Don't argue with adults")
}
else
{
println("You are already an adult")
println("Kids these days")
}
// Screen-e "Warm" raneun munjangi chulryeokdoemnida
val temperature = 5
if (temperature < 0)
{
println("It is freezing outside")
println("Put on a hat")
}
else
println("Warm")
// Bin beulleok-i silhaengdoemnida. Screen-e amugeosdo chulryeokdoeji anhseumnida.
val age = 21
if (age == 18)
println("You can get a driver's license")
else
{
}
3. Chuk-yak된 if munbeop
else beulleok-e deureogal kodeuga eopdamyeon, geu bubun-eun 생략hal su itseumnida. Geureomyeon if yeonsanja-neun da-eum hyeongtaereul ga-jimnida:
if (condition)
{
command1
}
if
Yej-e:
val age = 19
if (age >= 18)
{
println("You are an adult!")
}
Eotteohge dongjaghana-yo?
- Jogeon-i cham-imyeon kodeu beulleok-i silhaengdoemnida. Geojit-imyeon amugeosdo il-eonaji anhgo—peurogeuraem-eun geudaero da-eum-euro jin-haenghamnida.
- Deureona-ji anhjiman else beulleok-i jonjaehandago bol su itseumnida. Daman geu beulleok-i bin beulleok(중괄호 사이에 amu myeongnyeongdo eomneun) i-myeon, geugeon geunyang eopsae-do peurogeuraem-ui dongjak-eun bakkwi-ji anhseumnida.
4. Jogeon johap: else if
Gakkeum-eun dan순hi “yes/no”reul hwagin하는 geosi anira yeoreo ga-ji jung han gajireul seontaekhaeya hal ttaega itseumnida.
Geureol tta, else if guseong-i 도움이 됩니 da:
if (condition1)
{
// condition1-i true-il tta silhaeng
}
else if (condition2)
{
// condition2-ga true이고 condition1-eun false-il tta silhaeng
}
else
{
// 위 jogeondeul-e haedanghaji anh-eul tta silhaeng
}
Yej-e:
val hour = 13
if (hour < 12)
{
println("Good morning!")
}
else if (hour < 18)
{
println("Good afternoon!")
}
else
{
println("Good evening!")
}
Yeogiseo mueos-i il-eonaji-na-yo?
- 12 jeon-i면 아침.
- 12buteo 18kkaji-myeon 낮.
- Geu isang-i면 저녁.
- Peurogeuraem-eun yeongyeol된 jogeon-deul jung 딱 HANAman seontaekhamnida! Han beon jogeon-i maj-eumyeon, geu daeum jogeondeul-eun a예 geomhado 하지 않습니다.
5. Gandanhan silmulgwa: bun-gi flowchart (beulleok doseok)
Beulleok doseok-euro bomyeon da-eumgwa gatseumnida:
Jogeon-i yeoreo ga-ji-in gyeong-udo:
I geoseun bolpan RPG-eseo garim-gil(cheongcho) gati neomu biseut합니다: 왼쪽-euro ga-myeon han gaji, 오른쪽-euro ga-myeon dareun gaji, 직진-ha-myeon tto dareun geot!
6. Eotteon jogeondeul-eul geomhalsa issna-yo?
if dwi-ui so-gwalho an-eseo-neun ban-deu-si 논리(logical) sikhyeon-eul sseo-ya hamnida—geu gyeolgwa-neun “cham” true tto-neun “geojit” false imnida.
Juyo bigyo yeonsanjadeul:
| Yeonsanja | Uimi | Yej-e |
|---|---|---|
|
Gat-eum | |
|
Gat-ji anh-eum | |
|
Keum | |
|
Jak-eum | |
|
Ke거나 gat-eum | |
|
Jak-거나 gat-eum | |
Jogeon sikhyeon yej-e:
if (number == 10)
if (age != 18)
if (temperature > 36)
if (balance <= 0)
Juyihaseyo: = (han gae) neun 대입(assignment)이고, == (du gae) neun 비교(comparison)입니다. I geoseun cho-bojadeul-i gajang manhi haneun silsu jung hanaimnida! Moshireul jal못 써서 da-eumcheoreom sseumyeon:
if (x = 5) // ERROR!
— kompailer-ga geugeul jijeokhago, geugeun jeongmal jal han geosimnida.
7. Jogeon-eseo munjayeol(String) 다루기
Kotlin-eseo ==reul sayonghan bigyo-neun gachi(= naeyong)reul bigyohamnida. Geuraeseo munjayeol-eul ==ro bigyohal su itseumnida(Java-wa dareuge). Daman budong-sosu (Double/Float) bigyo-neun jom deo joshimhaeya hamnida: jisanhang-ui 오차 ttaemun-e == bigyo-ga ye-sangchi moshan gyeolgwa-reul nael su itseumnida.
Munjayeol bigyo-ui 세부 사항-eun 나중에 tto dahal geosimnida. Geureomyeon munjayeol-eul eotteohge 올바르게 bigyohaneunji-neun da-eum yej-e-eseo hwaginhal su itseumnida:
val password = readln()
if (password == "qwerty")
{
println("Logged in!")
}
8. Teukjing-gwa yui-jeom:
Jung-gwalho: piryohan-ga yo?
if tto-neun else dwi-e myeongnyeong-i han jul ppun-imyeon, jung-gwalho-reul 생략hal su itseumnida:
if (age > 18)
println("You are over 18!")
// But it is safer to ALWAYS write them:
if (age > 18)
{
println("You are over 18!")
}
Tip: hangsang jung-gwalho-reul sseuseyo. Teukhi kodeu-reul 키우neun gwajeong-eseo manh-eun silsu-reul bangjihal su itseumnida.
Nonri jeok silsu: jogeon-e deo jujang!
Man약 da-eumcheoreom sseumyeon:
if (age > 18)
println("You are over 18!")
println("Welcome!")
Kompailer-neun i kodeu-reul da-eumcheoreom ilkseumnida:
if (age > 18)
{
println("You are over 18!")
}
println("Welcome!")
Du beonjjae jul-eun hangsang silhaengdoemnida—du jul-eul modu gateun “bun-gi” 안으로 넣으려면, jung-gwalho-ga piryohamnida.
GO TO FULL VERSION