For Loops & Conditions Revisited A conditional statement ‘if condition then commands fi’ executes commands whenever condition holds. For instance, consider the following example: > a:=1: if (a=1) then print(“a is 1”) fi; “a is 1” The string is printed when a has been assigned the value 1 but not when it has been assigned the value 0 (as can be seen when the first line is replaced by ‘a:=0:’ ) Note ● the new command encountered here, print: the print command is used to specifically ask Maple to print its argument on the screen, ● ‘a:=1’ assigns a value of 1 to a whereas ‘a=1’ merely tests the value of a to determine whether or not it is equal to 1. As seen previously, the isprime command returns true or false depending on whether or not its argument is prime. In the event of encountering a prime i, Maple can be asked to print ‘i is prime’ rather than just ‘true’ as follows: > i:=3; if (isprime(i)) then print(“i is prime”) fi; Now recall how a for loop can be used to execute a number of similar computations consecutively: > for i from 1 to 10 do isprime(i) od; A conditional statement can be used in conjunction with a for loop to execute commands in a particular way or under particular conditions. For instance, consider the following piece of code which uses a conditional statement within a for loop to print all the prime numbers from 1 to 10. > for i from 1 to 10 do if (isprime(i)) then print(i, “is prime”) fi; od; The conditional statement, if…then…fi, can be extended to perform different actions depending on whether the condition holds or fails using the form if…then…else…fi. For instance, >n:=3; m:=5; if (n>m) then print(“n is larger”) else print(“m is larger”) fi; Exercises: 1. Write a piece of code that prints all prime numbers less than 150 on the screen. 2. Pierre de Fermat (1601-1665) conjectured that all integers of the form 2^(2^n)+1, where n is a non-negative integer, are prime. Write a piece of code that calculates 2^(2^n)+1 for each n in {0,1,2,3,4,5,6,7,8,9,10}, but only prints the result on the screen if it is prime. Was Fermat correct? 3. As seen previously, the code m mod n returns the integer remainder of m divided by n. Use this command to find the integer remainders of (i) 3 divided by 2, (ii) 6 divided by 2, (iii) 10 divided by 4. 4. Write a for loop to find the integer remainder of each integer from 1 to 10 when divided by 2. 5. (i) (ii) (iii) (iv) (v) Can you see how you could use the mod command to determine whether or not a particular number, i say, is even? Assign i the values 1 and 2 in turn. In each case ask Maple to print “i is even” on the screen if i is even. Write a piece of code that prints all even integers from 1 to 10 on the screen. Write a piece of code that prints all odd integers from 1 to 10 on the screen. Write a piece of code that prints all integers from 1 to 40 that are divisible by 3 on the screen. 6. (i) (ii) Using an if…then…else…fi statement, write a piece of code that prints “i is prime” or “i is composite” as appropriate, for all positive integers i from 2 to 50. Using an if…then…else…fi statement, write a piece of code that prints “i is even” or “i is odd” as appropriate, for all positive integers i up to 50.