// Meanvalue.swift Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2014-09-28 File created. // 2015-10-17 Last modification. // This program demonstrates the repeat-while loop, which is similar // to the do-while loop in other programming languages. import Foundation // The type of integer_from_keyboard is Int! because the conversion // from String may fail. var integer_from_keyboard : Int! = 0 var number_of_integers_given = -1 var mean_value = 0.0 // The type of mean_value becomes // Double as we assign a Double value var sum_of_integers = 0 print( "\n This program calculates the mean value of" + "\n the integers you enter from the keyboard." + "\n Please, start entering numbers. The program" + "\n stops when you enter a zero. \n" ) repeat { print( " Enter an integer: ", terminator: "" ) integer_from_keyboard = Int( readLine()! ) number_of_integers_given += 1 sum_of_integers = sum_of_integers + integer_from_keyboard } while integer_from_keyboard != 0 if number_of_integers_given > 0 { mean_value = Double( sum_of_integers ) / Double( number_of_integers_given ) } print( String( format: "\n The mean value is: %f \n\n", mean_value ) )