We provide complete mobile and web apps development solutions

Showing posts with label operators in swift. Show all posts
Showing posts with label operators in swift. Show all posts

Tuesday, August 2, 2016

Operators in Swift

Operators:
An Operator a special symbol that tells the compiler to perform mathematical or logical operations.

Assignment Operator:

The assignment operator (a=b) initailizes or updates the vlaue of a with a vlue of b.

let b = 12
var a = 10
a = b  // a is now 12

Arithmetic Operators :


Swift supports 4 arithmetic operators.

Addition
Subtraction
Multiplication
Division

Remainder Operator :

9 % 4 = 1

5 % 1.5 = 0.5



Comparison Operator :

Equal to (a == b)
Not equal to (a != b)
Greater than (a > b)
less than (a < b)   .................etc

Ternary Conditional Operator :

Ternary conditional operator is a special operator with 3 parts.

question ? answer 1 : answer 2

if question {
answer 1
} else {
answer2
}


if question is true, it evaluates answer 1, otherwise, it evaluates answer 2.

Range Operators:

Closed Range Operator:

The closed range operator (a...b) defines a range from a to b,incluses vlues a and b.

ex:

  for index in 1...6 {
 
  print("\(index) times 6 is \(index * 5)")
  }
 
  // 1 times 5 is 5
  // 2 times 5 is 10
  // 3 times 5 is 15
  // 4 times 5 is 20
  // 5 times 5 is 25
 
  Half- Open range operator :
 
  The half open range operator (a..<b) defines a range from a to b, but does not include b.

let countrynames = ["India","Malaysia","USA","Singapore","Thailand"]

 let count =  countrynames.count

 for i in 0..<count {
print("Country \(i+1) is called \(countrynames[i])")
}
// Country 1 is called India
// Country 2 is called Malaysia
.
//// Country 5 is called Thailand


Logical Operators :
Logical operators are similar to c, java languages.
Logical NOT (!a)
Logical AND (a && b)

Logical OR (a || b)

Online Training

Your Name :
Your Email: (required)
Your Message: (required)

Powered by Blogger.

Recent Posts

Find Us On Facebook

Popular Posts