做...在Scala循环 (do...while loop in Scala)

do...while loop in Scala is used to run a block of code multiple numbers of time. The number of executions is defined by an exit condition. If this condition is TRUE the code will run otherwise it runs the first time only

Scala中的do ... while循环用于多次运行代码块。 执行次数由退出条件定义。 如果此条件为TRUE,则代码将运行,否则它将仅在第一次运行

The do...while loop is used when the program does not have information about the exact number of executions taking place. The number of executions is defined by an exit condition that can be any variable or expression, the value evaluated in TRUE if it's positive and FALSE if it's zero.

当程序没有有关发生的确切执行次数的信息时,使用do ... while循环 。 执行次数由退出条件定义,退出条件可以是任何变量或表达式,如果值为正数则为TRUE,如果为零则为FALSE

This loop always runs once in the life span of code. If the condition is initially FALSE. The loop will run once in this case.

该循环在代码的生命周期中始终运行一次。 如果条件最初为FALSE 。 在这种情况下,循环将运行一次。

The do...while loop is also called exit controlled loop because its condition is checked after the execution of the loop's code block.

do ... while循环也称为退出控制循环,因为在执行循环的代码块后会检查其条件。

Syntax of do...while loop:

do ... while循环的语法:

    do{
//Code to be executed...
}
while(condition);

Flow chart of do...while loop:

do ... while循环流程图:

Example of do...while loop:

do ... while循环的示例:

object MyClass {def main(args: Array[String]) {var myVar = 12;
println("This code prints myVar even if it is greater that 10")
do{println(myVar)
myVar += 2;
}
while(myVar <= 10)
}
}

Output

输出量

This code prints myVar even if it is greater that 10
12

Code explanation:

代码说明:

This code implements the use of the do...while loop in Scala. The do...while loop being an exit control loop checks the condition after the first run. This is why the code prints 12 but the condition is myVar should not be greater than 10. In this we put the condition after the code block this means the code will run like this, print myVar increment it by 2 (using assignment operator) and then checks for the condition of the loop.

这段代码实现了Scala中do ... while循环的使用。 作为退出控制循环的do ... while循环在第一次运行后检查条件。 这就是为什么代码打印12但条件为myVar不应大于10的原因。在此,我们将条件放在代码块之后,这意味着代码将像这样运行,打印myVar将其递增2(使用赋值运算符 ),然后检查循环条件。

The assignments for the do...while loop that you can complete and submit to know your progress.

您可以完成并提交do ... while循环的作业,以了解自己的进度。

Assignment 1 (difficulty - beginner): Print all prime numbers from 342 - 422 that is divisible by 3. (use do-while loop and functions.)

作业1(难度-初学者):打印342-422中所有可被3整除的质数。(使用do-while循环和函数。)

Assignment 2 (difficulty - intermediate): Print all number between 54 - 1145 that have 11,13 and 17 as a factor.

作业2(难度-中间):打印54-1145之间的所有数字,其中11、13和17为因数。

翻译自: https://www.includehelp.com/scala/the-do-while-loop-in-scala.aspx

Scala中的do ... while循环相关推荐

  1. Scala中的嵌套循环

    Scala中的嵌套循环 (Nested loop in Scala) In programming, a nested loop is used in initializing or iterate ...

  2. Scala中没有break和continue, 如何退出循环

    Java是指令式风格,Scala是函数式风格. 在Scala中,应该尽量适用循环,而是应用函数的方式来处理. Scala并没有提供break和continue语句来退出循环,那么如果我们又确实要怎么办 ...

  3. scala条件替换_scala - 有没有办法用scala中的高阶方法替换嵌套的For循环 - SO中文参考 - www.soinside.com...

    有没有办法用scala中的高阶方法替换嵌套的For循环 问题描述 投票:0回答:1 我有一个mutableList,并希望获取所有行的总和,并根据某些条件将其行替换为其他值.下面的代码对我来说工作正常 ...

  4. Scala中的while循环

    在Scala中的while循环 (while loop in Scala) while loop in Scala is used to run a block of code multiple nu ...

  5. scala中循环守卫_Scala中的循环

    scala中循环守卫 Scala中的循环 (Loops in Scala) In programming, many times a condition comes when we need to e ...

  6. scala学习手记2 - scala中的循环

    先来看一段Java中的循环: for (int i = 1; i < 4; i++) {System.out.print(i + ","); } 毫无疑问,scala可以让这 ...

  7. Scala中集合类型与java中集合类型转换

    对于java中的集合元素并不能在scala中拿来就用的,需要进行相应的转换. 1. 转换规则如下 从下面可以看出,有些可以相互转换的,有些只能单向转换: scala.collection.Iterab ...

  8. Scala 中的函数式编程基础(一)

    主要来自 Scala 语言发明人 Martin Odersky 教授的 Coursera 课程 <Functional Programming Principles in Scala>. ...

  9. Scala 中的函数式编程基础

    主要来自 Scala 语言发明人 Martin Odersky 教授的 Coursera 课程 <Functional Programming Principles in Scala>. ...

最新文章

  1. VUE的本地应用-V-TEXT
  2. 使用es6中import和export报错
  3. PHP设计模式之适配器模式
  4. activiti自定义流程之Spring整合activiti-modeler5.16实例(四):部署流程定义
  5. pytorch的nn.CrossEntropyLoss()函数使用方法
  6. Spark-submit执行流程,了解一下
  7. python安装mysqldb模块_python MysqlDb模块安装及其使用详解
  8. 关于select标签曾经踩过的几个坑!
  9. oracle集群启动状态,oracle 11g集群软件无法启动解决步骤
  10. python enumerate() 函数的使用方法
  11. static 和 const的解释(转载)
  12. python大数据和java大数据的区别-学习大数据先学Python还是JAVA?
  13. mysql max as_mysql使用max函数+将类似123的字符型数据转换成数据类型
  14. python vscode下载和安装教程(windows)
  15. 服务器系统巡查登记表,信息设备巡检记录表
  16. P2P网贷谋求“去担保”:引入保险 收益下滑
  17. 问:平面布置图是什么?有什么作用?如何判断好的平面布置图方案?如何绘制?
  18. 使用浏览器插件,下载网页中的图片
  19. 银行间同业拆借利率有什么用?
  20. 适用于linux的解压工具,Linux下的压缩/解压缩工具

热门文章

  1. react学习路线图,学习react就是有捷径
  2. 方向盘左右能摇动_学车这么久了,你还不会打方向盘呢?
  3. js slice 参数为负值
  4. 使用FormData进行Ajax请求上传文件
  5. 如何通过CSS开启硬件加速来提高网站性能
  6. 网页变成灰色,对重大事件表示哀悼
  7. 前端学习---css基本知识
  8. kali linux 开启配置ssh服务
  9. [POJ1463] Strategic game
  10. 网络基础之 Nmap 命令