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

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

var i = 0



var temp = 0



while i < 1000 {

    

    temp += i

    

    i++

}



println(temp)



var j = 0



var temp2 = 0



do {



    temp2 += j



    j++



} while (j < 1000)



println(temp2)



var temp3 = 0



for a in 0...999 {

    

    temp3 += a

}


println(temp3)

for b in 1..1000 {

temp5 += b
}

PS:..表示不包括上限,...表示包括上限
println(temp5)
var temp4 = 0 for var b = 0; b < 1000; b++ { temp4 += b; } println(temp4)

 

func makeIncrementer() -> (Int -> Int) {

    func addOne(number: Int) -> Int {

        return 1 + number

    }

    return addOne

}

 

var increment = makeIncrementer()

 

println(increment(7))

你可能感兴趣的:(swift)