Variables, numbers and data type – Swift 3

Spread the love

One can represent numbers using swift just creating a variable. variables, numbers and data type is essential in every programming language. Swift  automatically detects which type of Variable it is. If you want to mention the variable type just add : after the variable name and then mention the type.

Variable is a container to keep data. Data type can be Integer, Double, Float, Boolean type.

Integer can be of 2 types.

One is bit-wise Signed, eg. Int8, Int16, Int32, Int64

and another one is bit-wise Unsigned, eg. UInt8, UInt16, UInt32, UInt64

We can concat or print some variables inside a Sting using String Interpolation ( \ – backslash ).

Check out the examples for some demonstration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//: Playground - noun: a place where people can play
 
import UIKit
 
// Number Practice 
 
var age = 28
 
//expicitly devleared type 
 
var weight:Int = 76
 
var salary:UInt64 = 20000
 
var s = 50.0
 
var s: Double = 20.0
 
var t = true
 
var f = False
 
// Arithmetic Operators 
/*
    + - / *
 */
 
var area = 15*10 // 150
 
var sum = 5+5 // 10
 
var diff = 10-3 //07
 
var div = 10/3 // 3 
 
var remainder = 16 % 5 // 1
 
print ("The result of sum 5+5 =\(sum),
        diff 10-3 = \(diff) , div 10/3 = \(div)
        and remiander 10/3 = \(remainder)")
 
 
var randomnumber = 50
 
if randomnumber % 3 == 0 {
    print("is enven")
}else{
    print("is Odd")
}

These are simple examples for Variables and Data types. Swift 3 documentation is one of the best documentation I have read so far. For further just go through the doc.