下面是一個典型的決策中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