Difference between revisions of "KOTLIN: For Loop"
Onnowpurbo (talk | contribs) (Created page with "Sumber: https://www.tutorialspoint.com/kotlin/kotlin_for_loop.htm What are loops? Imagine a situation when you need to print a sentence 20 times on your screen. You can do it...") |
Onnowpurbo (talk | contribs) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
Sumber: https://www.tutorialspoint.com/kotlin/kotlin_for_loop.htm | Sumber: https://www.tutorialspoint.com/kotlin/kotlin_for_loop.htm | ||
− | What are loops? | + | ==What are loops?== |
+ | |||
Imagine a situation when you need to print a sentence 20 times on your screen. You can do it by using print statement 20 times. What about if you need to print the same sentence one thousand times? This is where we need to use loops to simplify the programming job. Actually, Loops are used in programming to repeat a specific block of code until certain condition is met. | Imagine a situation when you need to print a sentence 20 times on your screen. You can do it by using print statement 20 times. What about if you need to print the same sentence one thousand times? This is where we need to use loops to simplify the programming job. Actually, Loops are used in programming to repeat a specific block of code until certain condition is met. | ||
Kotlin supports various types of loops and in this chapter we are going to learn Kotlin for loop. | Kotlin supports various types of loops and in this chapter we are going to learn Kotlin for loop. | ||
− | Kotlin For Loop | + | |
+ | |||
+ | Bayangkan situasi ketika Anda perlu mencetak kalimat 20 kali di layar Anda. Anda dapat melakukannya dengan menggunakan pernyataan cetak 20 kali. Bagaimana jika Anda perlu mencetak kalimat yang sama seribu kali? Di sinilah kita perlu menggunakan loop untuk menyederhanakan pekerjaan pemrograman. Sebenarnya, Loop digunakan dalam pemrograman untuk mengulang blok kode tertentu sampai kondisi tertentu terpenuhi. | ||
+ | |||
+ | Kotlin mendukung berbagai jenis loop dan dalam bab ini kita akan mempelajari Kotlin untuk loop. | ||
+ | |||
+ | |||
+ | |||
+ | ==Kotlin For Loop== | ||
+ | |||
Kotlin for loop iterates through anything that provides an iterator ie. that contains a countable number of values, for example arrays, ranges, maps or any other collection available in Kotlin. Kotlin for loop is equivalent to the foreach loop in languages like C#. | Kotlin for loop iterates through anything that provides an iterator ie. that contains a countable number of values, for example arrays, ranges, maps or any other collection available in Kotlin. Kotlin for loop is equivalent to the foreach loop in languages like C#. | ||
Kotlin does not provide a conventional for loop which is available in C, C++ and Java etc. | Kotlin does not provide a conventional for loop which is available in C, C++ and Java etc. | ||
− | Syntax | + | |
+ | |||
+ | Kotlin untuk loop berulang melalui apa pun yang menyediakan iterator yaitu. yang berisi jumlah nilai yang dapat dihitung, misalnya larik, rentang, peta, atau koleksi lain yang tersedia di Kotlin. Kotlin for loop setara dengan loop foreach dalam bahasa seperti C#. | ||
+ | |||
+ | Kotlin tidak menyediakan for loop konvensional yang tersedia di C, C++ dan Java dll. | ||
+ | |||
+ | |||
+ | ===Syntax=== | ||
+ | |||
The syntax of the Kotlin for loop is as follows: | The syntax of the Kotlin for loop is as follows: | ||
− | for (item in collection) { | + | Sintaks dari loop for Kotlin adalah sebagai berikut: |
− | + | ||
− | } | + | |
− | Iterate Through a Range | + | for (item in collection) { |
+ | // body of loop | ||
+ | } | ||
+ | |||
+ | ==Iterate Through a Range== | ||
+ | |||
We will study Kotlin Ranges in a separate chapter, for now you should know that Kotlin Ranges provide iterator, so we can iterate through a range using for loop. | We will study Kotlin Ranges in a separate chapter, for now you should know that Kotlin Ranges provide iterator, so we can iterate through a range using for loop. | ||
Following is an example where the loop iterates through the range and prints individual item. To iterate over a range of numbers, we will use a range expression: | Following is an example where the loop iterates through the range and prints individual item. To iterate over a range of numbers, we will use a range expression: | ||
− | fun main(args: Array<String>) { | + | |
− | + | Kami akan mempelajari Rentang Kotlin dalam bab terpisah, untuk sekarang Anda harus tahu bahwa Rentang Kotlin menyediakan iterator, sehingga kami dapat mengulangi melalui rentang menggunakan for loop. | |
− | + | ||
− | + | Berikut ini adalah contoh di mana loop berulang melalui rentang dan mencetak item individual. Untuk mengulangi rentang angka, kami akan menggunakan ekspresi rentang: | |
− | } | + | |
+ | |||
+ | |||
+ | fun main(args: Array<String>) { | ||
+ | for (item in 1..5) { | ||
+ | println(item) | ||
+ | } | ||
+ | } | ||
+ | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | 1 | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | 2 | + | |
− | 3 | + | |
− | 4 | + | 1 |
− | 5 | + | 2 |
+ | 3 | ||
+ | 4 | ||
+ | 5 | ||
+ | |||
Let's see one more example where the loop will iterate through another range, but this time it will step down instead of stepping up as in the above example: | Let's see one more example where the loop will iterate through another range, but this time it will step down instead of stepping up as in the above example: | ||
− | fun main(args: Array<String>) { | + | Mari kita lihat satu contoh lagi di mana loop akan beralih melalui rentang lain, tetapi kali ini akan turun alih-alih naik seperti pada contoh di atas: |
− | + | ||
− | + | ||
− | + | fun main(args: Array<String>) { | |
− | } | + | for (item in 5 downTo 1 step 2) { |
+ | println(item) | ||
+ | } | ||
+ | } | ||
+ | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | 5 | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | 3 | + | |
− | 1 | + | |
− | Iterate Through a Array | + | 5 |
+ | 3 | ||
+ | 1 | ||
+ | |||
+ | ==Iterate Through a Array== | ||
+ | |||
Kotlin Array is another data type which provides iterator, so we can use for loop to iterate through a Kotlin array in the similar way as we did it for the ranges. | Kotlin Array is another data type which provides iterator, so we can use for loop to iterate through a Kotlin array in the similar way as we did it for the ranges. | ||
Following is an example where we used for loop to iterate through an array of strings: | Following is an example where we used for loop to iterate through an array of strings: | ||
− | fun main(args: Array<String>) { | + | |
− | + | Kotlin Array adalah tipe data lain yang menyediakan iterator, jadi kita dapat menggunakan loop for untuk melakukan iterasi melalui array Kotlin dengan cara yang sama seperti yang kita lakukan untuk rentang. | |
− | + | ||
− | + | Berikut ini adalah contoh di mana kami menggunakan for loop untuk beralih melalui array string: | |
− | + | ||
− | + | ||
− | } | + | fun main(args: Array<String>) { |
+ | var fruits = arrayOf("Orange", "Apple", "Mango", "Banana") | ||
+ | |||
+ | for (item in fruits) { | ||
+ | println(item) | ||
+ | } | ||
+ | } | ||
+ | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | Orange | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | Apple | + | |
− | Mango | + | Orange |
− | Banana | + | Apple |
+ | Mango | ||
+ | Banana | ||
+ | |||
Let's see the same example once again, but this time we will iterate through the array using its index. | Let's see the same example once again, but this time we will iterate through the array using its index. | ||
− | fun main(args: Array<String>) { | + | Mari kita lihat contoh yang sama sekali lagi, tetapi kali ini kita akan mengulangi array menggunakan indeksnya. |
− | + | ||
− | + | ||
− | + | fun main(args: Array<String>) { | |
− | + | var fruits = arrayOf("Orange", "Apple", "Mango", "Banana") | |
− | + | ||
− | } | + | for (index in fruits.indices) { |
+ | println(fruits[index]) | ||
+ | } | ||
+ | } | ||
+ | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | Orange | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | Apple | + | |
− | Mango | + | |
− | Banana | + | Orange |
− | Quiz Time (Interview & Exams Preparation) | + | Apple |
+ | Mango | ||
+ | Banana | ||
+ | |||
+ | ==Quiz Time (Interview & Exams Preparation)== | ||
+ | |||
Q 1 - Which of the following is true about Kotlin for loop? | Q 1 - Which of the following is true about Kotlin for loop? | ||
Latest revision as of 11:09, 25 July 2022
Sumber: https://www.tutorialspoint.com/kotlin/kotlin_for_loop.htm
What are loops?
Imagine a situation when you need to print a sentence 20 times on your screen. You can do it by using print statement 20 times. What about if you need to print the same sentence one thousand times? This is where we need to use loops to simplify the programming job. Actually, Loops are used in programming to repeat a specific block of code until certain condition is met.
Kotlin supports various types of loops and in this chapter we are going to learn Kotlin for loop.
Bayangkan situasi ketika Anda perlu mencetak kalimat 20 kali di layar Anda. Anda dapat melakukannya dengan menggunakan pernyataan cetak 20 kali. Bagaimana jika Anda perlu mencetak kalimat yang sama seribu kali? Di sinilah kita perlu menggunakan loop untuk menyederhanakan pekerjaan pemrograman. Sebenarnya, Loop digunakan dalam pemrograman untuk mengulang blok kode tertentu sampai kondisi tertentu terpenuhi.
Kotlin mendukung berbagai jenis loop dan dalam bab ini kita akan mempelajari Kotlin untuk loop.
Kotlin For Loop
Kotlin for loop iterates through anything that provides an iterator ie. that contains a countable number of values, for example arrays, ranges, maps or any other collection available in Kotlin. Kotlin for loop is equivalent to the foreach loop in languages like C#.
Kotlin does not provide a conventional for loop which is available in C, C++ and Java etc.
Kotlin untuk loop berulang melalui apa pun yang menyediakan iterator yaitu. yang berisi jumlah nilai yang dapat dihitung, misalnya larik, rentang, peta, atau koleksi lain yang tersedia di Kotlin. Kotlin for loop setara dengan loop foreach dalam bahasa seperti C#.
Kotlin tidak menyediakan for loop konvensional yang tersedia di C, C++ dan Java dll.
Syntax
The syntax of the Kotlin for loop is as follows:
Sintaks dari loop for Kotlin adalah sebagai berikut:
for (item in collection) { // body of loop }
Iterate Through a Range
We will study Kotlin Ranges in a separate chapter, for now you should know that Kotlin Ranges provide iterator, so we can iterate through a range using for loop.
Following is an example where the loop iterates through the range and prints individual item. To iterate over a range of numbers, we will use a range expression:
Kami akan mempelajari Rentang Kotlin dalam bab terpisah, untuk sekarang Anda harus tahu bahwa Rentang Kotlin menyediakan iterator, sehingga kami dapat mengulangi melalui rentang menggunakan for loop.
Berikut ini adalah contoh di mana loop berulang melalui rentang dan mencetak item individual. Untuk mengulangi rentang angka, kami akan menggunakan ekspresi rentang:
fun main(args: Array<String>) { for (item in 1..5) { println(item) } }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
1 2 3 4 5
Let's see one more example where the loop will iterate through another range, but this time it will step down instead of stepping up as in the above example:
Mari kita lihat satu contoh lagi di mana loop akan beralih melalui rentang lain, tetapi kali ini akan turun alih-alih naik seperti pada contoh di atas:
fun main(args: Array<String>) { for (item in 5 downTo 1 step 2) { println(item) } }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
5 3 1
Iterate Through a Array
Kotlin Array is another data type which provides iterator, so we can use for loop to iterate through a Kotlin array in the similar way as we did it for the ranges.
Following is an example where we used for loop to iterate through an array of strings:
Kotlin Array adalah tipe data lain yang menyediakan iterator, jadi kita dapat menggunakan loop for untuk melakukan iterasi melalui array Kotlin dengan cara yang sama seperti yang kita lakukan untuk rentang.
Berikut ini adalah contoh di mana kami menggunakan for loop untuk beralih melalui array string:
fun main(args: Array<String>) { var fruits = arrayOf("Orange", "Apple", "Mango", "Banana") for (item in fruits) { println(item) } }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
Orange Apple Mango Banana
Let's see the same example once again, but this time we will iterate through the array using its index.
Mari kita lihat contoh yang sama sekali lagi, tetapi kali ini kita akan mengulangi array menggunakan indeksnya.
fun main(args: Array<String>) { var fruits = arrayOf("Orange", "Apple", "Mango", "Banana") for (index in fruits.indices) { println(fruits[index]) } }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
Orange Apple Mango Banana
Quiz Time (Interview & Exams Preparation)
Q 1 - Which of the following is true about Kotlin for loop?
A - It is used to loop through an iterator.
B - Kotlin does not provide conventional for loop like C, C++ or Java.
C - Kotlin for loop is equivalent to the foreach loop in languages like C#.
D - All of the above
Q 2 - What will be the last number printed by the following for loop?
fun main(args: Array<String>) {
for (item in 6 downTo 1 step 2) { println(item) }
} A - 6
B - 5
C - 3
D - 2