// ForeachDemo.swift Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2014-09-29 File created. // 2016-08-11 Last modification. // This program demonstrates the use of foreach loops that can be // used to go through all items in a collection (e.g. an array). // The foreach loop in Swift is called for-in loop. // Swift no longer supports a C-style for loop. import Foundation // The two arrays defined below are immutable arrays as they are // specified with keyword let. let array_of_integers = [ 33, 444, 55, 6, 777 ] let array_of_strings = [ "January", "February", "March" ] print( "\n\n array_of_integers printed using an index variable:\n" ) for integer_index in 0 ... array_of_integers.count - 1 { print( " " + String( array_of_integers[ integer_index ] ), terminator: "" ) } print( "\n\n array_of_integers printed with a proper for-in loop:\n" ) for integer_to_print in array_of_integers { print( " " + String( integer_to_print ), terminator: "" ) } print( "\n\n array_of_strings printed with an index variable:\n" ) for string_index in 0 ... array_of_strings.count - 1 { print( " " + array_of_strings[ string_index ], terminator: "" ) } 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, terminator: "" ) } print( "\n\n" )