定義作用(1)
語句的結合形成腳本的作用。
本章有以下內容:1、了解作用和流控制;2、分組;3、條件作用;4、環。
--------------------------------------------------------------------------------
1、了解作用和流控制
為了控制許多語句的執行順序,使用流控制(flow control)。流控制使你可以有條件地、重復地執行MEL腳本的一節。
使你可以產生一個純淨的和簡單的MEL腳本運行復雜的程序。流控制的最基本的成分是分組。
-------------------------------------------------------------------
2、分組
使用分組可以:
--用一條簡單的語句執行一組語句
--產生腳本的可調節性
--定義局部變量的可見性
典型地,使用分組在只允許執行一條語句的地方執行一組語句。為了不限制一組語句,使用花括號({ })。
例如, if 語句只允許執行一條語句,如果它的條件是真的話。在這條語句之後的任何語句將會被有條件地執行。
為了有條件地執行它們,需要對每一條語句加一個if 語句。這會是很沒有效率和使人厭煩。
int $mok = 241;
if ($mok % 3 == 0)
print("It's true!\n");
print("This is always executed\n");
if ($mok % 3 == 0)
print("It's really really true!\n");
把這個語句分成組只允許使用一條語句:
int $sok = 241;
if ($sok % 3 == 0)
{
print("It's true!\n");
print("It's really really true!\n");
}
一個組內的所有語句,即使只有一條,也必須用分號結束。在關閉了一個組之後不需要分號。 對圍繞程序體也需要分組:
proc delta()
{
print("Delta is active.\n");
print("Roger that.\n");
}
分組是很少使用的,雖然它對於避免名字沖突和重新覆蓋定位於組內的局部變量的存儲器是很有用的。
可見性
一個組內的局部變量用在該組執行之後。它在該組已經執行並使得這些變量在組外不可見時,
重新覆蓋了定位於這些局部變量之後的存儲器。一個組內的局部變量只存在於這個組裡。
int $real = 28;
{
float $ghost = 73.3;
print($real + "\n"); // real exists in grouping
print($ghost); // ghost exists in grouping
}
print($real); // real exists outside of grouping
print($ghost); // ERROR: ghost exists only in grouping
一個程序內的局部變量可認為是對該程序是局部的。因此,在一個程序內被聲明的局部變量將只在該程序內是可見的。
-------------------------------------------------------------------
3、條件作用
條件語句使你僅當該測試條件為真時執行一條語句或一組語句,這個測試條件必須在括號中,
結果值應當是一個整型、浮點或者是一個矢量。該測試條件值隨後被轉換為一個整型數表達真或假。
如果測試條件有一個int或float類型的值,僅當該值轉換成整型後不為0時,該測試條件為真。
if (0) print("true\n"); // False
if (1) print("true\n"); // True
if (-17.4) print("true\n"); // True
if (0.9) print("true\n"); // False
if (-0.7) print("true\n"); // False
如果測試條件有一個矢量類型的值,僅當該矢量的幅值轉換成整型時不為0時,該測試條件為真。
if (<<0.9, 0.3, 0.3>> ) print("true\n"); // False
if (<<.8, 0.3, 0.7>> ) print("true\n"); // True
if (<<0, 0, 0>> ) print("true\n"); // False
if (<<0, 4, -37>> ) print("true\n"); // True
if (<<0, 1, 0>> ) print("true\n"); // True
測試條件可以包括任何的變量和算符(甚至賦值算符),但是結果值應該是int、 float或者vector類型。
if
if 條件語句有以下格式:
if (test condition)
statement;
如果測試條件為真,語句被執行。
if (9836 % 27 > 13)
print("Wow, that's interesting!\n");
if (3743 % 17 <
{
print("These are...");
print(" useful results.\n");
}
else
else 語句僅與if 條件語句一起工作。它有以下格式:
if (test condition)
statement1;
else
statement2;
如果測試條件為真,執行語句1;否則執行語句2。語句1和語句2可以是任何的有效語句。
if (rand(10) < 5)
print("That was random.\n");
else
print("No it wasn't.\n");
if (rand(2))
print("It takes one to get here\n");
else
{
print("This is just...");
print(" less than one");
}
else if
else if 語句僅與if else 條件語句一起工作。它有以下格式:
if (test condition1)
statement1;
else if (test condition2)
statement2;
如果測試條件為真,執行語句1。否則,如果語句2為真,執行語句2;如果沒有真值,就都不執行。
語句1和語句2可以是任何的有效語句。
float $random = rand(2);
if ($random < 1)
print("That was random.\n");
else if ($random < 2)
print("Sure was.\n");
else
print("That's impossible...\n");
在上述的例子中,
--如果隨機變量的值小於1,只顯示 "That was random.\n" 。
--如果隨機變量大於等於1而小於2,只顯示 "Sure was.\n" 。
--如果隨機變量的值為2,只顯示"That's impossible...\n" 。
rand(2) 調用極少返回一個准確為2的值。
float $crow = rand(10);
if ($crow < 5)
print("The crow has landed.\n");
else if ($crow < 7)
{
print("The crow is between five...");
print(" and seven.\n");
}
switch
一個switch語句執行一些語句的片段,取決於控制值。該控制值可以是int、 float、string或者vector數據類型。
switch體被括在括號中與一個或更多的調用情況的語句塊相比較。
各個情況被case後跟一個相同類型的作為控制的值加以標號,然後是冒號。
該控制值決定了哪個執行case語句。如果一個case語句值等於控制值,該case 語句就被執行。
switch語句具有以下的總體形式:
switch (control value)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
case value3:
statement3;
break;
...
default:
statement4;
break;
}
在一個case 語句之後,可以有任何語句,包括流控制。為了防止MEL連續地執行下一個case語句,可以使用一個break語句。
在一個switch內,一個break語句使switch語句的其余部分被忽略。
例:break 語句
int $sway = rand(3);
switch ($sway)
{
case 0:
print("Case 0\n"); // Executed only if $sway = 0
break;
case 1:
print("Case 1\n"); // Executed only if $sway = 1
break;
case 2:
while (rand(10) < 7) // These statements
print("I say!\n"); // are executed only
print("Case 2\n"); // if $sway = 2
break;
}
提示:雖然在一個switch語句裡的最後的一個case語句不需要一個break語句,因為這是switch的結束,
但加上一個break仍然是個好想法。如果你想要再增加case 給該switch語句,break語句已經在這兒了。
如果你想要該switch繼續執行下一個case語句,可以不要break語句。但是,當以後修改這段程序代碼時有可能會出現問題。
int $argo = rand(2);
switch ($argo)
{
case 0:
print("Food\n"); // Executed if $argo is 0.
case 1:
print("Fight\n"); // Executed if $argo is 0 or 1.
break;
}
可以用多於一條case語句去執行同一個MEL語句塊。如,如果想要使"a"和"A"的控制值都執行一些語句,可以寫與以下例子相似的程序:
string $mt = rand(2) < 1 ? "a" : "b";
string $mp = rand(2) < 1 ? "A" : "B";
string $XXz = rand(2) < 1 ? $mt : $mp;
switch ($XXz)
{
case "a":
case "A":
print("Apple\n"); // Executed if "a" or "A"
break;
case "b":
case "B":
print("Banana\n"); // Executed if "b" or "B"
break;
}
在這個例子中,變量$mt可以是"a"或者"b",變量$mp可以是"A"或者"B"。另外,變量$XXz可以是可變量。這意味著$XXz和控制值可以是"a"、"b"、"A"或"B"。
要說明一個要執行的語句塊,即使沒有與控制值相匹配的case值,可以使用缺省標號。
該標號可以被定位在switch語句的任何地方,但是它總是出現在所有的case語句的後面。
如果沒有缺省標號和沒有與控制值相匹配的case值,則該switch語句什麼事也不做。
例子:缺省標號
vector $mgb = <<1, 1, 0>>;
switch ($mgb)
{
case <<0, 1, 1>>:
print("Who\n"); // Executed if $mgb is <<0, 1, 1>>
break;
case <<1, 0, 1>>:
print("What\n"); // Executed if $mgb is <<1, 0, 1>>
break;
default:
print("Why\n"); // Executed if $mgb is not
break; // <<0, 1, 1>> or <<1, 0, 1>>
}
在上述例子中,執行了缺省的case,並顯示"Why\n"。