Presentation is loading. Please wait.

Presentation is loading. Please wait.

Y >_< I Y 一,一一,一 I Maple's Control Statements 教授:蔡桂宏 博士 學生:張杰楷 95503 統資軟體課程講義95503 統資軟體課程講義.

Similar presentations


Presentation on theme: "Y >_< I Y 一,一一,一 I Maple's Control Statements 教授:蔡桂宏 博士 學生:張杰楷 95503 統資軟體課程講義95503 統資軟體課程講義."— Presentation transcript:

1 Y >_< I Y 一,一一,一 I Maple's Control Statements 教授:蔡桂宏 博士 學生:張杰楷 95503 統資軟體課程講義95503 統資軟體課程講義

2 Y >_< I Y 一,一一,一 I 內容綱要 5.1 Introduction 5.2 Repetition statements 5.3 More loop examples 5.4 Conditional statements 5.5 Boolean expressions 5.6 For-loop like commands 5.7 Statements vs. expressions

3 Y >_< I Y 一,一一,一 I 內容綱要 5.8 Print levels, printlevel, and print commands 5.9 Procedures that return unevaluated or return NULL 5.10 Online help for control statements

4 Y >_< I Y 一,一一,一 I 5.1 Introduction 為了寫些有趣的程式,需要學習兩種新的 Maple commands : 1. Repetition statement 2. Conditional statement 合稱為 control statements.

5 Y >_< I Y 一,一一,一 I 5.2 Repetition statements For-loop 語法: for index_variable from initial_value to final_value by step_size do sequence_of_Maple_commands od 一些例子

6 Y >_< I Y 一,一一,一 I 5.2 Repetition statements 注意: 1. by step_size 可以省略,當 initial_value 小於 final_value 時,且省略 step_size ,則內定 step_size 值為 1 。 2. 當 index_variable 遞增 by step_size 到達一個大於 final_value 的值時,或 step_size 為負,則當 index_variable 遞減 by step_size 到達一個小於 final_value 的值時,則 for-loop 將會停止。 3. for-loop 不承認 index_variable 在之前所設定的值。 Ex Ex

7 Y >_< I Y 一,一一,一 I 5.2 Repetition statements 注意: 4. 當 for-loop 停止, index_variable 值都會大 ( 小 ) 於 final_value ,但是確切的大 ( 小 ) 多少是由 initial_value, final_value, 與 step_size 所決定。 Ex Ex 5. from initial_value 與 to final_value 也都可以省略,當 from initial_value 省略時,則內定 initial_value 值為 1 , Ex ;不過當你省略 to final_value ,此 for-loop 將不停Ex 止。

8 Y >_< I Y 一,一一,一 I 5.2 Repetition statements 產生 “Tables” : for-loop 常被使用產生 “Tables” of results : Ex :某些整數的 10 進位、 2 進位與 16 進位互換Ex :某些整數的 10 進位、 2 進位與 16 進位互換 Ex : Table of polynomials and their factorizationsEx : Table of polynomials and their factorizations 注意: 1. Print 的結尾Print 的結尾 2. Od 的結尾Od 的結尾

9 Y >_< I Y 一,一一,一 I 5.2 Repetition statements 對 for-loop 的結果命名: 我們可以利用 for-loop 快速的得到運算結果 Ex ,可是當我 們想給每次 for-loop 的結果命名,該如何做?可以利用前 幾章所提過的方法: Ex 1. Concatenated namesConcatenated names 2. Indexed namesIndexed names Ex :找出前十個質數Ex :找出前十個質數

10 Y >_< I Y 一,一一,一 I 5.2 Repetition statements For-in-loop 語法: for index_variable in data_structure do sequence_of_Maple_commands od 一些例子

11 Y >_< I Y 一,一一,一 I 5.2 Repetition statements While-loop 語法: while boolean_expression do sequence_of_Maple_commands od 只要 boolean_expression 為真, while-loop 會反覆執行。 Ex :判定 1~10 是否為質數Ex :判定 1~10 是否為質數 注意: 當我們不知迴圈確切要做幾次時,使用 while-loop 較佳。 Ex :找最接近 1000 的兩個質數Ex :找最接近 1000 的兩個質數

12 Y >_< I Y 一,一一,一 I 5.2 Repetition statements Nest for-loop : 利用 Ex 說明。 Ex :計算多少質數小於 2^i ,且 i 從 2 到 18 。Ex :計算多少質數小於 2^i ,且 i 從 2 到 18 。 A loop that combines : Ex :找出第一個有 1000 位數的階層Ex :找出第一個有 1000 位數的階層 Ex : For-in-while-loopEx : For-in-while-loop

13 Y >_< I Y 一,一一,一 I 5.3 More loop examples 5.3.1 Example 1: Riemann sums 5.3.2 Example 2: Pascal‘s triangle 5.3.3 Example 3: Periodic extensions 5.3.4 Example 4: Drawing graphs 5.3.5 Example 5: Butterfly curve 5.3.6 Example 6: Animations

14 Y >_< I Y 一,一一,一 I 5.4 Conditional statements If-then-else-fi 語法: if boolean_expression then sequence_of_Maple_commands else sequence_of_Maple_commands fi 可簡化為: if boolean_expression then sequence_of_Maple_statements fi

15 Y >_< I Y 一,一一,一 I 5.4 Conditional statements Ex : Ex :一個簡單例子Ex :一個簡單例子 Ex :隨機產生 0 或 1 ,如果是 0 則輸出 heads ,不是則輸出 tails.Ex :隨機產生 0 或 1 ,如果是 0 則輸出 heads ,不是則輸出 tails. If 與 for-in-loop 的結合: Conditional statements 單獨使用其作用相當有限,因此其 常與程序或 loop 結合,為其 body 的一部份。 Ex : What percentage of the random integers were 10'sEx : What percentage of the random integers were 10's

16 Y >_< I Y 一,一一,一 I 5.4 Conditional statements Conditional statements 與數學: Ex :Ex : Some exercises : Ex Ex Nest conditional statements : Ex :Ex : Ex : Other nested conditional statements and nested proceduresEx : Other nested conditional statements and nested procedures

17 Y >_< I Y 一,一一,一 I 5.4 Conditional statements If-then-elif-then-else-fi : if boolean_expression then sequence_of_Maple_commands elif boolean expression then sequence_of_Maple_commands else sequence_of_Maple_commands fi

18 Y >_< I Y 一,一一,一 I 5.5 Boolean expressions Boolean expression 常被使用於 while-loops 與 conditional statements 。 Ex Ex Boolean expressions are made up of the three logical operators (and, or, not), the relational operators (, >=, =, <>), and functions that return true or false (i.e., boolean functions, ex : isprime ). We call and and or binary boolean operators and we call not a unary boolean operator.

19 Y >_< I Y 一,一一,一 I 5.5 Boolean expressions and, or, not 真值表: 一些例子 XYX and YX or Y false truefalsetrue false true Xnot X falsetrue false

20 Y >_< I Y 一,一一,一 I 5.5 Boolean expressions The following is not a boolean expression in Maple, though it would be a true statement in a mathematics book. ExEx There is a whole algebra to boolean expressions that specifies the order of precedence for all of the boolean operations, associativity rules for each boolean operation, and algebraic identities for boolean expressions. ExEx

21 Y >_< I Y 一,一一,一 I 5.6 For-loop like commands The three commands seq, add, and mul act very much like for-loops. In a sense they are abbreviations of special purpose for-loops. Seq : The seq command is used to create expression sequences. ExEx

22 Y >_< I Y 一,一一,一 I 5.6 For-loop like commands Add : The add command is a direct analogue in Maple to sigma notation. An add command of the form add( f(n), n=a..b ) means exactly the same thing as and their for-loop equivalent is result := 0: for n from a to b do result := result + f(n) od: result; ExEx

23 Y >_< I Y 一,一一,一 I 5.6 For-loop like commands Mul : The mul command is a direct analogue in Maple to mathematical product notation. A mul command of the form mul( f(n), n=a..b ) means exactly the same thing as and their for-loop equivalent is result := 1: for n from a to b do result := result * f(n) od: result; ExEx

24 Y >_< I Y 一,一一,一 I 5.6 For-loop like commands Sum and product commands : They are two Maple commands that are related to the add and mul command but they are not abbreviations for for-loops. ExEx 注意: Here is one tricky difference between add, mul on the one hand and sum, product. The index variable in an add or a mul command is local variables. The index variable in the sum and product commands are global variables. ExEx

25 Y >_< I Y 一,一一,一 I 5.7 Statements vs. expressions In Maple, we cannot use statements where you are supposed to use expressions. The most common kinds of statements in Maple are repetition statements, conditional statements, assignment statements, and the restart statement. Almost everything else in Maple is an expression (and every expression is also a statement). Ex Ex Maple allowed a conditional statement after the arrow but not a repetition statement. ExEx

26 Y >_< I Y 一,一一,一 I 5.8 Print levels, printlevel, and print commands 當你將 for-loops 放在 if-statements 中,或將 if-statements 放在 for-loops 中,將不會產生你所預期的結果 Ex ,這是因 為 Maple 的 print levels 概念。系統預設為印出 the first print level 的結果,不過上述情況的 print levels 常常不是 1 , 因此可用下列兩種方式解決: Ex 1. Use the print command, which always prints no matter what print level it is at. ExEx 2. Change the value of the printlevel variable. ExEx 雖然此兩種方式產生的結果,看來是相同地,不過事實上 它們是有差異的,請看以下說明。

27 Y >_< I Y 一,一一,一 I 5.8 Print levels, printlevel, and print commands The print command returns a special result called NULL, which is actually a name for the empty expression sequence. The last result variable, %, has the property that it always ignores the result NULL. This is why the print command did not have any effect on the last result variable. ExEx So putting print commands in a procedure can change what the return value of the procedure is. On the other hand, changing the value of printlevel does not have any effect on the return value of a procedure. So there is a difference between using print commands in a procedure and changing printlevel. ExEx

28 Y >_< I Y 一,一一,一 I 5.9. Procedures that return unevaluated or return NULL We defined procedures that used if-then-else-fi statements to implement piecewise defined functions. But there were some subtle problems with those procedures that we left unresolved in that section. ExEx Here is another, slightly different example of a function returning unevaluated. ExEx Here is another example of a command that returns NULL. ExEx

29 Y >_< I Y 一,一一,一 I 5.10 Online help for control statements Repetition statements : ?for Conditional statements : ?if Boolean expressions : ?boolean Seq, Add, and Mul : ?seq, ?add Sum and Product commands : ?sum, ?product Printlevel variable : ?printlevel Print command : print NULL represents : ?NULL “Fail return" : ?RETURN, ?procname All of Maple's statements : ?statement

30 Y >_< I Y 一,一一,一 I ~The End~


Download ppt "Y >_< I Y 一,一一,一 I Maple's Control Statements 教授:蔡桂宏 博士 學生:張杰楷 95503 統資軟體課程講義95503 統資軟體課程講義."

Similar presentations


Ads by Google