본문 바로가기

# 02/코틀린

[Kotlin] Control Flow - if/when/loops

반응형

if


fun test() {

val a = 10

val b = 20

var max = a

if ( a < b ) max = b

}



if else


fun test() {

val a = 10

val b = 20

val max = if ( a > b ) a else b

}



fun test() {

val a = 10

val b = 20

val max : Int

if ( a > b ) {

max = a

} else {

max = b

}

}



fun test() {

val a = 10

val b = 20

val max : Int

val min : Int

if ( a > b ) {

max = a

min = b

} else {

max = b

min = a

}

}

println("max $max min $min")




java switch


int a = 10;

switch (a) {

case 1 : 

case 10 :

// ...

break;

default : 

// ...

break;

}



When

-> Kotlin


val a = 10

when (a) {

1, 10 -> // ...

else -> // ...

}



val validNumbers = 9..10

val a : Any = 30

when(a) {

in 1..10 -> println("1~10")

in validNumbers -> println("validNumbers")

!in 10..20 -> println("a is outside the range")

else -> println("else")

}

-> in : 포함, !in : 포함하지 않은 경우



fun printString(a : Any) {

when (a) {

1, 10 -> println("1~10")

is String -> println(a)

else -> println("else")

}

}

isString("message")


-> is : instanceof



When return


fun isString(a : Any) = when (a) {

is String -> true

else -> false

}

println(isString("message"))




printSample(SampleOne("taehwan"))

printSample(SampleTwo(20))


fun printSample(sample : Sample) {

when (sample) {

is SampleOne -> println("sample name : ${sample.name}")

is SampleTwo -> println("sample age : ${sample.age}")

}

}


// -> 다음과 같은 data class 정의

sealed class Sample

data class SampleOne(val name : String) : Sample()

data class SampleTwo(val age : Int) : Sample()




For - java


String name = "name";

for (int i = 0; i < name.length(); i++) {

name.charAt(i);

}



List<String> list = new ArrayList<>();

list.add("name");

list.add("bbb");


for (String s : list) {

System.out.println(s);

}



For - kotlin


val name = "name"

for (i in name) {

println("i $i")

}



val list = mutableListOf(1, 2, 3, 4, 5)

for (i in list) {

println("i $i")

}



For - list에서 index가 필요하면


val list = mutableListOf(1, 2, 3, 4, 5)

for (i in list.indices) {

println("i ${list[i]}")

}

-> indices를 사용



For - list에서 index와 value가 모두 필요하면


val list = mutableListOf(1, 2, 3, 4, 5)

for ((index, value) in list.withIndex()) {

println("$index $value")

}



For - break


val list = mutableListOf(1, 2, 3, 4, 5)

for ((index, value) in list.withIndex()) {

println("$index $value")

if(index == 1) {

break

}

}



For - step


-> java

for (int i = 0; i < 10; i+=2) {

System.out.println("value " + i);

}


-> kotlin

for (value in 1..10 step 2) {

println("value $value")

}




For - map


val map = mutableMapOf(1 to "ABC", 2 to "BBB", 3 to "CCC")

for ((key, value) in map) {

println ("key $key value $value")

}



For - map - Decompile


Map map = MapsKt.mutableMapOf(new Pair[] {

TuplesKt.to(Integer.valueOf(1), "ABC"),

TuplesKt.to(Integer.valueOf(2), "BBB"),

TuplesKt.to(Integer.valueOf(3), "CCC")});

Iterator var3 = map.entrySet().iterator();


while(var3.hasNext()) {

Entry var2 = (Entry) var3.next();

int key = ((Number)var2.getKey()).intValue();

String value = (String)var2.getValue();

String var6 = "key " + key + " value "+ value;

System.out.println(var6);

}


반응형

'# 02 > 코틀린' 카테고리의 다른 글

[Kotlin] return and jumps and This-expressions  (0) 2019.07.11
[Kotlin] Lambda  (0) 2019.07.11
[Kotlin] Function  (0) 2019.07.11
[Kotlin] Class  (0) 2019.07.11
[Kotlin] Properties and Fields  (0) 2019.07.11