Scala中的while循环

在Scala中的while循环 (while loop in Scala)

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

Scala中的while循环用于多次运行代码块。 执行次数由输入条件定义。 如果此条件为TRUE,则代码将运行,否则它将不运行。

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 entry condition that can be any variable or expression, the value evaluated in TRUE if it's positive and FALSE if it's zero.

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

This loop might not run even once in the life span of code. If the condition is initially FALSE. The flow will not go in to loop in this case.

即使在代码的生命周期中,此循环也可能不会运行一次。 如果条件最初为FALSE 。 在这种情况下,该流将不会进入循环。

The while loop is also called entry controlled loop because its condition is checked before the execution of the loop's code block.

while循环也称为条目控制循环,因为在执行循环的代码块之前先检查其条件。

Syntax of while loop:

while循环的语法:

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

Flow chart of while loop:

while循环流程图:

while loop in Scala

Example of while loop:

while循环示例:

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

Code explanation:

代码说明:

The above code is to explain the usage of while loop in Scala. In this code, we have used a variable named myVar that is used as a counter in while loop. For printing text, to the screen, we are using println method that moves the cursor to the next line after printing. We have used += assignment operator that we have learned previously. The code prints that table of 2 up to 10.

上面的代码是解释Scala中while循环的用法。 在这段代码中,我们使用了一个名为myVar的变量,该变量在while循环中用作计数器。 为了将文本打印到屏幕上,我们使用println方法,该方法在打印后将光标移动到下一行。 我们使用了先前学习的+ =赋值运算符。 该代码打印2到10的表。

From this section, I am going to give you assignments that you can complete and submit to know your progress.

在本节中,我将为您提供可以完成并提交的作业,以了解您的进度。

Assignment 1 (difficulty - beginner): Print all numbers from 100 - 399 that are divisible by 3. (use while loop and functions.)

作业1(难度-初学者):打印100至399的所有可被3整除的数字(使用while循环和函数)。

Assignment 2 (difficulty - intermediate): Print all number between 541 - 643 that have 3, 5 and 7 as a factor.

分配2(难度-中等):打印541-643之间的所有数字,其中3、5和7为因数。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值