icerbox 3540976213 by Unknown

icerbox 3540976213 by Unknown

Author:Unknown
Language: eng
Format: epub


118 3 The Maple Programming La — all quite legal, but not that attractive. This programming idiom was showing up so often in Maple pio- grams that another form of the for was introduced: for x in expression do statements using x od Repeat for a sequence of values taken from an expression Example 78 f or-in statement Here we compute the sum of squares of even list members using both kinds of for loops. > aList :- [1.2,3,4,5]: > s :- 0: > for i to nops(aList) do > if irem(op(i,aList),2)-0 then > s :« s + op(i,aList) > fi > od: > s; 20 > s :- 0: > for n in aList do > if irem(n,2)»0 then s :» s + n*2 fi > od: > s; 20 The for and while can be combined. For example, the folbwing loop will conclude with the value (or i being the first prime number larger than 1010. for i from 1CT10+1 by 2 while not isprime(i) do od; 3.3 Conditional execution with if-then-else-f i if condition then statement sequence 1 else statement sequence 2 fi; Select a sequence of statements to perform 55 Conditional execution with if-then-else-f i 119 If you wish for statement sequence 1 to be performed if condition is true, and statement sequence 2 if it is not, then the if statement will do the trick. Programming language etymologists will notice that Maple has a "do.. od" and "if.. f i" style of brack- bracketing delimiters instead of "begin, .end" or "if . .endif". This is an idea borrowed from the programming language Algol 68. The condition referred to above can be a combination of equalities, inequalities, variables, or functions thatevalb would evaluate to true or false, such as a+b<6 or divide (poly 1 ,poly2) and lcoef f (polyl)»l. Cases can be selected according to one of several conditions by if condition 1 then statement sequence 1 elif condition 2 then statement sequence 2 elif condition n then statement sequence n else default statement sequence fi; Select one sequence to perform from several Eiample 79 Using if to print out results of primality testing npnae(n) returns true if n is prime, false otherwise. Inspect the numbers of the form ,;..ii j Terminate the loop nith a colon instead of a xrmicolon to display only the results of print. isprimeC7); isprimeF); true false for i to 8 do prime_candidate :» 2"B*i+l) if isprime(prime_candidate) then print(prime.candidate, else print(prime.candidate, fi; is prime'); is not prime) od: 7, 31, 127 511, 2047, 8191 32767, is prime is prime , is prime is not prime is not prime , is prime is not prime 131071, is prime

120 3 The Maple Programming Laneu Example 80 if ... then... elif ... else ... f i Fill the upper triangle with the row indices, the lower triangle with the column indices, and the diagonal with l's. > A :• array( 1..4, 1..4 ): > > > > > > > > > for i



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.