// ForeachDemo.kt Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2020-10-30 File created. // This program demonstrates the use of for-in loop, which is like // a foreach loop in some other programming languages. fun main() { val array_of_integers = intArrayOf( 33, 444, 55, 6, 777 ) val array_of_strings = arrayOf( "January", "February", "March" ) print( "\n\n array_of_integers printed using an index variable:\n" ) for ( integer_index in 0 .. array_of_integers.size - 1 ) { print( " " + array_of_integers[ integer_index ] ) } print( "\n\n array_of_integers printed with a proper for-in loop:\n" ) for ( integer_to_print in array_of_integers ) { print( " " + integer_to_print ) } print( "\n\n array_of_strings printed with an index variable:\n" ) for ( string_index in 0 .. array_of_strings.size - 1 ) { print( " " + array_of_strings[ string_index ] ) } print( "\n\n array_of_strings printed with a proper for-in loop:\n" ) for ( string_to_print in array_of_strings ) { print( " " + string_to_print ) } print( "\n\n array_of_strings printed with a forEach() call :\n" ) array_of_strings.forEach( { print( " " + it ) // 'it' is a special identifier in Kotlin. } ) print( "\n\n\n" ) /* // The following would work as well: array_of_strings.forEach{ print( " " + it ) } // But setting the left brace as follows is not acceptable: array_of_strings.forEach { print( " " + it ) } */ }