Variables in Swift 3

September 27, 2017

In this article you can get understanding of how to work with variables in Swift 3
You can learn how to define variable and understanding different types of variable

You can define integer variable without specifying the data type

var x=2
var y=3
var z=x+y

print("The result is \(x+y)")

You can see the following output
The result is 5

Integar Data Type

You can define the integer variable in following way and do the operations

var a: Int = 4
var b: Int = 1
var c: Int = (a+b)

print("The result is \(a+b)")

Double Data Type

var a: Double = 4.1
var b: Double = 1.2
var c: Double = (a+b)

print("The result is \(a+b)")

Your output for the above code
The result is 5.3

Float Data Type

var a: Float = 4.1
var b: Float = 1.2
var c: Float = (a+b)

print("The result is \(a+b)")

Boolean Data Type

You can define the Boolean data type in following way. Boolean data type can have only two values True or False

var isValid: Bool = true
print("Value of isValid: \(isValid)")

You will have following output for above code
Value of isValid: true

String Data Type

Swift 3 has the String data type to work with string. In this example you can see how to define String variable and contact two string variables

var hello: String = "Hello"
var world: String = "World"
var str: String = hello + " " + world
print(str)

In our Playground you can have the following output
Hello World