Kotlin program of print the first, last, and step value of a range

Learn how to print the first, last, and step value of a range in Kotlin?
Submitted by IncludeHelp, on March 26, 2022

In the below program, we will create an integer range using the (..) operator, and will print the first element of the list using ".first", last element of the list using ".last", and the value of the step using ".step".

fun main(args: Array<String>) {	
	// print first element of the range
	println((1..10 step 2).first)

	// print first element of the range
	println((1..10 step 2).last)

	// print value of the step
	println((1..10 step 2).step)
}

Output:

1
9
2

Kotlin Ranges Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.