Swift Operators

This entry is part 24 of 41 in the series Swift

Arithmetic, comparison and logical operators.

// Swift All Operators

var x : Int = 10
var y : Bool = false

// Arithmetic Operators
x = x + 10; print("X = \(x)")     // (+)  Operator example
x = x - 15; print("X = \(x)")     // (-)  Operator example
x = x * 10; print("X = \(x)")     // (*)  Operator example
x = x / 5;  print("X = \(x)")     // (/)  Operator example
x = x % 10; print("X = \(x)")     // (%)  Operator example
x++;        print("X = \(x)")     // (++) Operator example
--x;        print("X = \(x)")     // (--) Operator example
print ("")
// Comparison Operators
x == 0  ?   print("True") :   print("False")       // Equla to operator
x != 0  ?   print("True") :   print("False")       // Not equal to operator
x < 0   ?   print("True") :   print("False")       // Less than operator
x > 0   ?   print("True") :   print("False")       // Greater than operator
x <= 0  ?   print("True") :   print("False")       // Less equal operator
x >= 0  ?   print("True") :   print("False")       // Greater equal operator
print ("")
// Logical Operators
x == 0 && x < 1 ?   print("True") :   print("False")     // And Operator
x == 0 || x == 1 ?  print("True") :   print("False")     // Or Operator
!y == false   ?     print("True") :   print("False")     // Not Operator

swift

Series Navigation<< Swift: Else if
Swift: Adding and inserting items to a list >>

Leave a comment

Leave a Reply