Site icon C1CTech

Kotlin for Loop

<p class&equals;"p1">The for loop in Kotlin iterates through anything that provides an iterator&period; In this article&comma; you learn to create for loop &lpar;with the help of examples&rpar;&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Syntax<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>The syntax of for loop in Kotlin is&colon;<&sol;p>&NewLine;<pre>for &lpar;item in collection&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; body of loop<&sol;span><&sol;strong>&NewLine;&rcub;<&sol;pre>&NewLine;<p class&equals;"p1">In Kotlin&comma; for loop is used to iterate through the following because all of them provide iterator&period;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Range<&sol;span><&sol;strong><&sol;li>&NewLine;<li class&equals;"li1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Array<&sol;span><&sol;strong><&sol;li>&NewLine;<li class&equals;"li1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">String<&sol;span><&sol;strong><&sol;li>&NewLine;<li class&equals;"li1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Collection<&sol;span><&sol;strong><&sol;li>&NewLine;<&sol;ul>&NewLine;<h5><&sol;h5>&NewLine;<h4 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><b>Iterate through <span style&equals;"color&colon; &num;0000ff&semi;">range<&sol;span> using for loop<&sol;b><&sol;span><&sol;h4>&NewLine;<p>In Kotlin&comma; the range is a collection of finite values which is defined by endpoints &lpar;start and stop&rpar;&period;The start and stop are inclusive in the Range&period;<&sol;p>&NewLine;<p>Given below are the examples of traversing the range in different ways&colon;<&sol;p>&NewLine;<h5 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Iterate through range to print the n <span style&equals;"color&colon; &num;008000&semi;">values<&sol;span>&colon;<&sol;b><&sol;span><&sol;h5>&NewLine;<pre>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar;&NewLine;&lbrace;<strong><span style&equals;"color&colon; &num;008000&semi;">&NewLine;&sol;&sol;the loop iterates through the range and prints individual item&period;<&sol;span><&sol;strong>&NewLine;for &lpar;i in 1&period;&period;5&rpar; &lbrace; &NewLine; print&lpar;"&dollar;i "&rpar; &NewLine; &rcub; &NewLine;&rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>1 2 3 4 5<&sol;pre>&NewLine;<h5 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Iterate through range to print values only up to <span style&equals;"color&colon; &num;008000&semi;">n-1<span style&equals;"color&colon; &num;0000ff&semi;"> using <span style&equals;"color&colon; &num;008000&semi;">until&colon;<&sol;span><&sol;span><&sol;span><&sol;b><&sol;span><&sol;h5>&NewLine;<pre> for &lpar;i in 1 until 5&rpar; &lbrace;&NewLine; print&lpar;"&dollar;i "&rpar;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>1 2 3 4<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h5 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Iterate through range to jump using <span style&equals;"color&colon; &num;008000&semi;">step<&sol;span> &colon;<&sol;b><&sol;span><&sol;h5>&NewLine;<p>In Kotlin&comma; you can control the increment with <span style&equals;"color&colon; &num;008000&semi;"><b>step<&sol;b><span style&equals;"color&colon; &num;004000&semi;"> and the<&sol;span><&sol;span> step value must be positive&period;<&sol;p>&NewLine;<pre>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar;&NewLine;&lbrace;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;increment the value of i by 2<&sol;strong><&sol;span>&NewLine; for &lpar;i in 1&period;&period;10 step 2&rpar; &lbrace;&NewLine; print&lpar;"&dollar;i "&rpar;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>1 3 5 7 9<&sol;pre>&NewLine;<h5><&sol;h5>&NewLine;<h5 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Iterate through Range from top to down using <span style&equals;"color&colon; &num;008000&semi;">downTo<&sol;span>&colon;   <&sol;b><&sol;span><&sol;h5>&NewLine;<pre><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Iterate through Range from top to down without using downTo &NewLine;&sol;&sol;prints nothing&NewLine;<&sol;strong><&sol;span>&NewLine;for &lpar;i in 10&period;&period;1&rpar; &lbrace;&NewLine; print&lpar;"&dollar;i "&rpar;&NewLine;&rcub;<&sol;pre>&NewLine;<p>Using <strong><span style&equals;"color&colon; &num;008000&semi;">downTo<&sol;span> <&sol;strong>operator&comma; gives you the expected result&colon;<&sol;p>&NewLine;<pre>for &lpar;i in 10 downTo 1&rpar; &lbrace;&NewLine; print&lpar;"&dollar;i "&rpar;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>10 9 8 7 6 5 4 3 2 1<&sol;pre>&NewLine;<h5 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Iterate through Range from top to down using <span style&equals;"color&colon; &num;008000&semi;">downTo and step 3<&sol;span>&colon;<&sol;b><&sol;span><&sol;h5>&NewLine;<pre>for &lpar;i in 10 downTo 1 step 3&rpar; &lbrace;&NewLine; print&lpar;"&dollar;i "&rpar;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>10 7 4 1<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><b>Iterate through <span style&equals;"color&colon; &num;0000ff&semi;">array<&sol;span> using for loop <&sol;b><&sol;span><&sol;h4>&NewLine;<p>An array is a collection of data of similar data type like Integer or String&period;<&sol;p>&NewLine;<p>In an array&comma; you can traverse in the following ways&colon;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<ul>&NewLine;<li>Without using Index property<&sol;li>&NewLine;<li>Using Index property<&sol;li>&NewLine;<li>Using <strong><span style&equals;"color&colon; &num;008000&semi;">withIndex&lpar;&rpar;<&sol;span> <&sol;strong>function<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><&sol;h4>&NewLine;<h5 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Traverse an array without using index property&colon;<&sol;b><&sol;span><&sol;h5>&NewLine;<pre>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine;&NewLine; var numbers &equals; arrayOf&lpar;1&comma; 2&comma; 3&comma; 4&comma; 5&comma; 6&comma; 7&comma; 8&comma; 9&comma; 10&rpar;&NewLine;&NewLine; for &lpar;num in numbers&rpar; &lbrace;&NewLine; print&lpar;"&dollar;num "&rpar;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>1 2 3 4 5 6 7 8 9 10<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h5 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Traverse an array using index property&colon;<&sol;b><&sol;span><&sol;h5>&NewLine;<pre>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine; &NewLine; var days &equals; arrayOf&lpar;"Sunday"&comma; "Monday"&comma; "Tuesday"&comma; "wednesday"&comma; "Thursday"&comma; "Friday"&comma; "saturday"&rpar;&NewLine;&NewLine; for &lpar;day in days&period;indices&rpar; &lbrace;&NewLine; println&lpar;days&lbrack;day&rsqb;&rpar;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>Sunday&NewLine;Monday&NewLine;Tuesday&NewLine;wednesday&NewLine;Thursday&NewLine;Friday&NewLine;saturday<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h5 class&equals;"p1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Traverse an array using withIndex&lpar;&rpar; function&colon;<&sol;span><&sol;strong><&sol;h5>&NewLine;<pre>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine; &NewLine; var days &equals; arrayOf&lpar;"Sunday"&comma; "Monday"&comma; "Tuesday"&comma; "wednesday"&comma; "Thursday"&comma; "Friday"&comma; "saturday"&rpar;&NewLine;&NewLine; for &lpar;day in days&period;withIndex&lpar;&rpar;&rpar; &lbrace;&NewLine; println&lpar;"Item at &dollar;&lbrace;day&period;index&rcub; index is &dollar;&lbrace;day&period;value&rcub;"&rpar;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;OR<&sol;span><&sol;strong>&NewLine; &sol;&ast;for &lpar;&lpar;index&comma;value&rpar; in days&period;withIndex&lpar;&rpar;&rpar; &lbrace;&NewLine; println&lpar;"Item at &dollar;index index is &dollar;value"&rpar;&ast;&sol;&NewLine; &rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>Item at 0 index is Sunday&NewLine;Item at 1 index is Monday&NewLine;Item at 2 index is Tuesday&NewLine;Item at 3 index is wednesday&NewLine;Item at 4 index is Thursday&NewLine;Item at 5 index is Friday&NewLine;Item at 6 index is saturday<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><b>Iterate through <span style&equals;"color&colon; &num;0000ff&semi;">string<&sol;span> using for loop <&sol;b><&sol;span><&sol;h4>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">Strings<&sol;span> <&sol;strong>are defined as an array of characters and is terminated with a special character &OpenCurlyQuote;&bsol;0’&period;<&sol;p>&NewLine;<p>These are the following ways to traverse the string&colon;<&sol;p>&NewLine;<ul>&NewLine;<li class&equals;"p1"><span style&equals;"color&colon; &num;000000&semi;"> Without using index property&colon;<&sol;span><&sol;li>&NewLine;<li class&equals;"p1"><span style&equals;"color&colon; &num;000000&semi;"> Using index property&colon;<&sol;span><&sol;li>&NewLine;<li class&equals;"p1"><span style&equals;"color&colon; &num;000000&semi;"> Using <span style&equals;"color&colon; &num;008000&semi;"><strong>withIndex&lpar;&rpar;<&sol;strong><&sol;span> function&colon;<&sol;span><&sol;li>&NewLine;<&sol;ul>&NewLine;<pre>package kotlinforloop&NewLine;&NewLine;fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine;&NewLine; var name &equals; "C1C"&NewLine; var name2 &equals; "TECH"&NewLine; var name3 &equals; "C1CTECH"&NewLine;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; traversing string without using index property<&sol;span><&sol;strong>&NewLine; for &lpar;i in name&rpar;&NewLine; print&lpar;"&dollar;i "&rpar;&NewLine;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; traversing string using index property<&sol;span><&sol;strong>&NewLine; for &lpar;i in name2&period;indices&rpar;&NewLine; print&lpar;name2&lbrack;i&rsqb; &plus; " "&rpar;&NewLine;&NewLine; println&lpar;&rpar;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; traversing string using withIndex&lpar;&rpar; function<&sol;span><&sol;strong>&NewLine; for &lpar;&lpar;index&comma; value&rpar; in name3&period;withIndex&lpar;&rpar;&rpar;&NewLine; println&lpar;"Item at &dollar;index index is &dollar;value"&rpar;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol;OR<&sol;span><&sol;strong>&NewLine; &sol;&ast;for &lpar;data in name3&period;withIndex&lpar;&rpar;&rpar;&NewLine; println&lpar;"Item at &dollar;&lbrace;data&period;index&rcub; index is &dollar;&lbrace;data&period;value&rcub;"&rpar;&ast;&sol;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<h3><&sol;h3>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>C 1 C T E C H &NewLine;Item at 0 index is C&NewLine;Item at 1 index is 1&NewLine;Item at 2 index is C&NewLine;Item at 3 index is T&NewLine;Item at 4 index is E&NewLine;Item at 5 index is C&NewLine;Item at 6 index is H&NewLine;<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><b>Iterate through <span style&equals;"color&colon; &num;0000ff&semi;">collection<&sol;span> using for loop<&sol;b><&sol;span><&sol;h4>&NewLine;<p>A collection usually contains a number of objects of the same type and these objects in the collection are called elements or items&period; You can traverse through collection<strong><span style&equals;"color&colon; &num;008000&semi;"> &lpar;list&comma; map&comma; set&rpar;<&sol;span><&sol;strong> using the for loop&period;<&sol;p>&NewLine;<p>In Kotlin&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>listOf&lpar;&rpar;<&sol;strong><&sol;span> is used to create a list and we can pass different data types at the same time&period;<&sol;p>&NewLine;<pre>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;read only&comma; fix-size<&sol;span><&sol;strong>&NewLine; var collection &equals; listOf&lpar;1&comma; 3&period;2&comma; 9&period;9&comma; "Donald"&comma; "Ankita"&comma; "Rohan"&rpar;&NewLine;&NewLine; for &lpar;element in collection&rpar; &lbrace;&NewLine; println&lpar;element&rpar;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>1&NewLine;3&period;2&NewLine;9&period;9&NewLine;Donald&NewLine;Ankita&NewLine;Rohan<&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version