Swift – Operator Overloading

Swift – Operator Overloading Operator Overloading in Swift Operator overloading is a powerful technique in Swift programming. Operator overloading allows us to change the working of the existing operators like +, -, /, *, %, etc. with the customized code. It makes code more expressive and readable. To overload an operator, we have to define the behaviour of that operator using the “static func” keyword. Syntax Following is the syntax for operator overloading − static func operatorName() { // body } Example 1 Swift program to overload + operator to calculate the sum of two complex numbers. import Foundation struct ComplexNumber { var real: Double var imag: Double // Overloading + operator to add two complex numbers static func+(left: ComplexNumber, right: ComplexNumber) -> ComplexNumber { return ComplexNumber(real: left.real + right.real, imag: left.imag + right.imag) } } let complexNumber1 = ComplexNumber(real: 2.1, imag: 2.0) let complexNumber2 = ComplexNumber(real: 6.1, imag: 9.0) // Calling + operator to add two complex numbers let sumOfComplexNumbers = complexNumber1 + complexNumber2 print(sumOfComplexNumbers) Output ComplexNumber(real: 8.2, imag: 11.0) Example 2 Swift program to overload custom prefix operator. import Foundation struct Car { var price: Double // Overloading the custom prefix operator “++” to increase the price of car static prefix func ++ (carPrice: inout Car) { carPrice.price += 500000.0 } } var currentPrice = Car(price: 2500000.0) // Calling the custom ++ operator to increase the car price ++currentPrice print(“Updated car price is”, currentPrice.price) Output Updated car price is 2500000.0 Limitation of Operator Overloading in Swift The following are the limitations of operator overloading − In Swift, you can overload limited operators like arithmetic and customized operators. While overloading operators, you are not allowed to change the precedence or associativity of the operators. Swift does not support short-circuiting behaviour for overloading logical operators. Do not overuse operator overloading because it makes your code difficult to read and understand. Difference Between Operator Function and Normal Functions The following are the major differences between the operator functions and normal functions − Operator Function Normal Function They are define using “static func” keyword and a custom operator symbol. They are defined using “func” keyword and the function name. They are used to customize the behaviours of operators. They are used to complete general purpose tasks. They are called implicitly when using operator with custom types. They are called explicitly with the help of function name. They can be defined in index, prefix or postfix form. They can only defined in infix form. Print Page Previous Next Advertisements ”;

Swift – Range Operators

Swift – Range Operators ”; Previous Next Swift supports a special type of operator known as a range operator. The range operator is used to create a range of values. They are generally used with a for-in loop. There are three types of range operators are available in Swift − Operator Name Example (a…b) Closed Range 1…4 return 1, 2, 3, 4 (a..<b) Half-Open Range 1..<3 return 1, and 2 (a…) and (…a) One-Sided Range (1..) return 1, 2, 3, … end of the element (…2) return beginning to 2 Closed Range Operator in Swift A closed range operator is used to define a range that contains both starting and end values. In other words, a closed range is used to create a range from x to y, including the value of x and y. Here the starting value or value of x must not be greater than the ending value or value of y. Syntax Following is the syntax of the closed range operator − x…y Example Swift program to display a sequence of numbers using a closed range operator. import Foundation // For loop to display a sequence of numbers for x in 3…11{ print(x) } Output 3 4 5 6 7 8 9 10 11 Half-Open Range Operator in Swift A half-open range operator is used to create a range from x to y. This range only includes the starting value and does not include the ending value or y. Also, the value of x or the starting value should not be greater than the ending value of y. Or if the value of x is equal to y, then the range will be empty. This operator is generally used with zero-based lists like arrays, etc. Syntax Following is the syntax of the half-open range operator − x..<y Example Swift program to display the elements of an array using a half-open range operator. import Foundation let arr = [1, 4, 6, 2, 7] for x in 0..<arr.count{ print(arr[x]) } Output 1 4 6 2 7 One-Sided Ranges in Swift The one-sided ranges are used to define ranges that have a value of only one side. Such types of ranges are used when we need to know from where the iteration should begin. One-sided ranges are of two types − Partial Range from Start − It defines a range that starts from the beginning of the given collection or sequence and goes to the specified end value. This range contains only the end value and does not contain the starting value. Syntax Following is the syntax of partial range from start − …x or ..<x Partial Range to the End − It creates a range that starts from the specified value and goes all the way to the end of the given collection or sequence. This range contains only the starting value and does not contain the end value. Syntax Following is the syntax of partial range to the end − x… Example Swift program to demonstrate partial range to the start. import Foundation let arr = [1, 4, 6, 2, 7] // This one-sided range displays elements till index 3 print(“Sequence 1:”) for x in arr[…3]{ print(x) } // This one-sided range displays all the elements before index 3 print(“Sequence 2:”) for y in arr[..<3]{ print(y) } Output Sequence 1: 1 4 6 2 Sequence 2: 1 4 6 Example Swift program to demonstrate partial range to the end. import Foundation let arr = [1, 4, 6, 2, 7] // This one-sided range displays all elements // starting from index 2 to the end of the sequence print(“Sequence:”) for x in arr[2…]{ print(x) } Output Sequence: 6 2 7 Print Page Previous Next Advertisements ”;

Swift – Type Aliases

Swift – Type Aliases ”; Previous Next Swift provides a special feature named Type Aliases. Type aliases are used to define another name or aliases for the pre-defined types. It only gives a new name to the existing type and does not create a new type. For example, “Int” can also be defined as “myInt”. After defining type alias, we can use that alias anywhere in the program. A single program can contain more than one alias. A type alias is commonly used to improve code readability, code quality and abstracting complex types. We can define type aliases with the help of the type alias keyword. Syntax Following is the syntax of the type alias – typealiase newName = existingType We can define type aliases for the following data types − Primitive Data Types User Defined Data Types Complex Data Types Type Aliases for Primitive Data Types in Swift Primitive data types are the pre-defined data types provided by Swift such as Int, Float, Double, Character and String. With the help of a type alias, we can provide a new name to the in-built data types which can be used throughout the program without any error. For example, “typealias myString = String” now using myString we can create a string type variable. Syntax Following is the syntax of the type alias for primitive data types − typealias newName = PreDefinedDataType Here the PreDefinedDataType can be Int, Float, Double, String and Character. Example Swift program to create a type alias for in-built data types. import Foundation // Creating type typealias for String typealias myString = String // Creating type typealias for Float typealias myNum = Float // Creating type typealias for Int typealias Num = Int // Declaring integer type variable using Num var number : Num = 10 // Declaring string type variable using myString var newString : myString = “Tutorialspoint” // Declaring float type variable using myNum var value : myNum = 23.456 print(“Number:”, number) print(“type:”, type(of: number)) print(“nString:”, newString) print(“type:”, type(of: newString)) print(“nFloat:”, value) print(“type:”, type(of: value)) Output Number: 10 type: Int String: Tutorialspoint type: String Float: 23.456 type: Float Type Aliases for User- Defined Data Types in Swift Using type aliases, we can also provide an alternate name to the user-defined data types. For example, “typealias mySet = Set<String>” now using mySet we can create a set of string type. Syntax Following is the syntax of the type alias for user-defined data types − typealias newName = dataType Here the datatype can be Array, Set, dictionary, etc. Example Swift program to create a type alias for user-defined data types. import Foundation // Creating type typealias for Set typealias mySet = Set<Int> // Creating type typealias for Array typealias myNum = Array<Int> // Creating type typealias for Array typealias Num = Array<String> // Declaring set of integer type using mySet var newSet : mySet = [32, 3, 1, 2] // Declaring array of integer type using myNum var newArray : myNum = [32, 2, 1, 1, 3] // Declaring array of string type using Num var newArr : Num = [“Swift”, “C++”, “C#”] print(“Set:”, newSet) print(“Array:”, newArray) print(“Array:”, newArr) Output Set: [32, 3, 1, 2] Array: [32, 2, 1, 1, 3] Array: [“Swift”, “C++”, “C#”] Type Aliases for Complex Data Types in Swift Complex data are the special type of data types which contain more than one pre-defined data type. So using types aliases we can also create aliases of complex data types. For example, (String, String) -> string as “MyString”. Syntax Following is the syntax of type aliases for complex data types − typealias newName = CdataType Here the CdataType can be any complex data type like (String)->(String). Example Swift program to create a type alias for complex data types. import Foundation // Creating type typealias for function type typealias Value = (String, String) -> String func addStr(s1: String, s2: String) -> String{ return s1 + s2 } // Assigning addStr function to Value var newFunc : Value = addStr // Calling function var result = newFunc(“Hello”, “World”) print(result) Output HelloWorld Print Page Previous Next Advertisements ”;

Swift – Characters

Swift – Characters ”; Previous Next A character in Swift is a single character String literal such as “A”, “!”, “c”, addressed by the data type Character. Or we can say that the character data type is designed to represent a single Unicode character. Syntax Following is the syntax to declare a character data type − var char : Character = “A” Example Swift program to create two variables to store characters. import Foundation let char1: Character = “A” let char2: Character = “B” print(“Value of char1 (char1)”) print(“Value of char2 (char2)”) Output Value of char1 A Value of char2 B Example If we try to store more than one character in a Character type variable or constant, then Swift will not allow that and give an error before compilation. import Foundation // Following is illegal in Swift let char: Character = “AB” print(“Value of char (char)”) Output main.swift:4:23: error: cannot convert value of type ”String” to specified type ”Character” let char: Character = “AB” Example Also, we are not allowed to create an empty Character variable or constant which will have an empty value. If we try to do we will get an error. import Foundation // Creating empty Character let char1: Character = “” var char2: Character = “” print(“Value of char1 (char1)”) print(“Value of char2 (char2)”) Output main.swift:4:24: error: cannot convert value of type ”String” to specified type ”Character” let char1: Character = “” ^~ main.swift:5:24: error: cannot convert value of type ”String” to specified type ”Character” var char2: Character = “” ^~ Accessing Characters from Strings in Swift A string represents a collection of Character values in a specified order. So we can access individual characters from the given String by iterating over that string with a for-in loop − Example import Foundation // Accessing Characters from Strings using for-in loop for ch in “Hello” { print(ch) } Output H e l l o Concatenating String with Character in Swift In Swift, we can concatenate String with a character using the + and += operators. Both the operators concatenate the given character at the end of the specified string. While concatenating a string with a character we required an explicit conversion from character to string because Swift enforces strong typing. Also, we are not allowed to concatenate a character(variable) with a string. Example Swift program to concatenate a string with a character using the + operator. import Foundation let str = “Swift” let char: Character = “#” // Concatenating string with character using + operator let concatenatedStr = str + String(char) print(concatenatedStr) Output Swift# Example Swift program to concatenate a string with a character using the += operator. import Foundation var str = “Swift” let char: Character = “!” // Concatenating string with character using += operator str += String(char) print(str) Output Swift! Print Page Previous Next Advertisements ”;

Swift – Data Types

Swift – Data Types ”; Previous Next While developing programs in any programming language, the utilization of variables is necessary for storing data. Variables are nothing but reserved memory locations to store data. This means that when we create a variable, we reserve some amount of space in memory. Subsequently, when desiring to store data in the variable we can enter data of any type such as string, character, wide character, integer, floating point, Boolean, etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Built-in Data Types in Swift Swift offers the programmer a rich assortment of built-in data types, also known as primitive data types. They represent the basic values and are directly supported by the Swift language. Swift provides seven types of built-in data types and they are − Datatype Name Description Int or Uint This is used for whole numbers. More specifically, you can use Int32, Int64 to define 32 or 64-bit signed integers, whereas UInt32 or UInt64 to define 32 or 64-bit unsigned integer variables. For example, 42 and -23. Float This is used to represent a 32-bit floating-point number and numbers with smaller decimal points. For example, 3.14159, 0.1, and -273.158. Double This is used to represent a 64-bit floating-point number and is used when floating-point values must be very large. For example, 3.14159, 0.1, and -273.158. Bool This represents a Boolean value which is either true or false. String This is an ordered collection of characters. For example, “Hello, World!” Character This is a single-character string literal. For example, “C” Optional This represents a variable that can hold either a value or no value. Bound Values The following table shows the data type, how much memory it takes to store the value in memory, and what is the maximum and minimum values that can be stored in such types of variables. Type Typical Bit Width Typical Range Int8 1byte -127 to 127 UInt8 1byte 0 to 255 Int32 4bytes -2147483648 to 2147483647 UInt32 4bytes 0 to 4294967295 Int64 8bytes -9223372036854775808 to 9223372036854775807 UInt64 8bytes 0 to 18446744073709551615 Float 4bytes 1.2E-38 to 3.4E+38 (~6 digits) Double 8bytes 2.3E-308 to 1.7E+308 (~15 digits) User-Defined Data Types in Swift User-defined data types allow us to create customized data types according to their requirements. They provide more flexibility and abstraction. Following are some user-defined data types supported by Swift − Type Name Description Structures(struct) They are value types; means they can have copied when passed around in the program. They are good for representing simple data structure. Class They are reference types; means they are passed as a reference. They are good for complex data models and objects. Enumerations(Enum) They are used to define a group of related values. They are good at representing finite set. Protocols They define a blueprint for methods and properties that is good for a particular task or piece of functionality. Defining Data Types Defining a data type is a process in which we specify what type of data will be stored in the variable. As we know, Swift supports two types of data – built-in and user-defined data types. So we will see how to define built-in data types − Syntax Following is the syntax of the built-in data type − var name : dataType = Value Example Defining built-in data types − var index : Int = 10 var str : String = “Learn Swift!” var char : Character = “S” var num : Float = 23.45 var nums : Double = 32.233434 var value : Bool = true Now we will see how to define user-defined data types − Syntax Following is the syntax of the user-defined data type − struct Student { var name: String var age: Int } var myData = Student(name: “Mona”, age: 23) Example Defining built-in user-defined data types − // Structure struct Employee { var name: String var age: Int } // Structure data type var myData = Employee(name: “Seema”, age: 23) // Class class Student { var name: String var age: Int init(name: String, age: Int) { self.name = name self.age = age } } // Class data type var myInfo = Student(name: “Alice”, age: 25) // Enumeration enum Rectangle { case length, width, breadth } // Enum data type var side: Rectangle = .length Type Safety in Swift Swift is a type-safe language. It means that if a variable of your program expects a String, you can”t pass int in it by mistake because Swift performs type-checks while compiling your code and displays an error message if it finds any type mismatch. However, this does not imply that you have to specify the type of every variable or constant. Swift applies type inference which automatically determines the type of the variable or constant if it is not specified explicitly. Example Swift program to demonstrate type Safety. var varA = 42 // Here compiler will show an error message because varA // variable can only store integer type value varA

Swift – Environment

Swift – Environment ”; Previous Next Swift is a new generation language developed by Apple to create applications and software for the Apple ecosystem. Due to its speed, safety and modern syntax, it quickly emerged as a preferred language among the developers. Originally Siwft was designed to create applications for iOS and macOS. However, with the increase in its popularity, developers expanded its versatility. Now Swift is available not only for macOS but for Windows and Linux also and provides cross-platform solutions to the developers. Now let us see how we can install Swift on macOS and Windows step by step. Installing Swift on macOS To install Swift on macOS we required Xcode. Xcode is Apple’s integrated development environment(IDE) for all Apple’s platforms. It provides various tools to create apps and support the source code for various programming languages like Swift, Objective-C, Java, Python, C++, C, etc. So follow the following steps to download Xcode in macOS − Step 1 − Go to the App Store or you can go to the following link to download the latest version of Xcode:www.swift.org/install/. Step 2 − It will automatically install Xcode on your macOS Step 3 − Now open Xcode and go to file >> New >> Playground. Step 4 − Now select a blank option and then give the name and then click on the Create button. Step 5 − Now you are ready to write and run your first Swift program. Installing Swift on Windows To install Swift on Windows, follow the following steps − Step 1 − Go to the following link to download the latest version of Swift for your Windows www.swift.org/download/. Step 2 − Click on Windows x86_64 and it starts downloading. Step 3 − Go to the download folder and open the downloaded .exe file. Step 4 − Click on the install button and the installation process will start. Step 5 − After completing the installation go to the environment variable and check for SDKRoot. Step 6 − Now go to the command prompt and type the following command − Swift —version Print Page Previous Next Advertisements ”;

Swift – Literals

Swift – Literals ”; Previous Next What are Literals? A literal is the source code representation of a value of an integer, floating-point number, or string type. Or we can say that literals are used to represent the actual values that are used to assign a constant or variable. For example, 34 is an integer literal, 23.45 is a floating point literal. They are directly used in the program. We cannot directly perform operations on the literals because they are fixed values, but we can perform operations on the variables or constants (initialized with literals). Type of Swift Literals Swift supports the following types of literals − Integer literals Floating-point literals String literals Boolean literals Swift Integer Literals An integer literal is used to represent an integer value or whole number. We can specify a negative or positive value in the integer literal, for example, -10 or 10. An integer literal can contain leading zeros but they do not have any effect on the value of the literal, for example, 010 and 10 both are the same. Integer literals are of the following types − Decimal Integer Literal − It is the most commonly used form of the integer literal. For example, 34. Binary Integer Literal − It is used to represent base 2 or binary values. It is prefixed with “0b”. For example, 0b1011. Octal Integer Literal − It is used to represent base 8 or octal values. It is prefixed with “0o”. For example, 0o53. Hexadecimal Integer Literal − It is used to represent base 16 or hexadecimal values. It is prefixed with “0x”. For example, 0xFF. Example Swift program to demonstrate integer literals − import Foundation // Decimal integer literal let num1 = 34 print(“Decimal Integer: (num1)”) // Binary integer literal let num2 = 0b101011 print(“Binary Integer: (num2)”) // Octal integer literal let num3 = 0o52 print(“Octal Integer: (num3)”) // Hexadecimal integer literal let num4 = 0x2C print(“Hexadecimal Integer: (num4)”) Output Decimal Integer: 34 Binary Integer: 43 Octal Integer: 42 Hexadecimal Integer: 44 Swift Floating-point Literals A floating-point literal is used to represent a number with the fractional component, for example, 34.567. We can specify a negative or positive value in the floating-point literal, for example, -23.45 or 2.34. A floating point literal can contain an underscore(_), but it does not have any effect on the overall value of the literal, for example, 0.2_3 and 23 both are the same. Floating point literals are of the following types − Decimal Floating-point Literals − It consists of a sequence of decimal digits followed by either a decimal fraction, a decimal exponent, or both. For example, 12.1875. Hexadecimal Floating-point Literals − It consists of a 0x prefix, followed by an optional hexadecimal fraction, followed by a hexadecimal exponent. For example, 0xC.3p0. Exponential Literal − It is used to represent the power of 10. It contains the letter “e”. For example, 1.243e4. Example Swift program to demonstrate floating point literals. import Foundation // Decimal floating-point literal let num1 = 32.14 print(“Decimal Float: (num1)”) // Exponential notation floating-point literal let num2 = 2.5e3 print(“Exponential Float: (num2)”) // Hexadecimal floating-point literal let num3 = 0x1p-2 print(“Hexadecimal Float: (num3)”) Output Decimal Float: 32.14 Exponential Float: 2500.0 Hexadecimal Float: 0.25 Swift String Literals A string literal is a sequence of characters surrounded by double quotes, for example, “characters”. String literals can be represented in the following ways − Double Quoted String − It is used to represent single-line literals. For example, “hello”. Multiline String − It is used to represent multiline literals. It can contain multiple lines without any escape character. For example − “””Hello I like your car “”” String literals cannot contain an un escaped double quote (“), an un escaped backslash (), a carriage return, or a line feed. Special characters can be included in string literals using the following escape sequences &miinus; Escape Sequence Name Null Character \ character b Backspace f Form feed n Newline r Carriage return t Horizontal tab v Vertical tab ” Single Quote “ Double Quote 00 Octal number of one to three digits xhh… Hexadecimal number of one or more digits Example Swift program to demonstrate string literals. import Foundation // Double-quoted string literal let str1 = “Swift is good programming language” print(str1) // Multi-line string literal let str2 = “”” Hello priya is at Swift programming “”” print(str2) // Special characters in a string literal let str3 = “Escape characters: nt””\” print(str3) Output Hello priya is at Swift programming Escape characters: “” Swift Boolean Literals TBoolean literals are used to represent Boolean values true and false. They are generally used to represent conditions. Boolean literals are of two types − true − It represents a true condition. false − It represents a true condition. Example Swift program to demonstrate Boolean literals. import Foundation // Boolean literals in Swift let value1 = true let value2 = false print(“Boolean Literal: (value1)”) print(“Boolean Literal: (value2)”) Output Boolean Literal: true Boolean Literal: false Print Page Previous Next Advertisements ”;

Swift – Boolean

Swift – Boolean ”; Previous Next Just like other programming languages Swift also supports a boolean data type known as a bool. Boolean values are logical because they can either be yes or no. Boolean data types have only two possible values: true and false. They are generally used to express binary decisions and play a major role in control flow and decision-making. Syntax Following is the syntax of the boolean variable − let value1 : Bool = true let value2 : Bool = false Following is the shorthand syntax of the boolean type − let value1 = true let value2 = false Example Swift program to use boolean with logical statement. import Foundation // Defining boolean data type let color : Bool = true // If the color is true, then if block will execute if color{ print(“My car color is red”) } // Otherwise, else block will execute else{ print(“My car color is not red”) } Output My car color is red Combine Boolean with Logical Operators in Swift In Swift, we are allowed to combine boolean with logical operators like logical AND “&&”, logical OR “||” and logical NOT “!” to create more complex expressions. Using these operators, we can able to perform various conditional operations on boolean values. Example Swift program to combine boolean with logical operator. import Foundation // Defining boolean data type let isUsername = true let isPassword = true let hasAdminAccess = false let isUserAccount = true // Combining boolean data type with logical AND and OR operators let finalAccess = isUsername && isPassword && (hasAdminAccess || isUserAccount) /* If the whole expression returns true then only the user gets access to the admin panel. */ if finalAccess { print(“Welcome to the admin panel”) } else { print(“You are not allowed to access admin panel”) } Output Welcome to the admin panel Print Page Previous Next Advertisements ”;

Swift – Double

Swift – Double ”; Previous Next Double is a standard data type in Swift. Double data type is used to store decimal numbers like 23.344, 45.223221, 0.324343454, etc. It is a 64-bit floating-point number and stores values up to 15 decimal digits which makes it more accurate as compared to Float. If you create a variable to store a decimal number without specifying its type, then by default compiler will assume it is a Double type instead of a Float type, due to high precision. Syntax Following is the syntax of the Double data type − let num : Double = 23.4554 Following is the shorthand syntax of the Double data type − let num = 2.73937 Example Swift program to calculate the sum of two double numbers. import Foundation // Defining double numbers let num1 : Double = 2.3764 let num2 : Double = 12.738 // Store the sum of two double numbers var sum : Double = 0.0 sum = num1 + num2 print(“Sum of (num1) and (num2) = (sum)”) Output Sum of 2.3764 and 12.738 = 15.1144 Example Swift program to calculate the product of two double numbers. import Foundation // Defining double numbers let num1 = 12.3764832 let num2 = 22.7388787779074 // Store the product of two double numbers var product = 0.0 product = num1 * num2 print(“Product of (num1) and (num2) = (product)”) Output Product of 12.3764832 and 22.7388787779074 = 281.42735118160743 Difference Between Float and Double The following are the major differences between the floating point data type and the double data type. Double Float It has precision of at least 15 decimal digits. It has precision of at least 6 decimal digits. Memory size is of 8 bytes. Memory size is of 4 bytes. If no data type is defined, then compiler will treat it as Double. It is not preferred by the compiler by default. Print Page Previous Next Advertisements ”;

Swift – Floating-Point Numbers

Swift – Floating-Point Numbers ”; Previous Next Floating Point Numbers are used to store numbers with their fractional components like 3.2232, 92.2, 21.2, etc. Or we can say that the Float data type is used to store 32-bit floating point numbers. It has a precision of at least 6 decimal digits. We are allowed to perform various arithmetic operations on Floating point numbers in Swift such as addition, subtraction, multiplication, etc. We can also compare two floating point numbers using equal to or not equal to operators. Syntax Following is the syntax of the Float data type − let num : Float = 2.3421 Following is the shorthand syntax of the Float data type − let num = 3.21 Example Swift program to calculate the product of two floating point numbers. import Foundation // Defining Float data type var num1 : Float = 2.342 var num2 : Float = 23.44 // Store the result var result : Float // Calculating the product of the Floating point numbers result = num1 * num2 print(“Product of (num1) * (num2) is (result)”) Output Product of 2.342 * 23.44 is 54.89648 Example Swift program to calculate the sum of all the elements present in an array of float types. import Foundation // Defining Float type array var Array: [Float] = [2.3, 3.4, 6.5, 66.4, 3.2] // Store the sum var Sum : Float = 0.0 // Calculating the sum of all the elements present in the array for num in Array { Sum += num } print(“Sum = (Sum)”) Output Sum = 81.799995 Print Page Previous Next Advertisements ”;