SlideShare a Scribd company logo
1 of 26
Download to read offline
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

What's hot

MATLAB for Technical Computing
MATLAB for Technical ComputingMATLAB for Technical Computing
MATLAB for Technical ComputingNaveed Rehman
 
Operation and expression in c++
Operation and expression in c++Operation and expression in c++
Operation and expression in c++Online
 
Sorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - NotesSorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - NotesOmprakash Chauhan
 
Matrices, Arrays and Vectors in MATLAB
Matrices, Arrays and Vectors in MATLABMatrices, Arrays and Vectors in MATLAB
Matrices, Arrays and Vectors in MATLABAbu Raihan Ibna Ali
 
Bucket sort- A Noncomparision Algorithm
Bucket sort- A Noncomparision AlgorithmBucket sort- A Noncomparision Algorithm
Bucket sort- A Noncomparision AlgorithmKrupali Mistry
 
c++ programming Unit 4 operators
c++ programming Unit 4 operatorsc++ programming Unit 4 operators
c++ programming Unit 4 operatorsAAKASH KUMAR
 
Counting Sort and Radix Sort Algorithms
Counting Sort and Radix Sort AlgorithmsCounting Sort and Radix Sort Algorithms
Counting Sort and Radix Sort AlgorithmsSarvesh Rawat
 
Csci101 lect03 algorithms_i
Csci101 lect03 algorithms_iCsci101 lect03 algorithms_i
Csci101 lect03 algorithms_iElsayed Hemayed
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - NotesOmprakash Chauhan
 
ML - Multiple Linear Regression
ML - Multiple Linear RegressionML - Multiple Linear Regression
ML - Multiple Linear RegressionAndrew Ferlitsch
 
Python project2
Python project2Python project2
Python project2Young Song
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssionseShikshak
 

What's hot (18)

Operators and Expressions
Operators and ExpressionsOperators and Expressions
Operators and Expressions
 
MATLAB for Technical Computing
MATLAB for Technical ComputingMATLAB for Technical Computing
MATLAB for Technical Computing
 
Operation and expression in c++
Operation and expression in c++Operation and expression in c++
Operation and expression in c++
 
Sorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - NotesSorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - Notes
 
Matrices, Arrays and Vectors in MATLAB
Matrices, Arrays and Vectors in MATLABMatrices, Arrays and Vectors in MATLAB
Matrices, Arrays and Vectors in MATLAB
 
Programacion
ProgramacionProgramacion
Programacion
 
DATA TYPE IN PYTHON
DATA TYPE IN PYTHONDATA TYPE IN PYTHON
DATA TYPE IN PYTHON
 
Bucket sort- A Noncomparision Algorithm
Bucket sort- A Noncomparision AlgorithmBucket sort- A Noncomparision Algorithm
Bucket sort- A Noncomparision Algorithm
 
c++ programming Unit 4 operators
c++ programming Unit 4 operatorsc++ programming Unit 4 operators
c++ programming Unit 4 operators
 
Counting Sort and Radix Sort Algorithms
Counting Sort and Radix Sort AlgorithmsCounting Sort and Radix Sort Algorithms
Counting Sort and Radix Sort Algorithms
 
chapter-8.ppt
chapter-8.pptchapter-8.ppt
chapter-8.ppt
 
Csci101 lect03 algorithms_i
Csci101 lect03 algorithms_iCsci101 lect03 algorithms_i
Csci101 lect03 algorithms_i
 
C Building Blocks
C Building Blocks C Building Blocks
C Building Blocks
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
ML - Multiple Linear Regression
ML - Multiple Linear RegressionML - Multiple Linear Regression
ML - Multiple Linear Regression
 
Python project2
Python project2Python project2
Python project2
 
Alg2 lesson 2-6
Alg2 lesson 2-6Alg2 lesson 2-6
Alg2 lesson 2-6
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssions
 

Similar to Arrays and its properties IN SWIFT

Similar to Arrays and its properties IN SWIFT (20)

C Programming
C ProgrammingC Programming
C Programming
 
C code examples
C code examplesC code examples
C code examples
 
Cpl
CplCpl
Cpl
 
Understand more about C
Understand more about CUnderstand more about C
Understand more about C
 
Matlab1
Matlab1Matlab1
Matlab1
 
presentation.pptx
presentation.pptxpresentation.pptx
presentation.pptx
 
2014-mo444-practical-assignment-04-paulo_faria
2014-mo444-practical-assignment-04-paulo_faria2014-mo444-practical-assignment-04-paulo_faria
2014-mo444-practical-assignment-04-paulo_faria
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
 
Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structures
 
C and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfC and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdf
 
C lab-programs
C lab-programsC lab-programs
C lab-programs
 
Theory3
Theory3Theory3
Theory3
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
 
Lecture 7.pptx
Lecture 7.pptxLecture 7.pptx
Lecture 7.pptx
 
Introduction to programming c and data-structures
Introduction to programming c and data-structures Introduction to programming c and data-structures
Introduction to programming c and data-structures
 
What is c
What is cWhat is c
What is c
 
C important questions
C important questionsC important questions
C important questions
 
Few Operator used in c++
Few Operator used in c++Few Operator used in c++
Few Operator used in c++
 

More from LOVELY PROFESSIONAL UNIVERSITY

More from LOVELY PROFESSIONAL UNIVERSITY (19)

Enumerations, structure and class IN SWIFT
Enumerations, structure and class IN SWIFTEnumerations, structure and class IN SWIFT
Enumerations, structure and class IN SWIFT
 
Dictionaries IN SWIFT
Dictionaries IN SWIFTDictionaries IN SWIFT
Dictionaries IN SWIFT
 
Control structures IN SWIFT
Control structures IN SWIFTControl structures IN SWIFT
Control structures IN SWIFT
 
Array and its functionsI SWIFT
Array and its functionsI SWIFTArray and its functionsI SWIFT
Array and its functionsI SWIFT
 
practice problems on array IN SWIFT
practice problems on array IN SWIFTpractice problems on array IN SWIFT
practice problems on array IN SWIFT
 
practice problems on array IN SWIFT
practice problems on array  IN SWIFTpractice problems on array  IN SWIFT
practice problems on array IN SWIFT
 
practice problems on array IN SWIFT
practice problems on array IN SWIFTpractice problems on array IN SWIFT
practice problems on array IN SWIFT
 
practice problems on functions IN SWIFT
practice problems on functions IN SWIFTpractice problems on functions IN SWIFT
practice problems on functions IN SWIFT
 
10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING
 
Variables and data types IN SWIFT
 Variables and data types IN SWIFT Variables and data types IN SWIFT
Variables and data types IN SWIFT
 
Soft skills. pptx
Soft skills. pptxSoft skills. pptx
Soft skills. pptx
 
JAVA
JAVAJAVA
JAVA
 
Unit 5
Unit 5Unit 5
Unit 5
 
Unit 4
Unit 4Unit 4
Unit 4
 
Unit 3
Unit 3Unit 3
Unit 3
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
STRINGS IN JAVA
 
Unit 1
Unit 1Unit 1
Unit 1
 
COMPLETE CORE JAVA
COMPLETE CORE JAVACOMPLETE CORE JAVA
COMPLETE CORE JAVA
 
Data wrangling IN R LANGUAGE
Data wrangling IN R LANGUAGEData wrangling IN R LANGUAGE
Data wrangling IN R LANGUAGE
 

Recently uploaded

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 

Recently uploaded (20)

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 

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)