- Kotlin - Exception Handling
- Kotlin - Destructuring Declarations
- Kotlin - Delegation
- Kotlin - Generics
- Kotlin - Sealed Class
- Kotlin - Data Classes
- Kotlin - Extension
- Kotlin - Visibility Control
- Kotlin - Interface
- Kotlin - Abstract Classes
- Kotlin - Inheritance
- Kotlin - Constructors
- Kotlin - Class and Objects
- Kotlin - Maps
- Kotlin - Sets
- Kotlin - Lists
- Kotlin - Collections
- Kotlin - Break and Continue
- Kotlin - While Loop
- Kotlin - For Loop
- Kotlin - When Expression
- Kotlin - if...Else Expression
- Kotlin - Control Flow
- Kotlin - Functions
- Kotlin - Ranges
- Kotlin - Arrays
- Kotlin - Strings
- Kotlin - Booleans
- Kotlin - Operators
- Kotlin - Data Types
- Kotlin - Variables
- Kotlin - Keywords
- Kotlin - Comments
- Kotlin - Basic Syntax
- Kotlin - Architecture
- Kotlin - Environment Setup
- Kotlin - Overview
- Kotlin - Home
Kotlin Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Kotpn - For Loop
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 simppfy the programming job. Actually, Loops are used in programming to repeat a specific block of code until certain condition is met.
Kotpn supports various types of loops and in this chapter we are going to learn Kotpn for loop.
Kotpn For Loop
Kotpn 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 Kotpn. Kotpn for loop is equivalent to the foreach loop in languages pke C#.
Kotpn does not provide a conventional for loop which is available in C, C++ and Java etc.
Syntax
The syntax of the Kotpn for loop is as follows:
for (item in collection) { // body of loop }
Iterate Through a Range
We will study Kotpn Ranges in a separate chapter, for now you should know that Kotpn 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 inspanidual item. To iterate over a range of numbers, we will use a range expression:
fun main(args: Array<String>) { for (item in 1..5) { println(item) } }
When you run the above Kotpn program, it will generate the following output:
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:
fun main(args: Array<String>) { for (item in 5 downTo 1 step 2) { println(item) } }
When you run the above Kotpn program, it will generate the following output:
5 3 1
Iterate Through a Array
Kotpn Array is another data type which provides iterator, so we can use for loop to iterate through a Kotpn 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:
fun main(args: Array<String>) { var fruits = arrayOf("Orange", "Apple", "Mango", "Banana") for (item in fruits) { println(item) } }
When you run the above Kotpn program, it will generate the following output:
Orange Apple Mango Banana
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>) { var fruits = arrayOf("Orange", "Apple", "Mango", "Banana") for (index in fruits.indices) { println(fruits[index]) } }
When you run the above Kotpn program, it will generate the following output:
Orange Apple Mango Banana
Quiz Time (Interview & Exams Preparation)
Q 1 - Which of the following is true about Kotpn for loop?
Answer : D
Explanation
All the mentioned statements are correct about Kotpn for loop.
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) } }
Answer : D
Explanation
When we execute above program it prints 6 4 2, so the last number printed by the loop will be 2
Advertisements