Book Image

Programming Kotlin

Book Image

Programming Kotlin

Overview of this book

Quickly learn the fundamentals of the Kotlin language and see it in action on the web. Easy to follow and covering the full set of programming features, this book will get you fluent in Kotlin for Android.
Table of Contents (20 chapters)
Programming Kotlin
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface

Ranges


A range is defined as an interval that has a start value and an end value. Any types which are comparable can be used to create a range, which is done using the .. operator:

    val aToZ = "a".."z" 
    val oneToNine = 1..9 

Once a range is created, the in operator can be used to test whether a given value is included in the range. This is why the types must be comparable. For a value to be included in a range, it must be greater than or equal to the start value and less than or equal to the end value:

    val aToZ = "a".."z" 
    val isTrue = "c" in aToZ
    val oneToNine = 1..9 
    val isFalse = 11 in oneToNine 

Integer ranges (ints, longs, and chars) also have the ability to be used in a for loop. See the section on For loops for further details.

There are further library functions to create ranges not covered by the .. operator; for example, downTo() will create a range counting down and rangeTo()will create a range up to a value. Both of these functions...