-
-
Save anggaaryas/2f6ab964df437be53a33af61a54b232c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main(args: Array) { | |
// TODO: Statement if | |
/** | |
* * Secara keseluruhan, penggunaan if | |
* * Mirip dengan pemograman c++ dan java | |
*/ | |
val x = 5; | |
if(x == 0) println("ini 0") | |
else if (x != 5) println("bukan") | |
else if (x > 5) println("lebih besar") | |
else println(x) // 5 | |
//TODO: switch - case | |
val y = 'b' | |
when(y){ | |
// * bisa menggunakan range | |
in '0'..'9' -> println("angka") | |
// * 1 case juga bisa | |
'a' -> println("huruf A") | |
// * Tak perlu break; | |
// * Ini yang aku sukai :) | |
in 'a'..'z' -> { | |
println("huruf") | |
val d = 7; | |
var b : Float = d.toFloat() / 3; | |
println(b) | |
} | |
// * Untuk default | |
else -> println("simbol") | |
} | |
// * Contoh Switch yang lebih keren :) | |
// ! Materi ini belum dipelajari | |
fun describe(obj: Any): String = | |
when (obj) { | |
1 -> "One" | |
"Hello" -> "Greeting" | |
is Long -> "Long" | |
!is String -> "Not a string" | |
else -> "Unknown" | |
} | |
println(describe(8678)) | |
println(describe("8678")) | |
/////////////////////////////////////////// | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment