Reader small image

You're reading from  Android Programming with Kotlin for Beginners

Product typeBook
Published inApr 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789615401
Edition1st Edition
Languages
Right arrow
Author (1)
John Horton
John Horton
author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton

Right arrow

Ranges


In order to continue our discussion on loops, it is necessary to briefly introduce the topic of ranges. Ranges are intimately connected to the Kotlin topic of arrays, which we will discuss more fully in Chapter 15, Handling Data and Generating Random Numbers. What follows is a quick introduction to ranges to enable us to then go on to cover for loops.

Take a look at the following line of code that uses a range:

val rangeOfNumbers = 1..4 

What is happening is that we are using type inference to create a list of values that contains the values 1, 2, 3, and 4.

We can also explicitly declare and initialize a list, as in the following code:

val rangeOfNumbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

The preceding code uses the listOf keyword to explicitly create a list containing the numbers 1 through to 10 inclusively.

How these work under the hood will be explored in more depth when we learn about arrays in Chapter 15, Handling Data and Generating Random Numbers. Then, we will see that there...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Android Programming with Kotlin for Beginners
Published in: Apr 2019Publisher: PacktISBN-13: 9781789615401

Author (1)

author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton