Scala基础教程(四):if语句、循环语句、while语句
下面是一個典型的決策中IF...ELSE結構的一般形式使用在大多數的編程語言中:
if 語句:
if 語句包含一個布爾表達式后跟一個或多個語句。
語法:
一個 if 語句的語法:
if(Boolean_expression) { ?? // Statements will execute if the Boolean expression is true }如果布爾表達式的值為true,那么if語句里面的代碼模塊將被執行。如果不是這樣,第一組碼if語句結束后(右大括號后)將被執行。
示例:
object Test { ?? def main(args: Array[String]) { ????? var x = 10; ? ????? if( x < 20 ){ ???????? println("This is if statement"); ????? } ?? } }這將產生以下輸出結果:
C:/>scalac Test.scala C:/>scala Test This is if statement ? C:/>if...else語句:
if語句可以跟著一個可選的else語句,當 else 塊執行時,布爾表達式條件是假的。
語法:
if...else的語法是:
if(Boolean_expression){ ?? //Executes when the Boolean expression is true }else{ ?? //Executes when the Boolean expression is false }示例:
object Test { ?? def main(args: Array[String]) { ????? var x = 30; ? ????? if( x < 20 ){ ???????? println("This is if statement"); ????? }else{ ???????? println("This is else statement"); ????? } ?? } }這將產生以下結果:
C:/>scalac Test.scala C:/>scala Test This is else statement ? C:/>if...else if...else語句:
if語句可以跟著一個可選的else if ... else語句,這是非常有用的使用 if...else if如果測試各種條件聲明。
當使用 if , else if , else 語句有幾點要牢記。
·????????if可以有零或一個else,它必須跟在else if后面。
·????????一個if 可以有零到多個else if,并且它們必須在else之前。
·????????一旦一個?else if 匹配成功,剩余的else if或else不會被測試匹配。
語法:
if...else if...else的語法是:
if(Boolean_expression 1){ ?? //Executes when the Boolean expression 1 is true }else if(Boolean_expression 2){ ?? //Executes when the Boolean expression 2 is true }else if(Boolean_expression 3){ ?? //Executes when the Boolean expression 3 is true }else { ?? //Executes when the none of the above condition is true. }示例:
object Test { ?? def main(args: Array[String]) { ????? var x = 30; ? ????? if( x == 10 ){ ???????? println("Value of X is 10"); ????? }else if( x == 20 ){ ???????? println("Value of X is 20"); ????? }else if( x == 30 ){ ???????? println("Value of X is 30"); ????? }else{ ???? ????println("This is else statement"); ????? } ?? } }這將產生以下結果:
C:/>scalac Test.scala C:/>scala Test Value of X is 30 ? C:/>if ... else語句嵌套:
它始終是合法的嵌套 if-else 語句,這意味著可以使用一個 if 或 else if 在另一個if 或else if 語句中。
語法:
語法嵌套 if...else 如下:
if(Boolean_expression 1){ ?? //Executes when the Boolean expression 1 is true ?? if(Boolean_expression 2){ ????? //Executes when the Boolean expression 2 is true ?? } }可以嵌套else if...else在if語句中,反之也可以。
示例:
object Test { ?? def main(args: Array[String]) { ??????? var x = 30; ?????? ?var y = 10; ? ???????? if( x == 30 ){ ??????????? if( y == 10 ){ ??????????? println("X = 30 and Y = 10"); ???????? } ????? } ?? } }這將產生以下結果:
C:/>scalac Test.scala C:/>scala Test X = 30 and Y = 10 ? C:/>?
?
可能有一種情況,當需要多次執行代碼的幾個塊。在一般情況下,語句順序執行:在一個函數的第一條語句,首先執行,然后是第二個等等。
編程語言提供了各種控制結構,允許更多復雜的執行路徑。
循環語句可以執行語句多次或多組,下面是在大多數編程語言和循環語句一般如下:
Scala編程語言提供了以下循環類型的處理循環需求。點擊以下鏈接查看其詳細信息。
| 循環類型 | 描述 |
| while循環 | 重復聲明語句或一組,當給定的條件為真。它測試條件執行循環體前。 |
| do...while循環 | 像一個while語句,不同之處在于它測試條件在循環體的結尾 |
| for循環 | 執行語句多次序列并簡寫管理循環變量的代碼。 |
循環控制語句:
循環控制語句改變其正常的順序執行。當執行離開一個范圍,在該范圍內創建的所有對象自動被銷毀。但是Scala不支持break或continue語句,想要像Java,但從Scala2.8版本開始,有一種方法可以打退出循環。點擊以下鏈接查看詳細信息。
| 控制語句 | 描述 |
| break語句 | 終止循環語句并將執行立刻循環的下面語句。 |
無限循環:
一個循環變成無限循環,如果條件永遠不會為假。如果使用Scala,while循環是實現無限循環的最佳方式,如下:
object Test { ?? def main(args: Array[String]) { ????? var a = 10; ????? // An infinite loop. ????? while( true ){ ???????? println( "Value of a: " + a ); ????? } ?? } }如果將上面的代碼執行,它會在無限循環可以通過按Ctrl+ C鍵終止。
?
?
while 循環語句多次執行,只要給定的條件為真執行目標語句。
語法:
Scala while循環的語法是:
while(condition){ ?? statement(s); }在這里,聲明可以是單個語句或語句塊。所述條件可以是任何表達式,真值是任何非零值。當條件為true,則循環迭代。當條件為faklse,則程序控制進到緊接在循環之后的行。
流程圖:
在這里,while循環的關鍵點是循環可能不會永遠運行。當條件測試結果為false,循環體將跳過while循環后的第一個語句執行。
示例:
object Test { ?? def main(args: Array[String]) { ????? // Local variable declaration: ????? var a = 10; ? ????? // while loop execution ????? while( a < 20 ){ ???????? println( "Value of a: " + a ); ???????? a = a + 1; ????? } ?? } }當上述代碼被編譯和執行時,它產生了以下結果:
C:/>scalac Test.scala C:/>scala Test value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19 ? C:/>from: http://www.yiibai.com/scala/scala_basic_syntax.html總結
以上是生活随笔為你收集整理的Scala基础教程(四):if语句、循环语句、while语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Scala基础教程(三):访问修饰符、运
- 下一篇: Scala基础教程(五):函数、闭包