SlideShare une entreprise Scribd logo
1  sur  23
Télécharger pour lire hors ligne
SWIFT Programming basics
Contents
 Variables
 Constants
 Using print statement
 Data Types
 Operators
Kanika Sharma
2
Variables
 variables are used to store data in memory which can be used
throughout the program.
 Each variable must be given a unique name called identifier.
 In Swift, we use var keyword to declare a variable
Kanika Sharma
3
Assigning values to variables
 At the time of declaration
 Var name = “MCA”
 Later on in the program but using type annotation
var name : String
name=“mca”
OR
var name : String = “MCA”
Kanika Sharma
4
Constants
 In Swift, we use let keyword to declare a variable
Kanika Sharma
5
Literal
 A literal is a value that appears directly in your source code.
 It can be a number, character, or a string etc. For e.g: "Hello, World" , 12,
23.0, "C" are simple example of literals.
 Literals are often used to initialize (assign values to) variables or constants.
Kanika Sharma
6
Types of Literal
 Integer Literal
 Binary Literals
 Represents binary value.
 Begins with 0b.
 Octal Literals
 Represents octal value.
 Begins with 0o.
 Hexadecimal Literals
 Represents hexadecimal value.
 Begins with 0x.
 Decimal Literals
 Represents decimal value.
 Begins with nothing. Everything you declare in integer literal is of type decimal.
Kanika Sharma
7
Types of Literal
 String Literal
 Character Literal
 Decimal
 Boolean
Kanika Sharma
8
Best practices for naming Variable &
Constants
 Choose a name that makes sense. For example, var name makes more sense
than var n.
 Use camelCase notation to declare a variable or a constant. Camel-case
notation starts with lowercase letter.
 You can also define variables and constants without labeling it. Not labeling
with name means you are not going to use it in the program.
 Use constants if you only need to set a value once and never need to change
it again during a program. However, if you do need to change it at a later
point, use variables.
 Constant and variable names cannot contain whitespace characters,
mathematical symbols, arrows, private-use (or invalid) Unicode code points, or
line- and box-drawing characters. Nor can they begin with a number,
although numbers may be included elsewhere within the name.
Kanika Sharma
9
Using print statement
 Printing to the screen using simple print() function
 Print(“hello”)
 Printing constants, variables and literals
 Print(45)
 Printing without a link break using terminator parameter
 print("Hello”, terminator: " ")
 print(“MCA")
 print(“Welcome")
Kanika Sharma
10
Using print statement
 Printing multiple items using single print function
 print(“Welcome to", 2020, “MCA", separator: " ")
 Printing multiple lines
 print("Hello, rMCA")
 Printing multiple lines using triple quotes
 print("""
 Hello,
 MCA
 """)
Kanika Sharma
11
Using print statement
 Printing variables using string interpolation
var a = 10
var b = 20
var c = a + b
print("sum of (a) and (b) is",c)
Kanika Sharma
12
Example on print
var a = 10print(a) // Value
print(111) //Literal
print("Welcome",terminator:" ")
print("MCA")
print("Bye")
print("welcome","MCA","in",2020, separator:" .")
print("Hello rMCA")
print(""“
I
Am
SWIFT
""")
Kanika Sharma
13
Data Types
 Swift offers a collection of built-in data types which are string, integer,
floating-point numbers, and Booleans.
Kanika Sharma
14
Integers(Int)
 Integers (Int) are whole numbers.
 Variable/Constant declared of integer type can store whole numbers both positive and
negative including zero with no fractional components .
 Default Value: 0
 Size: 32/64 bit depends on the platform type.
 Range: -2,147,483,648 to 2,147,483,647 (32 bit platform)
 -9223372036854775808 to 9223372036854775807 (64 bit platform
 There are many variants of Integer type.e.g. UInt, Int8, Int16 etc. The most common one you use
is of Int.
 If you have a requirement to specify the size/type of integer a variable/constant can hold, you
can store it more specifically using UInt, Int8, Int16 etc.
Kanika Sharma
15
Integers(Int8)
 Int8
 Variant of Integer type that can store both positive and negative small
numbers.
 Default Value: 0
 Size: 8 bit
 Range: -128 to 127
 You can also find out the highest and lowest value a type can store using
.min and .max .
Kanika Sharma
16
Integers(Uint)
 Variant of Integer type called UInt (Unsigned Integer) which can only store
unsigned numbers (positive or zero).
 Default Value: 0
 Size: 32/64 bit depends on the platform type.
 Range: 0 to 4294967295 (32 bit platform)
 0 to 18446744073709551615 (64 bit platform)
Kanika Sharma
17
Floating Point (Float)
 Variables or Constants declared of float type can store number with
decimal or fraction points.
 Floating-point numbers are numbers with a fractional component, such as
4.993, 0.5, and −234.99.
 Default Value: 0.0
 Size: 32 bit floating point number.
 Range: 1.2*10-38 to 3.4 * 1038 (~6 digits)
Kanika Sharma
18
Floating Point (Double)
 Variables / Constants declared of Double type also stores number with
decimal or fraction points as Float but larger decimal points than Float
supports.
 Default value : 0.0
 Size: 64-bit floating point number. (Therefore, a variable of type Double can
store number with decimal or fraction points larger than Float supports)
 Range: 2.3*10-308 to 1.7*10308 (~15 digits)
Kanika Sharma
19
Boolean(Bool)
 Booleans (bools) are referred to as logical because they can either be
true or false.
 Variable/Constant declared of Bool type can store only two values either
true or false.
 Use Booleans when you need to determine whether some logic is true or
false.
 Default Value: false
Kanika Sharma
20
Character
 Variables/Constants declared of Character type can store a single-
character string literal.
 You can include emoji or languages other than english as an character in
Swift using escape sequence u{n} (unicode code point ,n is in
hexadecimal)
Kanika Sharma
21
String
 Variables or Constants declared of String type can store collection of characters.
 Default Value: "" (Empty String)
 It is Value type.
 You can use for-in loop to iterate over a string.
 Swift also supports a few special escape sequences to use them in string. For example,
 0 (null character),
  (a plain backslash ),
 t (a tab character),
 v (a vertical tab),
 r (carriage return),
 " (double quote),
 ' (single quote), and
 u{n} (unicode code point ,n is in hexadecimal).
Kanika Sharma
22
Comments
 Comments are a great way to create notes or reminders to yourself. When
you comment code, it means that it will not execute when your code
runs. There are two types of comments used: // or /* */. // is used for a
one-line comment and /**/ is used for a block of text.
Kanika Sharma
23

Contenu connexe

Tendances (20)

Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
Strings
StringsStrings
Strings
 
358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6
 
Strings in C
Strings in CStrings in C
Strings in C
 
LL(1) parsing
LL(1) parsingLL(1) parsing
LL(1) parsing
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Print input-presentation
Print input-presentationPrint input-presentation
Print input-presentation
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
 
concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D array
 
String in c
String in cString in c
String in c
 
String in c programming
String in c programmingString in c programming
String in c programming
 
Python strings
Python stringsPython strings
Python strings
 
Flow chart and pseudo code
Flow chart and pseudo code Flow chart and pseudo code
Flow chart and pseudo code
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Unit ii ppt
Unit ii pptUnit ii ppt
Unit ii ppt
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
Python ppt
Python pptPython ppt
Python ppt
 

Similaire à Variables and data types IN SWIFT

Constants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya JyothiConstants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya JyothiSowmyaJyothi3
 
C the basic concepts
C the basic conceptsC the basic concepts
C the basic conceptsAbhinav Vatsa
 
02. Primitive Data Types and Variables
02. Primitive Data Types and Variables02. Primitive Data Types and Variables
02. Primitive Data Types and VariablesIntro C# Book
 
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docxINPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docxjaggernaoma
 
C programming tutorial
C programming tutorialC programming tutorial
C programming tutorialMohit Saini
 
Fundamentals of Programming Constructs.pptx
Fundamentals of  Programming Constructs.pptxFundamentals of  Programming Constructs.pptx
Fundamentals of Programming Constructs.pptxvijayapraba1
 
unit 1 cpds.pptx
unit 1 cpds.pptxunit 1 cpds.pptx
unit 1 cpds.pptxmadhurij54
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++K Durga Prasad
 
434090527-C-Cheat-Sheet. pdf C# program
434090527-C-Cheat-Sheet. pdf  C# program434090527-C-Cheat-Sheet. pdf  C# program
434090527-C-Cheat-Sheet. pdf C# programMAHESHV559910
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic Manzoor ALam
 
CSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdfCSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdfssusera0bb35
 

Similaire à Variables and data types IN SWIFT (20)

Constants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya JyothiConstants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya Jyothi
 
Data type
Data typeData type
Data type
 
Unit 2- Module 2.pptx
Unit 2- Module 2.pptxUnit 2- Module 2.pptx
Unit 2- Module 2.pptx
 
C the basic concepts
C the basic conceptsC the basic concepts
C the basic concepts
 
02. Primitive Data Types and Variables
02. Primitive Data Types and Variables02. Primitive Data Types and Variables
02. Primitive Data Types and Variables
 
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docxINPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
 
C programming tutorial
C programming tutorialC programming tutorial
C programming tutorial
 
Fundamentals of Programming Constructs.pptx
Fundamentals of  Programming Constructs.pptxFundamentals of  Programming Constructs.pptx
Fundamentals of Programming Constructs.pptx
 
unit 1 cpds.pptx
unit 1 cpds.pptxunit 1 cpds.pptx
unit 1 cpds.pptx
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
434090527-C-Cheat-Sheet. pdf C# program
434090527-C-Cheat-Sheet. pdf  C# program434090527-C-Cheat-Sheet. pdf  C# program
434090527-C-Cheat-Sheet. pdf C# program
 
Pengaturcaraan asas
Pengaturcaraan asasPengaturcaraan asas
Pengaturcaraan asas
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
CSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdfCSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdf
 
keyword
keywordkeyword
keyword
 
keyword
keywordkeyword
keyword
 
C#
C#C#
C#
 

Plus de LOVELY PROFESSIONAL UNIVERSITY

Plus de 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
 
Arrays and its properties IN SWIFT
Arrays and its properties IN SWIFTArrays and its properties IN SWIFT
Arrays and its properties 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
 
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
 

Dernier

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 

Dernier (20)

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 

Variables and data types IN SWIFT

  • 2. Contents  Variables  Constants  Using print statement  Data Types  Operators Kanika Sharma 2
  • 3. Variables  variables are used to store data in memory which can be used throughout the program.  Each variable must be given a unique name called identifier.  In Swift, we use var keyword to declare a variable Kanika Sharma 3
  • 4. Assigning values to variables  At the time of declaration  Var name = “MCA”  Later on in the program but using type annotation var name : String name=“mca” OR var name : String = “MCA” Kanika Sharma 4
  • 5. Constants  In Swift, we use let keyword to declare a variable Kanika Sharma 5
  • 6. Literal  A literal is a value that appears directly in your source code.  It can be a number, character, or a string etc. For e.g: "Hello, World" , 12, 23.0, "C" are simple example of literals.  Literals are often used to initialize (assign values to) variables or constants. Kanika Sharma 6
  • 7. Types of Literal  Integer Literal  Binary Literals  Represents binary value.  Begins with 0b.  Octal Literals  Represents octal value.  Begins with 0o.  Hexadecimal Literals  Represents hexadecimal value.  Begins with 0x.  Decimal Literals  Represents decimal value.  Begins with nothing. Everything you declare in integer literal is of type decimal. Kanika Sharma 7
  • 8. Types of Literal  String Literal  Character Literal  Decimal  Boolean Kanika Sharma 8
  • 9. Best practices for naming Variable & Constants  Choose a name that makes sense. For example, var name makes more sense than var n.  Use camelCase notation to declare a variable or a constant. Camel-case notation starts with lowercase letter.  You can also define variables and constants without labeling it. Not labeling with name means you are not going to use it in the program.  Use constants if you only need to set a value once and never need to change it again during a program. However, if you do need to change it at a later point, use variables.  Constant and variable names cannot contain whitespace characters, mathematical symbols, arrows, private-use (or invalid) Unicode code points, or line- and box-drawing characters. Nor can they begin with a number, although numbers may be included elsewhere within the name. Kanika Sharma 9
  • 10. Using print statement  Printing to the screen using simple print() function  Print(“hello”)  Printing constants, variables and literals  Print(45)  Printing without a link break using terminator parameter  print("Hello”, terminator: " ")  print(“MCA")  print(“Welcome") Kanika Sharma 10
  • 11. Using print statement  Printing multiple items using single print function  print(“Welcome to", 2020, “MCA", separator: " ")  Printing multiple lines  print("Hello, rMCA")  Printing multiple lines using triple quotes  print("""  Hello,  MCA  """) Kanika Sharma 11
  • 12. Using print statement  Printing variables using string interpolation var a = 10 var b = 20 var c = a + b print("sum of (a) and (b) is",c) Kanika Sharma 12
  • 13. Example on print var a = 10print(a) // Value print(111) //Literal print("Welcome",terminator:" ") print("MCA") print("Bye") print("welcome","MCA","in",2020, separator:" .") print("Hello rMCA") print(""“ I Am SWIFT """) Kanika Sharma 13
  • 14. Data Types  Swift offers a collection of built-in data types which are string, integer, floating-point numbers, and Booleans. Kanika Sharma 14
  • 15. Integers(Int)  Integers (Int) are whole numbers.  Variable/Constant declared of integer type can store whole numbers both positive and negative including zero with no fractional components .  Default Value: 0  Size: 32/64 bit depends on the platform type.  Range: -2,147,483,648 to 2,147,483,647 (32 bit platform)  -9223372036854775808 to 9223372036854775807 (64 bit platform  There are many variants of Integer type.e.g. UInt, Int8, Int16 etc. The most common one you use is of Int.  If you have a requirement to specify the size/type of integer a variable/constant can hold, you can store it more specifically using UInt, Int8, Int16 etc. Kanika Sharma 15
  • 16. Integers(Int8)  Int8  Variant of Integer type that can store both positive and negative small numbers.  Default Value: 0  Size: 8 bit  Range: -128 to 127  You can also find out the highest and lowest value a type can store using .min and .max . Kanika Sharma 16
  • 17. Integers(Uint)  Variant of Integer type called UInt (Unsigned Integer) which can only store unsigned numbers (positive or zero).  Default Value: 0  Size: 32/64 bit depends on the platform type.  Range: 0 to 4294967295 (32 bit platform)  0 to 18446744073709551615 (64 bit platform) Kanika Sharma 17
  • 18. Floating Point (Float)  Variables or Constants declared of float type can store number with decimal or fraction points.  Floating-point numbers are numbers with a fractional component, such as 4.993, 0.5, and −234.99.  Default Value: 0.0  Size: 32 bit floating point number.  Range: 1.2*10-38 to 3.4 * 1038 (~6 digits) Kanika Sharma 18
  • 19. Floating Point (Double)  Variables / Constants declared of Double type also stores number with decimal or fraction points as Float but larger decimal points than Float supports.  Default value : 0.0  Size: 64-bit floating point number. (Therefore, a variable of type Double can store number with decimal or fraction points larger than Float supports)  Range: 2.3*10-308 to 1.7*10308 (~15 digits) Kanika Sharma 19
  • 20. Boolean(Bool)  Booleans (bools) are referred to as logical because they can either be true or false.  Variable/Constant declared of Bool type can store only two values either true or false.  Use Booleans when you need to determine whether some logic is true or false.  Default Value: false Kanika Sharma 20
  • 21. Character  Variables/Constants declared of Character type can store a single- character string literal.  You can include emoji or languages other than english as an character in Swift using escape sequence u{n} (unicode code point ,n is in hexadecimal) Kanika Sharma 21
  • 22. String  Variables or Constants declared of String type can store collection of characters.  Default Value: "" (Empty String)  It is Value type.  You can use for-in loop to iterate over a string.  Swift also supports a few special escape sequences to use them in string. For example,  0 (null character),  (a plain backslash ),  t (a tab character),  v (a vertical tab),  r (carriage return),  " (double quote),  ' (single quote), and  u{n} (unicode code point ,n is in hexadecimal). Kanika Sharma 22
  • 23. Comments  Comments are a great way to create notes or reminders to yourself. When you comment code, it means that it will not execute when your code runs. There are two types of comments used: // or /* */. // is used for a one-line comment and /**/ is used for a block of text. Kanika Sharma 23