scala中循环守卫

Scala中的循环 (Loops in Scala)

In programming, many times a condition comes when we need to execute the same statement or block of code more than one time. It could be difficult to write the same code multiple times, so programing language developers come up with a solution, name a loop statement.

在编程中,很多情况是我们需要多次执行同一条语句或代码块时出现的。 可能很难多次编写相同的代码,因此编程语言开发人员想出了一个解决方案,命名为loop语句

A Loop statement executes the specified block of code multiple times based on some condition.

Loop语句根据某种条件多次执行指定的代码块

Scala defines three types of loop statements. They are,

Scala定义了三种循环语句 。 他们是,

  1. for Loop

    循环

  2. while Loop

    while循环

  3. do...while Loop

    做...而循环

1)循环 (1) for Loop)

A for loop in used when you want to execute a block of code n number of times. The number n is specified explicitly. It is used when the user knows the number of times the loop needs to be executed beforehand. That is before the loop statement the number of executions is specified.

当您要执行n次代码块时,使用for循环 。 数字n是明确指定的。 当用户事先知道循环需要执行的次数时,将使用此方法。 那就是在循环语句之前指定执行次数。

In for loop, a variable is used. This variable is a counter to the number of loops that are executed using them for a statement.

for循环中 ,使用了一个变量。 此变量是一个计数器,该计数器针对使用一条语句执行的循环数。

Syntax:

句法:

    for(loop condition){
// code to be executed...
}

Example: There are various ways to use for loop we are discussing basic one here.

示例:有多种用于循环的方法,我们在这里讨论基本方法。

object MyClass {def add(x:Int, y:Int) = x + y;
def main(args: Array[String]) {var i = 0;
print("Print numbers from 5 to 10\n")
for(  i <- 5 to 10){print(i + "\n")
}
}
}

Output

输出量

Print numbers from 5 to 10
5
6
7
8
9
10

2)while循环 (2) while Loop)

A while loop is used when you want to execute a block of code some condition is TRUE. The condition can be any variable, or expression. When evaluated the all positive values are treated as TRUE and 0 is treated as FALSE.

当您要执行代码块时,使用while循环 ,某些条件为TRUE 。 条件可以是任何变量或表达式。 求值时,所有正值均被视为TRUE,0被视为FALSE

It checks the condition before entering the loop's block. If the condition is FALSE then the block is not executed. There might be a condition when the block of code does not execute at all. This condition arises when the condition is initially FALSE. There can also be infinite loop execution if the expression never turns FALSE and no internal termination statement is available.

它在进入循环块之前检查条件。 如果条件为FALSE ,则不执行该块。 当代码块根本不执行时,可能会出现某种情况。 该条件最初为FALSE时出现 。 如果表达式从不变为FALSE并且没有可用的内部终止语句,则还可能存在无限循环执行。

Syntax:

句法:

    while(condition){
//code to be executed
}

Example:

例:

object MyClass {def main(args: Array[String]) {var myVar = 2;
println("This code prints 2's table upto 10")
while(myVar <= 10){println(myVar)
myVar += 2;
}
}
}

Output

输出量

This code prints 2's table upto 10
2
4
6
8
10

3)做...而循环 (3) do...while Loop)

A do...while loop is when you want to execute a block of code until a specific condition is TRUE. The condition can be any variable or expression. This condition when evaluated is TRUE for all positive value and for 0 it is FALSE.

do ... while循环是您要执行代码块直到特定条件为TRUE时 。 条件可以是任何变量或表达式。 对所有正值求值时,此条件为TRUE ;对于0 ,则为FALSE

It checks the condition before exiting the loop's block. If the condition is FALSE then the block is executed only once. There is never a condition when the block of code does not execute at all. There can be loop execution if the expression never turns FALSE and no internal termination statement is done.

它在退出循环块之前检查条件。 如果条件为FALSE,则该块仅执行一次。 当代码块根本不执行时,永远不会有条件。 如果表达式从不变为FALSE并且没有完成内部终止语句,则可能存在循环执行。

Syntax:

句法:

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

Example:

例:

object MyClass {def main(args: Array[String]) {var myVar = 2;
println("This code prints 2's table upto 10")
while(myVar <= 10){println(myVar)
myVar += 2;
}
}
}

Output

输出量

This code prints 2's table upto 10
2
4
6
8
10

翻译自: https://www.includehelp.com/scala/loops-in-scala.aspx

scala中循环守卫

scala中循环守卫_Scala中的循环相关推荐

  1. scala 函数中嵌套函数_Scala中的VarArgs函数和@varargs批注

    scala 函数中嵌套函数 In this post, we are going to discuss about Functions with Var-Args (Variable Number O ...

  2. scala部分应用函数_Scala中的部分函数

    scala部分应用函数 Scala部分功能 (Scala partial functions) A partial function is a function that returns values ...

  3. scala 访问修饰符_Scala中的访问修饰符

    scala 访问修饰符 Access modifiers are used in order to restrict the usage of a member function to a class ...

  4. scala方法中的变量_Scala中的变量

    scala方法中的变量 Scala变量 (Scala variables) A variable is named a reference to a memory location. The loca ...

  5. scala 函数中嵌套函数_Scala中的嵌套函数 用法和示例

    scala 函数中嵌套函数 Scala中的嵌套函数 (Nested functions in Scala) A nested function is defined as a function whi ...

  6. for循环中抛出异常_不要抛出循环!

    到目前为止,在本系列文章中,我一直专注于Scala对Java生态系统的忠诚度,向您展示Scala如何整合Java的大部分核心对象功能. 但是,如果Scala只是编写对象的另一种方式,那么它就不会像它那 ...

  7. 汇编语言 循环让字符串中的字母变成大写

    这个仅仅实现了变化前面的4个字母. 但是关于出栈入栈的思想是很值得学习的! 代码部分: assume cs:codesg,ds:datasg,ss:stacksgstacksg segment dw ...

  8. python中while嵌套循环_python中的while循环

    循环 目标 程序的三大流程  while 循环基本使用  break 和 continue  while 循环嵌套 01. 程序的三大流程 在程序开发中,一共有三种流程方式: 顺序 -- 从上向下,顺 ...

  9. JAVA中循环删除list中元素的方法总结

    原文:http://www.cnblogs.com/aspirant/p/7900854.html 印象中循环删除list中的元素使用for循环的方式是有问题的,但是可以使用增强的for循环,然后今天 ...

最新文章

  1. Linux之 xstart调用 x11vnc远程图形化桌面
  2. 服务器性能优化的正确姿势
  3. WebService的两种方式SOAP和REST比较 (转)
  4. Spring AOP里面的几个名词的概念:
  5. 个人常用Sublime Text 插件
  6. how is SAP CDS view SADL load generated
  7. 2018百度之星程序设计大赛 - 资格赛 1002 子串查询
  8. Layui在表格中无法显示进度条(layui-progress)的值
  9. 医学遗传学词汇英语术语英文(Glossary) 5
  10. 计算机硬件驱动备份,驱动备份还原软件工具
  11. Python链家广州二手房的数据爬取--数据爬取
  12. php实现logo的上传,PHP实现图片的等比缩放和Logo水印功能示例
  13. how the sold to party and ship to party determined in IDOC#
  14. 张家界 凤凰 旅游有感
  15. QT设置窗体标题及背景颜色
  16. 移动云平台的基础架构之旅(一):云应用
  17. 2021年焊工(初级)考试内容及焊工(初级)最新解析
  18. 网页ssl证书风险怎么解决
  19. 【QA】VMware Workstation 不可恢复错误: (vmx)解决方案
  20. 如何做好网络推广浅析SEO优化网站文章需要注意哪些技巧?

热门文章

  1. php自然排序法的比较过程,PHP中strnatcmp()函数“自然排序算法”进行字符串比较用法分析(对比strcmp函数)...
  2. oracle 数据库回闪,各种数据库闪回的总结
  3. php的filter input,php中filter_input函数用法分析
  4. centos mysql 5.5 art_Linux CentOS6.5下编译安装MySQL 5.5.51''''
  5. g标签 怎么设置svg_SVG g元素
  6. linux访问网站出现443,Linux访问网站一直出现超时
  7. anaconda python3.8目录_MacBook Pro 安装anaconda、配置环境
  8. python井字棋ai_实现AI下井字棋的alpha-beta剪枝算法(python实现)
  9. android 左滑按钮,android开发类似微信列表向左滑动按钮操作
  10. JAVA知识基础(一):数据类型