SlideShare a Scribd company logo
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)

More Related Content

PPTX
BB - Functions (Operations and Piecewise)
PPTX
MCM FUNCTIONS BLACKBOARD COMPATIBILITY
PPTX
Operators and expressions in C++
DOCX
C – operators and expressions
PPT
C Sharp Jn (2)
PPTX
February 10 2016
BB - Functions (Operations and Piecewise)
MCM FUNCTIONS BLACKBOARD COMPATIBILITY
Operators and expressions in C++
C – operators and expressions
C Sharp Jn (2)
February 10 2016

What's hot (18)

PPTX
Operators and Expressions
PDF
MATLAB for Technical Computing
PPT
Operation and expression in c++
PPTX
Sorting and Searching - Data Structure - Notes
PDF
Matrices, Arrays and Vectors in MATLAB
DOCX
Programacion
PPTX
DATA TYPE IN PYTHON
PPTX
Bucket sort- A Noncomparision Algorithm
PPTX
c++ programming Unit 4 operators
PPT
Counting Sort and Radix Sort Algorithms
PPT
chapter-8.ppt
PPTX
Csci101 lect03 algorithms_i
PDF
C Building Blocks
PPTX
Queue - Data Structure - Notes
PPTX
ML - Multiple Linear Regression
PPTX
Python project2
PPTX
Alg2 lesson 2-6
PPT
Mesics lecture 4 c operators and experssions
Operators and Expressions
MATLAB for Technical Computing
Operation and expression in c++
Sorting and Searching - Data Structure - Notes
Matrices, Arrays and Vectors in MATLAB
Programacion
DATA TYPE IN PYTHON
Bucket sort- A Noncomparision Algorithm
c++ programming Unit 4 operators
Counting Sort and Radix Sort Algorithms
chapter-8.ppt
Csci101 lect03 algorithms_i
C Building Blocks
Queue - Data Structure - Notes
ML - Multiple Linear Regression
Python project2
Alg2 lesson 2-6
Mesics lecture 4 c operators and experssions
Ad

Similar to Arrays and its properties IN SWIFT (20)

PPTX
Basic iOS Training with SWIFT - Part 2
PDF
Swift - the future of iOS app development
PPTX
presentationofswift.pptx
PDF
Programs in array using SWIFT
PPT
Swift-Programming Part 1
PPTX
Introduction to Swift (tutorial)
PDF
Introducing Swift v2.1
PPTX
IOS Swift Language 4th tutorial
PDF
NUS iOS Swift Talk
PDF
Think sharp, write swift
PDF
Workshop Swift
PDF
Swift Programming Language
PPTX
Net (f#) array
PDF
The swift programming language
PDF
The Swift Programming Language - Xcode6 for iOS App Development - AgileInfowa...
PDF
Swift Programming Language
PDF
What Swift can teach us all
PPTX
JavaScript / Web Engineering / Web Development / html + css + js/presentation
PDF
Variables and data types IN SWIFT
PPTX
IOS Swift language 2nd tutorial
Basic iOS Training with SWIFT - Part 2
Swift - the future of iOS app development
presentationofswift.pptx
Programs in array using SWIFT
Swift-Programming Part 1
Introduction to Swift (tutorial)
Introducing Swift v2.1
IOS Swift Language 4th tutorial
NUS iOS Swift Talk
Think sharp, write swift
Workshop Swift
Swift Programming Language
Net (f#) array
The swift programming language
The Swift Programming Language - Xcode6 for iOS App Development - AgileInfowa...
Swift Programming Language
What Swift can teach us all
JavaScript / Web Engineering / Web Development / html + css + js/presentation
Variables and data types IN SWIFT
IOS Swift language 2nd tutorial
Ad

More from LOVELY PROFESSIONAL UNIVERSITY (18)

PPTX
Enumerations, structure and class IN SWIFT
PDF
Dictionaries IN SWIFT
PDF
Control structures IN SWIFT
PDF
Array and its functionsI SWIFT
PDF
practice problems on array IN SWIFT
PDF
practice problems on array IN SWIFT
PDF
practice problems on array IN SWIFT
PDF
practice problems on functions IN SWIFT
PDF
10. funtions and closures IN SWIFT PROGRAMMING
PPTX
PPT
PPT
COMPLETE CORE JAVA
PPT
Data wrangling IN R LANGUAGE
Enumerations, structure and class IN SWIFT
Dictionaries IN SWIFT
Control structures IN SWIFT
Array and its functionsI SWIFT
practice problems on array IN SWIFT
practice problems on array IN SWIFT
practice problems on array IN SWIFT
practice problems on functions IN SWIFT
10. funtions and closures IN SWIFT PROGRAMMING
COMPLETE CORE JAVA
Data wrangling IN R LANGUAGE

Recently uploaded (20)

PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Pharma ospi slides which help in ospi learning
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
Lesson notes of climatology university.
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
VCE English Exam - Section C Student Revision Booklet
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
01-Introduction-to-Information-Management.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Complications of Minimal Access Surgery at WLH
Pharma ospi slides which help in ospi learning
O5-L3 Freight Transport Ops (International) V1.pdf
Cell Types and Its function , kingdom of life
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Lesson notes of climatology university.
STATICS OF THE RIGID BODIES Hibbelers.pdf
O7-L3 Supply Chain Operations - ICLT Program
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
VCE English Exam - Section C Student Revision Booklet

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 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
  • 7. Array Example 1 : var arr=[4,2,6,9,15,3] for n in arr { print(n) } Result 4 2 6 9 15 3
  • 8. Array Example 2 : var arr=["Red","Green","Yellow","Black"] for n in arr { print(n) } Result Red Green Yellow Black
  • 9. Empty Array • Creating an array without any value is called Empty Array. • Example: var emptyArray: [Int] = [ ] print(emptyArray) Result [ ]
  • 10. 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
  • 11. 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.") }
  • 12. 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]
  • 13. 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]
  • 14. 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]
  • 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 • 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)
  • 17. 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
  • 18. 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
  • 19. 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
  • 20. 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
  • 21. 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
  • 22. 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
  • 23. 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.
  • 24. 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)
  • 25. 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) } }
  • 26. 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)