练习1:循环1-20
var i = 1
while (i<=20){
println(i)
i +=1
}
练习2:循环for的基本用法
for (j <-1 to 20){
println(j)
}
3.for循环语句的步长
// 2.步长
// 12345678(步长为:1)
// 1 3 5 7 9 11 13(步长为:2)
for (i <-1 to 20 by 2){
println(i)
}
4.for语句的循环守卫
概念:可以在循环中添加条件,如果条件成立就执行循环。这个叫循环守卫。
for (j <-1 to 20 ;if (j%2==0);if(j%3==0)){
println(j)
}