SlideShare a Scribd company logo
Memulai
Pemrograman
dengan Kotlin
#2
Ahmad Arif Faizin
Academy Content Writer
Dicoding Indonesia
x
Tips Trik Seputar Instalasi
- Pastikan menggunakan versi yang sama dengan yang ada di modul.
- Jika sudah pernah menginstall Java sebelumnya, install dulu versi yang lama.
- Invalidate cache/restart jika tidak bisa run EduTools.
- Disable antivirus seperti Avast.
- Jika errornya “Failed to launch checking”, coba hapus folder project dan ulangi
mengambil course.
- Pastikan koneksi lancar saat instalasi.
- Kode merah-merah? pastikan besar kecil spasi sama dengan di modul.
- Untuk pengguna 32 bit bisa mencoba install JDK lewat Azul.com
Dts x dicoding #2 memulai pemrograman kotlin
3.
Kotlin Fundamental
Tipe Data & Fungsi
Hello Kotlin!
// main function
fun main() {
println("Hello Kotlin!")
}
fungsi pertama ketika
program dijalankan
untuk memberi komentar
fungsi untuk mencetak teks
ke layar/console
println vs print
fun main() {
println("Kotlin, ")
print("is Awesome!")
}
/*
output:
Kotlin,
is Awesome
*/
fun main() {
print("Kotlin, ")
print("is Awesome!")
}
/*
output:
Kotlin, is Awesome
*/
Contoh lain
fun main() {
val name = "Arif"
print("Hello my name is ")
println(name)
print(if (true) "Always true" else "Always false")
}
/*
output:
Hello my name is Arif
Always true
*/
cetak teks ke layar
untuk membuat variabel dengan tipe data string
untuk membuat
percabangan
cetak teks ke layar dan
membuat baris baru
Cara Menulis Variabel
var namaVariabel: TipeData = isi(value)
Contoh :
var company: String = "Dicoding"
atau bisa disingkat
var company = "Dicoding"
tipe data akan
menyesuaikan dengan
isinya
var vs val
Bisa diubah nilainya
(mutable)
var company = "Dicoding"
company = "Dicoding Academy"
Tidak bisa diubah nilainya
(immutable)
val company = "Dicoding"
company = "Dicoding Academy"
//Val cannot be reassigned
Tipe Data
Char hanya satu karakter ‘A’
String kumpulan dari banyak karakter (teks) “Kotlin”
Array menyimpan banyak objek arrayOf(“Aku”, “Kamu”, “Dia”)
Numbers angka 7
Boolean hanya memiliki 2 nilai true/false
Char
Contoh :
val character: Char = 'A'
Contoh salah
val character: Char = 'ABC'
// Incorrect character literal
Char
Operator increment & decrement
fun main() {
var vocal = 'A'
println("Vocal " + vocal++)
println("Vocal " + vocal--)
println("Vocal " + vocal)
}
/*
output:
Vocal A
Vocal B
Vocal A
*/
String
Memasukkan variabel ke dalam String
Contoh :
fun main() {
val text = "Kotlin"
val firstChar = text[0]
print("First character of" + text + " is " + firstChar)
}
print("First character of $text is $firstChar") string template
String
Membuat pengecualian(escaped) di dalam String
Contoh :
val statement = "Kotlin is "Awesome!""
String
Membuat Raw String
Contoh :
fun main() {
val line = """
Line 1
Line 2
Line 3
Line 4
""".trimIndent()
print(line)
}
/*
output:
Line 1
Line 2
Line 3
Line 4
*/
Array
indeks dimulai dari 0
fun main() {
val intArray = intArrayOf(1, 3, 5, 7) // [1, 3, 5, 7]
intArray[2] = 11 // [1, 3, 11, 7]
print(intArray[2])
}
/*
Output: 11
*/
1 3 5 7
ke-0 ke-1 ke-2 ke-3
Boolean
Numbers
Int (32 bit) nilai angka yang umum digunakan val intNumbers = 100
Long (64 bit) nilai angka yang besar val longNumbers = 100L
Short (16 bit) nilai angka yang kecil val shortNumber: Short = 10
Byte (8 bit) nilai angka yang sangat kecil val byteNumber = 0b11010010
Double (64 bit) nilai angka pecahan val doubleNumbers = 1.3
Functions
tanpa nilai kembalian
fun main() {
setUser("Arif", 17)
}
fun setUser(name: String, age: Int){
println("Your name is $name, and you $age years old")
}
parameter yang dikirimkan
ke dalam fungsi
untuk membuat fungsi
Functions
dengan nilai kembalian
fun main() {
val user = setUser("Arif", 17)
println(user)
}
fun setUser(name: String, age: Int): String {
return "Your name is $name, and you $age years old"
}
untuk membuat nilai
kembalian
tipe data nilai yang
dikembalikan
fun setUser(name: String, age: Int) = "Your name is $name, and you $age years old"
If Expressions
percabangan berdasarkan kondisi
val openHours = 7
val now = 20
if (now > openHours){
println("office already open")
}
val openHours = 7
val now = 20
val office: String
if (now > openHours) {
office = "Office already open"
} else {
office = "Office is closed"
}
print(office)
val openHours = 7
val now = 7
val office: String
office = if (now > 7) {
"Office already open"
} else if (now == openHours){
"Wait a minute, office will be open"
} else {
"Office is closed"
}
print(office)
Nullable Types
Contoh:
val text: String? = null
Contoh salah:
val text: String = null
// compile time error
Safe Call Operator ?.
memanggil nullable dengan aman
val text: String? = null
text?.length
val text: String? = null
val textLength = text?.length ?: 7
Elvis Operator ?:
nilai default jika objek bernilai null
Kerjakan Latihan
4.
Control Flow
Percabangan & Perulangan
When Expression
Percabangan selain If Else
fun main() {
val value = 7
val stringOfValue = when (value) {
6 -> "value is 6"
7 -> "value is 7"
8 -> "value is 8"
else -> "value cannot be reached"
}
println(stringOfValue)
}
/*
output : value is 7
*/
When Expression
val anyType : Any = 100L
when(anyType){
is Long -> println("the value has a Long type")
is String -> println("the value has a String type")
else -> println("undefined")
}
val value = 27
val ranges = 10..50
when(value){
in ranges -> println("value is in the range")
!in ranges -> println("value is outside the range")
else -> println("value undefined")
}
val x = 11
when (x) {
10, 11 -> print("the value between 10 and 11")
11, 12 -> print("the value between 11 and 12")
}
val x = 10
when (x) {
5 + 5 -> print("the value is 10")
10 + 10 -> print("the value is 20")
}
Enumeration
fun main() {
val color: Color = Color.GREEN
when(color){
Color.RED -> print("Color is Red")
Color.BLUE -> print("Color is Blue")
Color.GREEN -> print("Color is Green")
}
}
enum class Color(val value: Int) {
RED(0xFF0000),
GREEN(0x00FF00),
BLUE(0x0000FF)
}
//output : Color is Green
While
Perulangan
fun main() {
var counter = 1
while (counter <= 5){
println("Hello, World!")
counter++
}
}
fun main() {
println("Hello World")
println("Hello World")
println("Hello World")
println("Hello World")
println("Hello World")
}
=
fun main() {
var counter = 1
while (counter <= 7){
println("Hello, World!")
counter++
}
}
cek dulu
baru jalankan
While vs Do While
fun main() {
var counter = 1
do {
println("Hello, World!")
counter++
} while (counter <= 7)
}
jalankan dulu
baru cek
Hati - Hati dengan infinite loop
Karena kondisi tidak terpenuhi
var value = 'A'
do {
print(value)
} while (value <= 'Z')
var i = 3
while (i > 3) {
println(i)
i--
}
val rangeInt = 1.rangeTo(10)
=
val rangeInt = 1..10
=
val rangeInt = 1 until 10
dari bawah
ke atas
rangeTo vs downTo
val downInt = 10.downTo(1)
dari atas ke bawah
For Loop
Perulangan dengan counter otomatis
fun main() {
val ranges = 1..5
for (i in ranges){
println("value is $i!")
}
}
fun main() {
var counter = 1
while (counter <= 5){
println("value is $counter!")
counter++
}
}
=
For Each
Perulangan dengan counter otomatis
fun main() {
val ranges = 1..5
ranges.forEach{
println("value is $i!")
}
}
fun main() {
val ranges = 1..5
for (i in ranges){
println("value is $i!")
}
}
=
Break & Continue
Contoh program deteksi angka ganjil sampai angka 10
fun main() {
val listNumber = 1.rangeTo(50)
for (number in listNumber) {
if (number % 2 == 0) continue
if (number > 10) break
println("$number adalah bilangan ganjil")
}
}
* % adalah modulus / sisa pembagian
misal 10 % 2 == 0, 11 % 2 = 1 , 9 % 6 = 3
/*
output:
1 adalah bilangan ganjil
3 adalah bilangan ganjil
5 adalah bilangan ganjil
7 adalah bilangan ganjil
9 adalah bilangan ganjil
*/
Lanjutkan Latihan
&
Terus Belajar!
OUR LIFE SHOULD BE JUST LIKE
MODULUS. DOESN’T MATTER WHAT VALUE
IS ENTERED, IT’S RESULT ALWAYS
POSITIVE. AND SO SHOULD BE OUR LIFE
IN EVERY SITUATION
- Pranay Neve
’’
’’
You can find me at:
● Google : Ahmad Arif Faizin
● Discord : @arifaizin
Thanks!
Any questions?

More Related Content

PPTX
Dts x dicoding #3 memulai pemrograman kotlin
PPTX
Dts x dicoding #5 memulai pemrograman kotlin
PPTX
Dts x dicoding #1 memulai pemrograman kotlin
PPTX
Dts x dicoding #4 memulai pemrograman kotlin
PPTX
Sistem Operasi
PDF
7. Queue (Struktur Data)
DOCX
Algotitma dan Struktur Algoritma - Collection
PPTX
8. Multi List (Struktur Data)
Dts x dicoding #3 memulai pemrograman kotlin
Dts x dicoding #5 memulai pemrograman kotlin
Dts x dicoding #1 memulai pemrograman kotlin
Dts x dicoding #4 memulai pemrograman kotlin
Sistem Operasi
7. Queue (Struktur Data)
Algotitma dan Struktur Algoritma - Collection
8. Multi List (Struktur Data)

What's hot (20)

PDF
Modul praktikum-pemrograman java dgn netbeans
DOCX
Laporan Praktikum Web dengan PHP
PDF
4.1 Operasi Dasar Singly Linked List 1 (primitive list)
PDF
5. Doubly Linked List (Struktur Data)
PDF
Be Smart, Constrain Your Types to Free Your Brain!
PDF
Analisis Algoritma - Notasi Asimptotik
PPTX
Materi 6. perulangan
PPT
Algoritma - prosedur dan fungsi
PDF
4.2. Operasi Dasar Singly Linked List 2 (primitive list)
PPT
Modul 8 - Jaringan Syaraf Tiruan (JST)
PPTX
Database rumah sakit
PPTX
Bab 4 aplikasi stata pada regresi cox (STATA)
PPTX
Array 1
PDF
Algoritma dan Struktur Data (Python) - Perulangan
PDF
Pengenalan Pemrograman Java
PPT
Pengenalan c++ bagian 3
PPT
Metode numerik persamaan non linier
PDF
Pertemuan 3 activity
PPTX
Materi 3 Finite State Automata
PDF
Modul Praktikum Pemrograman Berorientasi Objek (Chap.11)
Modul praktikum-pemrograman java dgn netbeans
Laporan Praktikum Web dengan PHP
4.1 Operasi Dasar Singly Linked List 1 (primitive list)
5. Doubly Linked List (Struktur Data)
Be Smart, Constrain Your Types to Free Your Brain!
Analisis Algoritma - Notasi Asimptotik
Materi 6. perulangan
Algoritma - prosedur dan fungsi
4.2. Operasi Dasar Singly Linked List 2 (primitive list)
Modul 8 - Jaringan Syaraf Tiruan (JST)
Database rumah sakit
Bab 4 aplikasi stata pada regresi cox (STATA)
Array 1
Algoritma dan Struktur Data (Python) - Perulangan
Pengenalan Pemrograman Java
Pengenalan c++ bagian 3
Metode numerik persamaan non linier
Pertemuan 3 activity
Materi 3 Finite State Automata
Modul Praktikum Pemrograman Berorientasi Objek (Chap.11)
Ad

Similar to Dts x dicoding #2 memulai pemrograman kotlin (20)

PPTX
kotlin-nutshell.pptx
PDF
Kotlin fundamentals - By: Ipan Ardian
PDF
Kotlin Fundamentals
PDF
Privet Kotlin (Windy City DevFest)
PPTX
Kotlin Basics
PDF
Kotlin: forse è la volta buona (Trento)
PDF
Kotlin @ Devoxx 2011
PDF
Kotlin Slides from Devoxx 2011
PPTX
Compose Code Camp (1).pptx
PPTX
Kotlin InDepth Tutorial for beginners 2022
PDF
Kotlin Bytecode Generation and Runtime Performance
PPTX
Introduction to Kotlin
PDF
Kotlin Basics - Apalon Kotlin Sprint Part 2
PDF
Compose Camp
PDF
Kotlin- Basic to Advance
PDF
Bologna Developer Zone - About Kotlin
PDF
레진코믹스가 코틀린으로 간 까닭은?
PPTX
Introduction to kotlin and OOP in Kotlin
PDF
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
kotlin-nutshell.pptx
Kotlin fundamentals - By: Ipan Ardian
Kotlin Fundamentals
Privet Kotlin (Windy City DevFest)
Kotlin Basics
Kotlin: forse è la volta buona (Trento)
Kotlin @ Devoxx 2011
Kotlin Slides from Devoxx 2011
Compose Code Camp (1).pptx
Kotlin InDepth Tutorial for beginners 2022
Kotlin Bytecode Generation and Runtime Performance
Introduction to Kotlin
Kotlin Basics - Apalon Kotlin Sprint Part 2
Compose Camp
Kotlin- Basic to Advance
Bologna Developer Zone - About Kotlin
레진코믹스가 코틀린으로 간 까닭은?
Introduction to kotlin and OOP in Kotlin
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
Ad

More from Ahmad Arif Faizin (20)

PDF
Guideline Submission GitHub BFAA Dicoding
PPTX
Proker Departemen Dakwah dan Syiar 2013.pptx
PPTX
DKM_2013_BISMILLAH.pptx
PPT
Proker bendahara al muhandis 2013.ppt
PPTX
PPT raker EKONOMI 2013.pptx
PPT
Program Kerja Kaderisasi Al Muhandis 2013
PPTX
Departemen Mentoring.pptx
PPTX
ANNISAA' 2013.pptx
PPTX
PPT KKN PEDURUNGAN 2016.pptx
PPTX
Absis UNBK.pptx
PPTX
Dsc how google programs make great developer
PPTX
First Gathering Sandec
PPTX
Mockup Android Application Template Library
PPTX
Mockup Android Application : Go bon
PPTX
Lomba Sayembara Logo
PPTX
Template Video Invitation Walimatul Ursy
PPTX
Training Android Wonderkoding
PPTX
The Best Way to Become an Android Developer Expert with Android Jetpack
PPTX
Mengapa Perlu Belajar Coding?
PPTX
PPT Seminar TA Augmented Reality
Guideline Submission GitHub BFAA Dicoding
Proker Departemen Dakwah dan Syiar 2013.pptx
DKM_2013_BISMILLAH.pptx
Proker bendahara al muhandis 2013.ppt
PPT raker EKONOMI 2013.pptx
Program Kerja Kaderisasi Al Muhandis 2013
Departemen Mentoring.pptx
ANNISAA' 2013.pptx
PPT KKN PEDURUNGAN 2016.pptx
Absis UNBK.pptx
Dsc how google programs make great developer
First Gathering Sandec
Mockup Android Application Template Library
Mockup Android Application : Go bon
Lomba Sayembara Logo
Template Video Invitation Walimatul Ursy
Training Android Wonderkoding
The Best Way to Become an Android Developer Expert with Android Jetpack
Mengapa Perlu Belajar Coding?
PPT Seminar TA Augmented Reality

Recently uploaded (20)

PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Empathic Computing: Creating Shared Understanding
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
KodekX | Application Modernization Development
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Encapsulation theory and applications.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Building Integrated photovoltaic BIPV_UPV.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Chapter 3 Spatial Domain Image Processing.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Empathic Computing: Creating Shared Understanding
NewMind AI Weekly Chronicles - August'25 Week I
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Reach Out and Touch Someone: Haptics and Empathic Computing
KodekX | Application Modernization Development
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Encapsulation_ Review paper, used for researhc scholars
20250228 LYD VKU AI Blended-Learning.pptx
Machine learning based COVID-19 study performance prediction
Diabetes mellitus diagnosis method based random forest with bat algorithm
Encapsulation theory and applications.pdf
Big Data Technologies - Introduction.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows

Dts x dicoding #2 memulai pemrograman kotlin

  • 1. Memulai Pemrograman dengan Kotlin #2 Ahmad Arif Faizin Academy Content Writer Dicoding Indonesia x
  • 2. Tips Trik Seputar Instalasi - Pastikan menggunakan versi yang sama dengan yang ada di modul. - Jika sudah pernah menginstall Java sebelumnya, install dulu versi yang lama. - Invalidate cache/restart jika tidak bisa run EduTools. - Disable antivirus seperti Avast. - Jika errornya “Failed to launch checking”, coba hapus folder project dan ulangi mengambil course. - Pastikan koneksi lancar saat instalasi. - Kode merah-merah? pastikan besar kecil spasi sama dengan di modul. - Untuk pengguna 32 bit bisa mencoba install JDK lewat Azul.com
  • 5. Hello Kotlin! // main function fun main() { println("Hello Kotlin!") } fungsi pertama ketika program dijalankan untuk memberi komentar fungsi untuk mencetak teks ke layar/console
  • 6. println vs print fun main() { println("Kotlin, ") print("is Awesome!") } /* output: Kotlin, is Awesome */ fun main() { print("Kotlin, ") print("is Awesome!") } /* output: Kotlin, is Awesome */
  • 7. Contoh lain fun main() { val name = "Arif" print("Hello my name is ") println(name) print(if (true) "Always true" else "Always false") } /* output: Hello my name is Arif Always true */ cetak teks ke layar untuk membuat variabel dengan tipe data string untuk membuat percabangan cetak teks ke layar dan membuat baris baru
  • 8. Cara Menulis Variabel var namaVariabel: TipeData = isi(value) Contoh : var company: String = "Dicoding" atau bisa disingkat var company = "Dicoding" tipe data akan menyesuaikan dengan isinya
  • 9. var vs val Bisa diubah nilainya (mutable) var company = "Dicoding" company = "Dicoding Academy" Tidak bisa diubah nilainya (immutable) val company = "Dicoding" company = "Dicoding Academy" //Val cannot be reassigned
  • 10. Tipe Data Char hanya satu karakter ‘A’ String kumpulan dari banyak karakter (teks) “Kotlin” Array menyimpan banyak objek arrayOf(“Aku”, “Kamu”, “Dia”) Numbers angka 7 Boolean hanya memiliki 2 nilai true/false
  • 11. Char Contoh : val character: Char = 'A' Contoh salah val character: Char = 'ABC' // Incorrect character literal
  • 12. Char Operator increment & decrement fun main() { var vocal = 'A' println("Vocal " + vocal++) println("Vocal " + vocal--) println("Vocal " + vocal) } /* output: Vocal A Vocal B Vocal A */
  • 13. String Memasukkan variabel ke dalam String Contoh : fun main() { val text = "Kotlin" val firstChar = text[0] print("First character of" + text + " is " + firstChar) } print("First character of $text is $firstChar") string template
  • 14. String Membuat pengecualian(escaped) di dalam String Contoh : val statement = "Kotlin is "Awesome!""
  • 15. String Membuat Raw String Contoh : fun main() { val line = """ Line 1 Line 2 Line 3 Line 4 """.trimIndent() print(line) } /* output: Line 1 Line 2 Line 3 Line 4 */
  • 16. Array indeks dimulai dari 0 fun main() { val intArray = intArrayOf(1, 3, 5, 7) // [1, 3, 5, 7] intArray[2] = 11 // [1, 3, 11, 7] print(intArray[2]) } /* Output: 11 */ 1 3 5 7 ke-0 ke-1 ke-2 ke-3
  • 18. Numbers Int (32 bit) nilai angka yang umum digunakan val intNumbers = 100 Long (64 bit) nilai angka yang besar val longNumbers = 100L Short (16 bit) nilai angka yang kecil val shortNumber: Short = 10 Byte (8 bit) nilai angka yang sangat kecil val byteNumber = 0b11010010 Double (64 bit) nilai angka pecahan val doubleNumbers = 1.3
  • 19. Functions tanpa nilai kembalian fun main() { setUser("Arif", 17) } fun setUser(name: String, age: Int){ println("Your name is $name, and you $age years old") } parameter yang dikirimkan ke dalam fungsi untuk membuat fungsi
  • 20. Functions dengan nilai kembalian fun main() { val user = setUser("Arif", 17) println(user) } fun setUser(name: String, age: Int): String { return "Your name is $name, and you $age years old" } untuk membuat nilai kembalian tipe data nilai yang dikembalikan fun setUser(name: String, age: Int) = "Your name is $name, and you $age years old"
  • 21. If Expressions percabangan berdasarkan kondisi val openHours = 7 val now = 20 if (now > openHours){ println("office already open") } val openHours = 7 val now = 20 val office: String if (now > openHours) { office = "Office already open" } else { office = "Office is closed" } print(office) val openHours = 7 val now = 7 val office: String office = if (now > 7) { "Office already open" } else if (now == openHours){ "Wait a minute, office will be open" } else { "Office is closed" } print(office)
  • 22. Nullable Types Contoh: val text: String? = null Contoh salah: val text: String = null // compile time error
  • 23. Safe Call Operator ?. memanggil nullable dengan aman val text: String? = null text?.length val text: String? = null val textLength = text?.length ?: 7 Elvis Operator ?: nilai default jika objek bernilai null
  • 26. When Expression Percabangan selain If Else fun main() { val value = 7 val stringOfValue = when (value) { 6 -> "value is 6" 7 -> "value is 7" 8 -> "value is 8" else -> "value cannot be reached" } println(stringOfValue) } /* output : value is 7 */
  • 27. When Expression val anyType : Any = 100L when(anyType){ is Long -> println("the value has a Long type") is String -> println("the value has a String type") else -> println("undefined") } val value = 27 val ranges = 10..50 when(value){ in ranges -> println("value is in the range") !in ranges -> println("value is outside the range") else -> println("value undefined") } val x = 11 when (x) { 10, 11 -> print("the value between 10 and 11") 11, 12 -> print("the value between 11 and 12") } val x = 10 when (x) { 5 + 5 -> print("the value is 10") 10 + 10 -> print("the value is 20") }
  • 28. Enumeration fun main() { val color: Color = Color.GREEN when(color){ Color.RED -> print("Color is Red") Color.BLUE -> print("Color is Blue") Color.GREEN -> print("Color is Green") } } enum class Color(val value: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } //output : Color is Green
  • 29. While Perulangan fun main() { var counter = 1 while (counter <= 5){ println("Hello, World!") counter++ } } fun main() { println("Hello World") println("Hello World") println("Hello World") println("Hello World") println("Hello World") } =
  • 30. fun main() { var counter = 1 while (counter <= 7){ println("Hello, World!") counter++ } } cek dulu baru jalankan While vs Do While fun main() { var counter = 1 do { println("Hello, World!") counter++ } while (counter <= 7) } jalankan dulu baru cek
  • 31. Hati - Hati dengan infinite loop Karena kondisi tidak terpenuhi var value = 'A' do { print(value) } while (value <= 'Z') var i = 3 while (i > 3) { println(i) i-- }
  • 32. val rangeInt = 1.rangeTo(10) = val rangeInt = 1..10 = val rangeInt = 1 until 10 dari bawah ke atas rangeTo vs downTo val downInt = 10.downTo(1) dari atas ke bawah
  • 33. For Loop Perulangan dengan counter otomatis fun main() { val ranges = 1..5 for (i in ranges){ println("value is $i!") } } fun main() { var counter = 1 while (counter <= 5){ println("value is $counter!") counter++ } } =
  • 34. For Each Perulangan dengan counter otomatis fun main() { val ranges = 1..5 ranges.forEach{ println("value is $i!") } } fun main() { val ranges = 1..5 for (i in ranges){ println("value is $i!") } } =
  • 35. Break & Continue Contoh program deteksi angka ganjil sampai angka 10 fun main() { val listNumber = 1.rangeTo(50) for (number in listNumber) { if (number % 2 == 0) continue if (number > 10) break println("$number adalah bilangan ganjil") } } * % adalah modulus / sisa pembagian misal 10 % 2 == 0, 11 % 2 = 1 , 9 % 6 = 3 /* output: 1 adalah bilangan ganjil 3 adalah bilangan ganjil 5 adalah bilangan ganjil 7 adalah bilangan ganjil 9 adalah bilangan ganjil */
  • 37. OUR LIFE SHOULD BE JUST LIKE MODULUS. DOESN’T MATTER WHAT VALUE IS ENTERED, IT’S RESULT ALWAYS POSITIVE. AND SO SHOULD BE OUR LIFE IN EVERY SITUATION - Pranay Neve ’’ ’’
  • 38. You can find me at: ● Google : Ahmad Arif Faizin ● Discord : @arifaizin Thanks! Any questions?