Skip to content

Instantly share code, notes, and snippets.

@mildocjr
Last active May 20, 2019 23:38
Show Gist options
  • Save mildocjr/f4def7cf9ad9947e6ac7f4f10ed43047 to your computer and use it in GitHub Desktop.
Save mildocjr/f4def7cf9ad9947e6ac7f4f10ed43047 to your computer and use it in GitHub Desktop.
swift-operators-1
// Math Operators
// Addition
var a = 10 + 20 // a is equal to 30 (10 + 20 = 30)
// Subtraction
var s = 100 - 75 // s is equal to 25 (100 - 75 = 25)
// Multiplication - we use * to multiply
var m = 10 * 15 // m is equal to 150 (10 x 15 = 150)
// Division - we use / to divide
var d = 100 / 50 // d is equal to 2 (100 ÷ 50 = 2)
/*
*note about division, it is easy to divide numbers and end up
* with a float value (decimal value)
* if you do not make the variable strongly typed you will end up
* with an an Integer value (6 / 5 = 1)
* (e.g. var a: Float = 50 / 100 or var a: Double = 50 /100)
*/
// Comparison operators
// Greater than
var gt = 2 > 1 // gt is equal to true (2 is bigger)
// Less than
var lt = 2 < 1 // lt is equal to false (2 is not smaller)
// Equal to - we use double equal signs to determine equality
// we use a single equal sign for assignment
var eq = (2 == 2) // eq is equal to true (2 is equal to 2)
// Greater than or equal to
var gtEq = (2 >= 2) // gtEq is equal to true (2 is equal to 2)
// Less than or equal to
var ltEq = (2 <= 3) // ltEq is equal to true
// Unary Operators (1 operand, binary is two operands as above)
// Not equal
var notEq = (2 != 3) // notEq is true
// Minus operator (there is a plus but it does not change anything)
var minus = -(-100) // minus is 100
// Logical Operators
// And operator - this and that must be true
var andOp = (true && 3 == 3) // andOp equals true
// Or operator - this or that must be true
var orOp = (true || false). // orOp equals true
// Not operator - reverses the result of a boolean evaluation
var isRaining = !true // isRaining is false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment