大数据全系列 教程
1869个小节阅读:467.2k
目录
408考研
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
当循环条件表达式返回值为true时,执行循环体代码。
xxxxxxxxxx
while( 循环条件表达式 ) {
循环体
}
Scala中不能使用count++或--,只能使用count = count+1简化为count += 1;--同理
xxxxxxxxxx
package cn.itbaizhan.chapter02
object WhileLoop {
def main(args: Array[String]): Unit = {
//定义一个计数器
//var index = 0
var index:Int = 0
//有可能一次也不执行 比如index>=5
while(index<5){
println("第"+index+"次while循环")
//改变计数器 scala中不支持自增++ 自减-- index -= 1
index += 1
}
}
}
输出:
xxxxxxxxxx
第0次while 循环
第1次while 循环
第2次while 循环
第3次while 循环
第4次while 循环
一种特殊的while循环就是,先执行循环体,再判断循环条件是否成立
xxxxxxxxxx
do {
循环体
} while ( 循环条件表达式 )
xxxxxxxxxx
package cn.itbaizhan.chapter02
object DoWhileLoop {
def main(args: Array[String]): Unit = {
var index = 0
do{
index +=1
println("第"+index+"次do while 循环")
}while(index <5 )
}
}
输出:
xxxxxxxxxx
第1次do while 循环
第2次do while 循环
第3次do while 循环
第4次do while 循环
第5次do while 循环
实时效果反馈
1. 关于Scala while和do while循环控制的描述,错误的是:
A 当while循环条件表达式返回值为true时,执行循环体代码。
B while循环是先判断,符合条件再执行一次循环体。
C do...while循环就是,先执行循环体,再判断循环条件是否成立。
D do...while循环可能一次也不执行。
答案:
1=>D do...while循环至少执行一次。