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

时间:2021-06-19 19:16:50

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


 {

    temp += i

    i++
}

println(temp)

do {

    temp2 += j

    j++

} )

println(temp2)

... {

    temp3 += a
}
println(temp3)

for b in 1..1000 {

    temp5 += b}

PS:..表示不包括上限,...表示包括上限
println(temp5)

; b < ; 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))