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定義了三種循環語句 。 他們是,
for Loop
循環
while Loop
while循環
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中循環守衛