首页 > 代码库 > swift从0加到1000(不包括1000)的四种写法

swift从0加到1000(不包括1000)的四种写法

用了while, do...while, for in, for in ...

var i = 0var temp = 0while i < 1000 {        temp += i        i++}println(temp)var j = 0var temp2 = 0do {    temp2 += j    j++} while (j < 1000)println(temp2)var temp3 = 0for a in 0...999 {        temp3 += a}println(temp3)var temp4 = 0for var b = 0; b < 1000; b++ {        temp4 += b;}println(temp4)