scala中循环守卫_Scala中的循环

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中循环守卫

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值