Constants in Swift • Constants refer to fixed values that a program may not alter during its execution. • Example: let tax = 4.5
separator and terminator • To separate values or variables, separator is used. • To add something at the end, terminator is used. • If you want to print multiple variable in 1 statement, how to do? var a=4 var b=4 var c=4 print(“(a)n(b)n(c))
separator and terminator • By using separator example: var a = 4 var b= 2 var c = 6 print(a,b,c, separator: ”n”) Result 4 2 6
separator and terminator • By using terminator example: var a = 4 var b = 2 var c = 6 print(a,b,c, terminator: ”nThis is end”) Result 4 2 6 This is end
separator and terminator • By using separator and terminator together example: var a = 4 var b= 2 var c = 6 print(a,b,c, separator: ”n”, terminator: ”This is end”) Result 4 2 6 This is end
Array • Arrays are used to store similar kind of values. • Array stores the values of same type. • Syntax: var identifier: [datatype] OR var identifier: [datatype] = [value1,value2,…] } Declaration Declaration and Initialization
Array Example 1 : var arr=[4,2,6,9,15,3] for n in arr { print(n) } Result 4 2 6 9 15 3
Array Example 2 : var arr=["Red","Green","Yellow","Black"] for n in arr { print(n) } Result Red Green Yellow Black
Empty Array • Creating an array without any value is called Empty Array. • Example: var emptyArray: [Int] = [ ] print(emptyArray) Result [ ]
count property of Array • To get the number of elements in an array. • Example: var noOfElements = [1, 2, 3, 10, 100] print(noOfElements.count) Result 5
isEmpty property of Array • To check the number of elements in an array are zero or not(i.e. count is zero or not). • Example: if cart.isEmpty { print("The cart is empty.") } else { print("The cart is not empty.") }
Adding values to Array • You can add elements to the end of an array using the append method. • Example: var arr: [Int] = [1,2,3,4,5] for i in 6...9 { arr.append(i) } print(arr) Result [1, 2, 3, 4, 5, 6, 7, 8, 9]
Adding values to specific index in Array • To insert an item into the array at a specified index, call the array’s insert(at:) method. • Example: var arr: [Int] = [1,3,7,10] insert(5, at: 2) insert(9, at: 4) print(arr) Result [1, 3, 5, 7, 9, 10]
Removing values from Array • To remove an item from a specific index call the remove(at:) method. • Example: var arr: [Int] = [1,2,3,4,5] arr.remove(at: 2) print(arr) Result [1, 2, 4, 5]
Mutable Vs Immutable • To declare a mutable variable or array var keyward is used. • To declare a immutable variable or array let keyward is used. • Example: var arr: [Int] = [1,2,3,4,5] // mutable array let arr: [Int] = [1,2,3,4,5] // immutable array
Any keyword • To declare an array of any type of instance/value Any keyword is used. • Example: var a: [Any] = [ ] a.append(9) a.append("to") a.append(11) • Example: var a = [1,2,3,4,"apple","car"] as Any print(a)
MCQs.. 1. What will be the value of len? var array1 = [1, 2, 3, 4, 5] var array2 = array1 array2.append(6) var len = array1.count a) 4 b) 5 c) 6 d) None of these
MCQs.. 1. What will be the value of len? var array1 = [1, 2, 3, 4, 5] var array2 = array1 array2.append(6) var len = array1.count a) 4 b) 5 c) 6 d) None of these
MCQs.. 2. What will be the value of result? let op1: Int = 10 let op2: UInt = 20 let op3: Double = 30.55 var result = op1 + Double(op2) + op3 a) 60 b) 60.55 c) Error d) None of these
MCQs.. 2. What will be the value of result? let op1: Int = 10 let op2: UInt = 20 let op3: Double = 30.55 var result = op1 + Double(op2) + op3 a) 60 b) 60.55 c) Error d) None of these
MCQs.. 3. What will be the value of result? let op1: Double = 10.20 var result:Double = 20.50 + op1 a) 30 b) 30.7 c) Error d) None of these
MCQs.. 3. What will be the value of result? let op1: Double = 10.20 var result:Double = 20.50 + op1 a) 30 b) 30.7 c) Error d) None of these
Questions 1. Write a program to print a maximum number from an array. 2. Write a program to print the odd numbers from an array. 3. Write a program to sum the positive elements of array as well as negative elements of array.
Questions 1. Write a program to print a maximum number from an array. var listOfNumbers = [1, 2, 3, 10, 100] var maxVal = listOfNumbers[0] for number in listOfNumbers { if maxVal < number { maxVal = number } } print(maxVal)
Questions 2. Write a program to print the odd numbers from an array. var listOfNumbers = [1, 2, 3, 10, 100] for number in listOfNumbers { if number % 2 != 0 { print(number) } }
Questions 3. Write a program to sum the positive elements of array as well as negative elements of array. var listOfNumbers = [1, -2, 3, -6 , 20] var sumPos = 0 ,sumNeg = 0 for number in listOfNumbers { if number>0 { sumPos += number } else { sumNeg += number } } print(sumPos) print(sumNeg)

Arrays and its properties IN SWIFT

  • 1.
    Constants in Swift •Constants refer to fixed values that a program may not alter during its execution. • Example: let tax = 4.5
  • 2.
    separator and terminator •To separate values or variables, separator is used. • To add something at the end, terminator is used. • If you want to print multiple variable in 1 statement, how to do? var a=4 var b=4 var c=4 print(“(a)n(b)n(c))
  • 3.
    separator and terminator •By using separator example: var a = 4 var b= 2 var c = 6 print(a,b,c, separator: ”n”) Result 4 2 6
  • 4.
    separator and terminator •By using terminator example: var a = 4 var b = 2 var c = 6 print(a,b,c, terminator: ”nThis is end”) Result 4 2 6 This is end
  • 5.
    separator and terminator •By using separator and terminator together example: var a = 4 var b= 2 var c = 6 print(a,b,c, separator: ”n”, terminator: ”This is end”) Result 4 2 6 This is end
  • 6.
    Array • Arrays areused to store similar kind of values. • Array stores the values of same type. • Syntax: var identifier: [datatype] OR var identifier: [datatype] = [value1,value2,…] } Declaration Declaration and Initialization
  • 7.
    Array Example 1 : vararr=[4,2,6,9,15,3] for n in arr { print(n) } Result 4 2 6 9 15 3
  • 8.
    Array Example 2 : vararr=["Red","Green","Yellow","Black"] for n in arr { print(n) } Result Red Green Yellow Black
  • 9.
    Empty Array • Creatingan array without any value is called Empty Array. • Example: var emptyArray: [Int] = [ ] print(emptyArray) Result [ ]
  • 10.
    count property ofArray • To get the number of elements in an array. • Example: var noOfElements = [1, 2, 3, 10, 100] print(noOfElements.count) Result 5
  • 11.
    isEmpty property ofArray • To check the number of elements in an array are zero or not(i.e. count is zero or not). • Example: if cart.isEmpty { print("The cart is empty.") } else { print("The cart is not empty.") }
  • 12.
    Adding values toArray • You can add elements to the end of an array using the append method. • Example: var arr: [Int] = [1,2,3,4,5] for i in 6...9 { arr.append(i) } print(arr) Result [1, 2, 3, 4, 5, 6, 7, 8, 9]
  • 13.
    Adding values tospecific index in Array • To insert an item into the array at a specified index, call the array’s insert(at:) method. • Example: var arr: [Int] = [1,3,7,10] insert(5, at: 2) insert(9, at: 4) print(arr) Result [1, 3, 5, 7, 9, 10]
  • 14.
    Removing values fromArray • To remove an item from a specific index call the remove(at:) method. • Example: var arr: [Int] = [1,2,3,4,5] arr.remove(at: 2) print(arr) Result [1, 2, 4, 5]
  • 15.
    Mutable Vs Immutable •To declare a mutable variable or array var keyward is used. • To declare a immutable variable or array let keyward is used. • Example: var arr: [Int] = [1,2,3,4,5] // mutable array let arr: [Int] = [1,2,3,4,5] // immutable array
  • 16.
    Any keyword • Todeclare an array of any type of instance/value Any keyword is used. • Example: var a: [Any] = [ ] a.append(9) a.append("to") a.append(11) • Example: var a = [1,2,3,4,"apple","car"] as Any print(a)
  • 17.
    MCQs.. 1. What willbe the value of len? var array1 = [1, 2, 3, 4, 5] var array2 = array1 array2.append(6) var len = array1.count a) 4 b) 5 c) 6 d) None of these
  • 18.
    MCQs.. 1. What willbe the value of len? var array1 = [1, 2, 3, 4, 5] var array2 = array1 array2.append(6) var len = array1.count a) 4 b) 5 c) 6 d) None of these
  • 19.
    MCQs.. 2. What willbe the value of result? let op1: Int = 10 let op2: UInt = 20 let op3: Double = 30.55 var result = op1 + Double(op2) + op3 a) 60 b) 60.55 c) Error d) None of these
  • 20.
    MCQs.. 2. What willbe the value of result? let op1: Int = 10 let op2: UInt = 20 let op3: Double = 30.55 var result = op1 + Double(op2) + op3 a) 60 b) 60.55 c) Error d) None of these
  • 21.
    MCQs.. 3. What willbe the value of result? let op1: Double = 10.20 var result:Double = 20.50 + op1 a) 30 b) 30.7 c) Error d) None of these
  • 22.
    MCQs.. 3. What willbe the value of result? let op1: Double = 10.20 var result:Double = 20.50 + op1 a) 30 b) 30.7 c) Error d) None of these
  • 23.
    Questions 1. Write aprogram to print a maximum number from an array. 2. Write a program to print the odd numbers from an array. 3. Write a program to sum the positive elements of array as well as negative elements of array.
  • 24.
    Questions 1. Write aprogram to print a maximum number from an array. var listOfNumbers = [1, 2, 3, 10, 100] var maxVal = listOfNumbers[0] for number in listOfNumbers { if maxVal < number { maxVal = number } } print(maxVal)
  • 25.
    Questions 2. Write aprogram to print the odd numbers from an array. var listOfNumbers = [1, 2, 3, 10, 100] for number in listOfNumbers { if number % 2 != 0 { print(number) } }
  • 26.
    Questions 3. Write aprogram to sum the positive elements of array as well as negative elements of array. var listOfNumbers = [1, -2, 3, -6 , 20] var sumPos = 0 ,sumNeg = 0 for number in listOfNumbers { if number>0 { sumPos += number } else { sumNeg += number } } print(sumPos) print(sumNeg)