<p class="p1">The for loop in Kotlin iterates through anything that provides an iterator. In this article, you learn to create for loop (with the help of examples).</p>
<h4><span style="color: #0000ff;"><strong>Syntax</strong></span></h4>
<p>The syntax of for loop in Kotlin is:</p>
<pre>for (item in collection) {
 <strong><span style="color: #008000;">// body of loop</span></strong>
}</pre>
<p class="p1">In Kotlin, for loop is used to iterate through the following because all of them provide iterator.</p>
<ul class="ul1">
<li class="li1"><strong><span style="color: #0000ff;">Range</span></strong></li>
<li class="li1"><strong><span style="color: #0000ff;">Array</span></strong></li>
<li class="li1"><strong><span style="color: #0000ff;">String</span></strong></li>
<li class="li1"><strong><span style="color: #0000ff;">Collection</span></strong></li>
</ul>
<h5></h5>
<h4 class="p1"><span style="color: #000080;"><b>Iterate through <span style="color: #0000ff;">range</span> using for loop</b></span></h4>
<p>In Kotlin, the range is a collection of finite values which is defined by endpoints (start and stop).The start and stop are inclusive in the Range.</p>
<p>Given below are the examples of traversing the range in different ways:</p>
<h5 class="p1"><span style="color: #0000ff;"><b>Iterate through range to print the n <span style="color: #008000;">values</span>:</b></span></h5>
<pre>fun main(args: Array<;String>;)
{<strong><span style="color: #008000;">
//the loop iterates through the range and prints individual item.</span></strong>
for (i in 1..5) { 
 print("$i ") 
 } 
}</pre>
<h5><span style="color: #0000ff;"><strong>Output:</strong></span></h5>
<pre>1 2 3 4 5</pre>
<h5 class="p1"><span style="color: #0000ff;"><b>Iterate through range to print values only up to <span style="color: #008000;">n-1<span style="color: #0000ff;"> using <span style="color: #008000;">until:</span></span></span></b></span></h5>
<pre> for (i in 1 until 5) {
 print("$i ")
}</pre>
<h5><span style="color: #0000ff;"><strong>Output:</strong></span></h5>
<pre>1 2 3 4</pre>
<h4></h4>
<h5 class="p1"><span style="color: #0000ff;"><b>Iterate through range to jump using <span style="color: #008000;">step</span> :</b></span></h5>
<p>In Kotlin, you can control the increment with <span style="color: #008000;"><b>step</b><span style="color: #004000;"> and the</span></span> step value must be positive.</p>
<pre>fun main(args: Array<;String>;)
{
<span style="color: #008000;"><strong> //increment the value of i by 2</strong></span>
 for (i in 1..10 step 2) {
 print("$i ")
 }
}</pre>
<h5><span style="color: #0000ff;"><strong>Output:</strong></span></h5>
<pre>1 3 5 7 9</pre>
<h5></h5>
<h5 class="p1"><span style="color: #0000ff;"><b>Iterate through Range from top to down using <span style="color: #008000;">downTo</span>: </b></span></h5>
<pre><span style="color: #008000;"><strong>//Iterate through Range from top to down without using downTo 
//prints nothing
</strong></span>
for (i in 10..1) {
 print("$i ")
}</pre>
<p>Using <strong><span style="color: #008000;">downTo</span> </strong>operator, gives you the expected result:</p>
<pre>for (i in 10 downTo 1) {
 print("$i ")
}</pre>
<h5><span style="color: #0000ff;"><strong>Output:</strong></span></h5>
<pre>10 9 8 7 6 5 4 3 2 1</pre>
<h5 class="p1"><span style="color: #0000ff;"><b>Iterate through Range from top to down using <span style="color: #008000;">downTo and step 3</span>:</b></span></h5>
<pre>for (i in 10 downTo 1 step 3) {
 print("$i ")
}</pre>
<h5><span style="color: #0000ff;"><strong>Output:</strong></span></h5>
<pre>10 7 4 1</pre>
<h4></h4>
<h4 class="p1"><span style="color: #000080;"><b>Iterate through <span style="color: #0000ff;">array</span> using for loop </b></span></h4>
<p>An array is a collection of data of similar data type like Integer or String.</p>
<p>In an array, you can traverse in the following ways:</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<ul>
<li>Without using Index property</li>
<li>Using Index property</li>
<li>Using <strong><span style="color: #008000;">withIndex()</span> </strong>function</li>
</ul>
<h4></h4>
<h5 class="p1"><span style="color: #0000ff;"><b>Traverse an array without using index property:</b></span></h5>
<pre>fun main(args: Array<;String>;) {

 var numbers = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

 for (num in numbers) {
 print("$num ")
 }
}</pre>
<h5><span style="color: #0000ff;"><strong>Output:</strong></span></h5>
<pre>1 2 3 4 5 6 7 8 9 10</pre>
<h4></h4>
<h5 class="p1"><span style="color: #0000ff;"><b>Traverse an array using index property:</b></span></h5>
<pre>fun main(args: Array<;String>;) {
 
 var days = arrayOf("Sunday", "Monday", "Tuesday", "wednesday", "Thursday", "Friday", "saturday")

 for (day in days.indices) {
 println(days[day])
 }
}</pre>
<h5><span style="color: #0000ff;"><strong>Output:</strong></span></h5>
<pre>Sunday
Monday
Tuesday
wednesday
Thursday
Friday
saturday</pre>
<h4></h4>
<h5 class="p1"><strong><span style="color: #0000ff;">Traverse an array using withIndex() function:</span></strong></h5>
<pre>fun main(args: Array<;String>;) {
 
 var days = arrayOf("Sunday", "Monday", "Tuesday", "wednesday", "Thursday", "Friday", "saturday")

 for (day in days.withIndex()) {
 println("Item at ${day.index} index is ${day.value}")
 }

 <strong><span style="color: #008000;"> //OR</span></strong>
 /*for ((index,value) in days.withIndex()) {
 println("Item at $index index is $value")*/
 }</pre>
<h5><span style="color: #0000ff;"><strong>Output:</strong></span></h5>
<pre>Item at 0 index is Sunday
Item at 1 index is Monday
Item at 2 index is Tuesday
Item at 3 index is wednesday
Item at 4 index is Thursday
Item at 5 index is Friday
Item at 6 index is saturday</pre>
<h4></h4>
<h4 class="p1"><span style="color: #000080;"><b>Iterate through <span style="color: #0000ff;">string</span> using for loop </b></span></h4>
<p><strong><span style="color: #008000;">Strings</span> </strong>are defined as an array of characters and is terminated with a special character ‘\0’.</p>
<p>These are the following ways to traverse the string:</p>
<ul>
<li class="p1"><span style="color: #000000;"> Without using index property:</span></li>
<li class="p1"><span style="color: #000000;"> Using index property:</span></li>
<li class="p1"><span style="color: #000000;"> Using <span style="color: #008000;"><strong>withIndex()</strong></span> function:</span></li>
</ul>
<pre>package kotlinforloop

fun main(args: Array<;String>;) {

 var name = "C1C"
 var name2 = "TECH"
 var name3 = "C1CTECH"


 <strong><span style="color: #008000;">// traversing string without using index property</span></strong>
 for (i in name)
 print("$i ")


 <strong><span style="color: #008000;"> // traversing string using index property</span></strong>
 for (i in name2.indices)
 print(name2[i] + " ")

 println()

 <strong><span style="color: #008000;">// traversing string using withIndex() function</span></strong>
 for ((index, value) in name3.withIndex())
 println("Item at $index index is $value")

 <strong><span style="color: #0000ff;">//OR</span></strong>
 /*for (data in name3.withIndex())
 println("Item at ${data.index} index is ${data.value}")*/

}</pre>
<h3></h3>
<h5><span style="color: #0000ff;"><strong>Output:</strong></span></h5>
<pre>C 1 C T E C H 
Item at 0 index is C
Item at 1 index is 1
Item at 2 index is C
Item at 3 index is T
Item at 4 index is E
Item at 5 index is C
Item at 6 index is H
</pre>
<h4></h4>
<h4 class="p1"><span style="color: #000080;"><b>Iterate through <span style="color: #0000ff;">collection</span> using for loop</b></span></h4>
<p>A collection usually contains a number of objects of the same type and these objects in the collection are called elements or items. You can traverse through collection<strong><span style="color: #008000;"> (list, map, set)</span></strong> using the for loop.</p>
<p>In Kotlin, <span style="color: #008000;"><strong>listOf()</strong></span> is used to create a list and we can pass different data types at the same time.</p>
<pre>fun main(args: Array<;String>;) {
<strong><span style="color: #008000;"> //read only, fix-size</span></strong>
 var collection = listOf(1, 3.2, 9.9, "Donald", "Ankita", "Rohan")

 for (element in collection) {
 println(element)
 }

}</pre>
<h5><span style="color: #0000ff;"><strong>Output:</strong></span></h5>
<pre>1
3.2
9.9
Donald
Ankita
Rohan</pre>
<p> ;</p>


