这一章相对简单,只对运算符进行简单的举例讲解。
基本运算包括:
一、赋值(=)
例如:
let b = 10
var a = 5
a = b
// a is now equal to 10
二、算法(+ - * /)
例如:
1 +2 // equals 3
5 -3 // equals 2
2 *3 // equals 6
10.0 / 2.5 // equals4.0
三、自加和自减(++ 、--)
例如:
var i = 0
++i // i now equals 1
四、一目加减法(=-、=+)
例如:let three = 3
let minusThree = -three // minusThree equals -3
let plusThree = -minusThree // plusThree equals 3, or "minus minusthree"
五、混合运算(+=、-=)
例如:var a = 1
a += 2
// a is now equal to 3
六、比较运算(==、<、>、!=)
例如:1 == 1 // true, because 1 isequal to 1
2 !=1 // true, because 2 is not equal to 1
2 >1 // true, because 2 is greater than 1
1 <2 // true, because 1 is less than 2
1 >=1 // true, because 1 is greater than orequal to 1
2 <= 1 // false,because 2 is not less than or equal to 1
七、三元条件运算(?:)
例如:if question {
answer1
} else {
answer2
}
八、范围运算(a…b)
例如:for index in 1...5{
println("\(index) times 5 is \(index *5)")
}
// 1times 5 is 5
// 2times 5 is 10
// 3times 5 is 15
// 4times 5 is 20
// 5 times 5 is 25
九、逻辑运算(!a、a&&b、a||b)
例如:let allowedEntry =false
if!allowedEntry {
println("ACCESS DENIED")
}
// prints "ACCESS DENIED"
原文地址:http://blog.csdn.net/qi_ruihua/article/details/28478063