Chapter 10 : Swift Literals

As per wikipedia meaning of Literal in computer programming is a notation for representing a fixed value in source code.

Almost all programming languages have notations for atomic values such as integers, floating-point numbers, and strings, and usually for booleans and characters; some also have notations for elements of enumerated types and compound values such as arrays, records and objects.

Boolean Literal

True / False Nil

String Literal

“Hello 9to5ios”

Integer Literal 

95

Floating point Literal

3.14

Boolean Literals

It can be either

nil for nil representing for no value

False for representing false

True for representing true

Integer Literals

It can be a binary, octal, decimal or hexadecimal constant.

for example 

let integer_decimal = 17                 // decimal notation

let integer_binary = 0b10010          // binary notation

let integer_octal = 0o32                 // octal notation

let integer_hexadecimal = 0x12      // hexadecimal notation

String Literals

It can a sequence of characters inside double quotes.

for example :

“testString”

let stringA = “This is test ”

print (stringA)

Output

This is test

There are below escape sequence we can used : 

Escape sequence symbols Meaning
\0 Null Character
\\ \character
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\’ Single Quote
\” Double Quote
\000 Octal number of one to three digits
\xhh… Hexadecimal number of one or more digits

Floating-point Literals

It contain decimal points , fractional parts, integer part, exponent part.

It is used for decimal form or hexadecimal form.

for example

let double_decimal = 95.101

let double_exponent = 1.14923e2

let double_hexadecimal = 0xC.3p0

Add a Comment

Your email address will not be published. Required fields are marked *