A loop is a programming structure that repeats a sequence of instruction until a specific condition is met. A range is essentially just an interval between two values, usually numbers: a start and an end. To replace the most common use cases of such loops, kotlin uses the concepts of ranges. Displaying a range produces a readable format.

1. Types of loops

The following are the two types of Loops.

Different loops based on control
Exit and Entry controlled loops

Entry controlled loop

It is also called as a pre-checking loop. In an entry controlled loop, a condition is checked before executing the body of a loop.

for loop

// The syntax for for loop is
for (item in collection) {
    // body of loop
}
ForLoopFlowChart

for Loop is a looping structure that is used to execute a sequence of code until the given condition returns false.

Example

// Looping numbers using for loop
fun main() {
    val array = arrayOf(1,2,3,4,5)
    for (index in array) {
        println("Number $index")
    }
}

/* Output */
Number 1
Number 2
Number 3
Number 4
Number 5
// Looping characters using for loop
fun main() {
    val string = "Loop"
    for (index in string) {
        println(index)
    }
}

/* Output */
L
o
o
p

while loop

// Syntax for while loop
while (condition) {
   // body of Loop
}
while loop in koltin

The body is executed while the condition is true. The task executes until the condition is false. This does not differ much comparing while loop in java.

Example

fun main() {
    val i = 1
    while (i <= 5) {
        println("Number $i")
        ++i
    }
}

/* Output */
Number 1
Number 2
Number 3
Number 4
Number 5

Exit controlled loop

It is also called as a post-checking loop. In an exit controlled loop, a condition is checked after executing the body of a loop.

do while loop

A do-while loop is similar to while loop except that it checks the condition at the end of iteration. A do-while loop will at least run once even if the given condition is false.

// Syntax for do while loop
do {
    // block of code to be executed
} while (condition)
A do while loop image

The body is executed for the first time unconditionally. After that, it’s executed while the condition is true. This does not differ much comparing while loop in java.

// looping numbers using do-while loop
fun main() {
    val i = 1
    do {
        println("Number $i")
        ++i
    } while (i <= 5)
}

/* Output */
Number 1
Number 2
Number 3
Number 4
Number 5

2. Types of operators in ranges

A range is essentially just an interval between two values, usually numbers: a start and end. A progression in ranges is just a sequences of numbers. We usually use first ,last and step.

dot

Using .. syntax includes both bounds in the resulting range. Such as the start bound and the bound.

fun main() {
    for (y in 1..3) {
        print(y)
    }
}

/* Output */
1
2
3

until

The until operator includes the start range value and excludes the end value.

fun main() {
    for (i in 0 until 3) {
        print(i)
    }
}

/* Output */
0
1
2

downTo

The downTo operator produces a decreasing range. As its produce the values in reverse order.

fun main() {
    for (i in 6 downTo 1) {
        print(i)
    }
}

/* Output */
6
5
4
3
2
1

step

The step operator changes the interval based upon the values.

fun main() {
    for (k in 1..10 step 4) {
        print(k)
    }
}

/* Output */
1
5
9

rangeTo

The rangeTo operator is same like the dot operator it also consist of start and end inclusions.

fun main() {
    for(u in 1.rangeTo(5)) {
        print(u)
    }
}

/* Output */
1
2
3
4
5

3. Types of ranges

You use the in operator to check whether a value is in a range. We have different types of ranges below.

IntRange

In IntRange we define the range value in integer type.

fun main() {
    val range: IntRange = 1..6
    for (item: Int in range) {
        print(item)
    }
}

/* Output */
1
2
3
4
5
6

CharRange

In CharRange we define the range value in char type.

fun main() {
    val range: CharRange = 'a'..'d'
    for (item: Char in range) {
        print(item)
    }
}

/* Output */
a
b
c
d

LongRange

In CharRange we define the range value in char type.

4. Progression in ranges

The progressions in ranges is a sequences of number and a difference between the numbers the properties of progression are first, last and step.

fun main() {
    val reverseRange: CharProgression = 'e' downTo 'a'
    for (range: Char in reverseRange) {
        print(range)
    }
}

/* Output */
e
d
c
b
a

5. Ranges using min, max, sum and average

By using kotlin ranges we can also find the minimum value ,maximum value and sum, average.

fun main() {
    val w = (1..10)
    println(w.minOrNull())
    println(w.maxOrNull())
    println(w.sum())
    println(w.average())
}

/* Output */
1
5
15
2.5

6. Project code and resources

That’s it! Thanks for reading the article, hope it was helpful !

Here We Go Again : (

if (article == helpful) {
    println("Like and subscribe to blog newsletter.")
} else {
    println("Let me know what i should blog on.")
}

This site uses Akismet to reduce spam. Learn how your comment data is processed.