scala几种循环判断语句_Scala循环控制语句– while,while和for循环

本文介绍了Scala编程语言中的三种循环控制语句:while循环、do while循环和for循环。详细阐述了每种循环的工作原理、语法结构,并通过实例演示了如何使用这些循环。此外,还提到了for循环与集合、过滤条件以及使用yield的情况。
摘要由CSDN通过智能技术生成

scala几种循环判断语句

In software programming, certain situations may arise where we need to execute a block of code several times. The loop statements helps in this repetitive tasks and are executed sequentially. Loop control statements include while, do while and for loop. Let’s look into how we use them in Scala programming language.

在软件编程中,某些情况下可能会出现,我们需要多次执行一段代码。 循环语句有助于完成此重复性任务,并按顺序执行。 循环控制语句包括whiledo whilefor循环。 让我们研究一下如何在Scala编程语言中使用它们。

Scala while循环 (Scala while loop)

A while loop statement is executed repeatedly until the condition is false.

重复执行while循环语句,直到条件为假。

The syntax for while loop is

while循环的语法是

while(condition) {
	statement
}

The condition can be an expression, boolean value or a non zero value. The statement can be a single statements or a group of statements. When the specified condition becomes false the control is transferred to the next statement outside the loop.

条件可以是表达式布尔值或非零值。 该语句可以是单个语句或一组语句。 当指定条件为假时,控制将转移到循环外的下一条语句。

Consider an example below.

考虑下面的示例。

object Stu{
 def main(args:Array[String])  {
 var sid = 5 ;
 while(sid < 15 ) {
 println("Student Id is:" +sid);
 sid = sid + 1;
 }
}
}

We are declaring and initializing a variable sid with an initial value of 5. In the while loop the condition if sid less than 15 is checked and the value of id is printed after which sid is incremented. The loop stops when sid is greater than 14

我们声明并初始化一个初始值为5的变量sid。在while循环中,检查sid小于15的条件,并打印id的值,然后递增sid。 sid大于14时循环停止

Run the above code by typing Stu.main(null) in scala shell and you will see below output.

通过在scala shell中键入Stu.main(null)运行以上代码,您将看到以下输出。

Student Id is:5
Student Id is:6
Student Id is:7
Student Id is:8
Student Id is:9
Student Id is:10
Student Id is:11
Student Id is:12
Student Id is:13
Student Id is:14

You can also save the above code in Stu.scala file and run as shown in below image.

您也可以将上面的代码保存在Stu.scala文件中,并如下图所示运行。

Scala做while循环 (Scala do while loop)

The do while loop statements executes at least once and then the while loop is executed till the condition is true.

do while循环语句至少执行一次,然后执行while循环直到条件为真。

The syntax for do-while loop is;

do-while循环的语法为;

do{
  
 statements;

}while( condition );

The statements enclosing the do loop is executed first and then the while condition is checked.

首先执行包含do循环的语句,然后检查while条件。

Consider an example below.

考虑下面的示例。

object Stud {
	def main(args:Array[String]) {
	var sid = 4;
	do {
    	println("Student Id is:"+sid);
    	sid = sid + 1
    	}while(sid < 10)
}
}

The value of student id is displayed and the value is incremented by 1 and then the condition in while loop is checked.

显示学生ID的值,并将其值加1,然后检查while循环中的条件。

Run the above code by typing Stud.main(null) and you will see output as in below image.

通过键入Stud.main(null)运行以上代码,您将看到如下图所示的输出。

Scala for循环 (Scala for loop)

For loop is a repetitive structure which allows us to execute a block of code multiple times similar to the other loops we saw.

For循环是一种重复结构,使我们可以多次执行代码块,这与我们看到的其他循环类似。

Since for loop is widely used, there are various forms of for loop in Scala.

由于for循环已被广泛使用,因此Scala中有多种形式的for循环。

For loop with ranges:

对于带范围的循环

The syntax of for loop is

for循环的语法是

for(variable <- Range){
   statements
}

Range represents numbers as x to y or x until y. The left arrow operator( object Student { def main(args:Array[String]) { var sid = 0 for (sid <- 6 to 12){ println(“Student Id is :”+sid) } } }

范围将数字表示为x到y或x直到y。 左箭头运算符( object Student { def main(args:Array[String]) { var sid = 0 for (sid <- 6 to 12){ println(“Student Id is :”+sid) } } }

In this example we are printing student id specifying the range as 6 to 12.

在此示例中,我们将打印学生ID,并将范围指定为6到12。

Run the above code as shown in below image with output.

如下图所示运行上面的代码,并显示输出。

Alternatively for loop range can also be written as x until y.

另外,for循环范围也可以写成x直到y

object Student {
def main(args:Array[String]) {
	var sid = 0
for (sid <- 9 until 14){
	println("Student Id is :"+sid)
}
}
}

Here we specify range as 9 until 14 so that the values from 9,10,11,12 and 13 will be displayed.

在这里,我们将范围指定为9到14,以便显示9,10,11,12和13中的值。

scala> object Student {
     | def main(args:Array[String]) {
     |  var sid = 0
     | for (sid <- 9 until 14){
     |  println("Student Id is :"+sid)
     | }
     | }
     | }
defined object Student

scala> Student.main(null)
Student Id is :9
Student Id is :10
Student Id is :11
Student Id is :12
Student Id is :13

scala>

Multiple ranges can be specified using semicolon (;) as the separator and the loop iterates for all possible combinations.

可以使用分号 (;)作为分隔符来指定多个范围,并且循环针对所有可能的组合进行迭代。

For example

例如

object Student {
   def main(args: Array[String]) {
      var id = 0;
      var  marks = 60;
     
      for( id <- 1 to 2; marks <- 70 to 72){
         println( "Student Id is : " + id );
         println( "Marks is : " + marks );
      }
   }
}

Here we are specifying ranges for id and marks together separated by a semicolon.

在这里,我们指定ID和标记的范围,两者之间用分号分隔。

Run the above code by as shown below.

如下运行上述代码。

scala> object Student {
     |    def main(args: Array[String]) {
     |       var id = 0;
     |       var  marks = 60;
     |      
     |       for( id <- 1 to 2; marks <- 70 to 72){
     |          println( "Student Id is : " + id );
     |          println( "Marks is : " + marks );
     |       }
     |    }
     | }
defined object Student

scala> Student.main(null)
Student Id is : 1
Marks is : 70
Student Id is : 1
Marks is : 71
Student Id is : 1
Marks is : 72
Student Id is : 2
Marks is : 70
Student Id is : 2
Marks is : 71
Student Id is : 2
Marks is : 72

For Loop with collections:

对于带有集合的循环

for loop can be used efficiently to iterate over collections. The syntax is

for循环可以有效地用于遍历集合。 语法是

for( var a <- List ){
   statement
}

List represents collection having list of all the elements to iterate using for loop.

List表示具有要循环使用的所有元素的列表的集合。

Consider an example below.

考虑下面的示例。

object Student {
def main(args:Array[String]) {
	var marks = 0;
	val Listmarks = List(60,65,70,75,80,85);
	for( m1 <-Listmarks) {
    	println("Marks :"+m1);
}
}
}

Below image shows the output produced by above example.

下图显示了上面示例产生的输出。

for loop with Filters

带过滤器的for循环

Some elements can be filtered in the for loop using if statements.

可以使用for语句在for循环中过滤某些元素。

The syntax is;

语法是;

for( var a <- List
      if condition1; if condition2...
   ){
   statement;
}

For example;

例如;

object Student {
 def main(args:Array[String]) {
 var id = 0;
 val Listid = List(4,5,6,7,8,9,10,11,12);
 for(id <- Listid
     if id > 6; if id !=11 ) {
     println("Id:"+id);
}
}
}

Here we are using filter conditions as id greater than 6 and id not equal to 11.

在这里,我们使用ID大于6且ID不等于11的过滤条件。

Run the above code by and you will see output as shown below.

运行上面的代码,您将看到如下所示的输出。

scala> Student.main(null)
Id:7
Id:8
Id:9
Id:10
Id:12

for loop with yield:

for循环with yield

The return values can be stored in a variable or can be returned through a function. To do this use the keyword yield as

返回值可以存储在变量中,也可以通过函数返回。 为此,请使用关键字yield作为

var result = for{ var i <- List
     if condition1; if condition2...
}yield i

For example;

例如;

object Student {
 def main(args:Array[String]) {
 var id =0;
 val Listid = List(4,5,6,7,8,10);
 var result = for{ id <- Listid
           if  id <9; if id !=7
 }yield id

for(id <- result) {
 println("Student Id:"+id);
}
}
}

The value of id is stored in the variable id using the keyword yield. Below image shows the output produced.

id的值使用关键字yield存储在变量id中。 下图显示了产生的输出。

That’s all for loop control statements in Scala programming, we will look into more Scala features in coming posts.

这就是Scala编程中循环控制语句的全部内容,我们将在以后的文章中探讨更多Scala功能。

翻译自: https://www.journaldev.com/7905/scala-loop-control-statements-while-do-while-for-loops

scala几种循环判断语句

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值