Notes for Exercise #6 for Computer Tools for Engineers KJC (10/24/99) ====================================================== Learning Objectives: 1) Use of simple IF, IF-THEN, and IF-THEN-ELSE - Comparing integer and real values - Working with logical variables 2) Simple use of counter, pre-test, and post-test loops 3) Introduction to functions ------------------------------------------------------------------------------- Concept #1: Simple IF statement for comparing integer values Example #1: (try this... what happens if 10.0 is entered?) C *** Example #1 INTEGER N, M PARAMETER (M = 10) READ *, N IF (N.EQ.M) PRINT *, 'ZAP' END Exercise #1: Change the example program to output ZAP if N is 10 or if N is greater than 100. Hint: magic numbers in the execution are bad. ------------------------------------------------------------------------------- Concept #2: Comparing real values (remember precision issue) Example #2: (try this... enter integer values, enter 10.0 and 10.0000001) C *** Example #2 REAL X, Y, EPS PARAMETER (EPS = 0.0001, Y = 10.0) READ *, X IF (ABS(X-Y).LT.EPS) PRINT *, 'ZAP' END Exercise #2: Change the example to output ZAP if X is 10 or if X is 123.112. ------------------------------------------------------------------------------- Concept #3: Working with logical values and IF-THEN-ELSE Example #3: (try this... inputs of (100, 10), (99, 9), and (99, 999)) C *** Example #3 LOGICAL FLAG1, FLAG2 INTEGER X, Y READ *, X, Y FLAG1 = X.GT.Y FLAG2 = X.NE.100 IF (FLAG1.AND.FLAG2) THEN PRINT *, '(1)' ELSE IF (FLAG2) THEN PRINT *, '(2)' ENDIF END Exercise #3: Write a program to input two values, SPEED and DIREC. Output "OK" if SPEED is less than 100 and if DIREC is less than 45. Output "Warning" if SPEED is more than 100 or if DIREC is greater than 45. Output "Watch Out" if SPEED is more than 100 and if DIREC is greater than 45. ------------------------------------------------------------------------------- Concept #4: Counter loop Example #4: C *** Example #4 INTEGER J DO 10 J = 1, 10, 2 PRINT *, J 10 CONTINUE END Exercise #4: Write a program to do a count-down, "10, 9, 8, ... 0, blastoff" ------------------------------------------------------------------------------- Concept #5: Pre-test loop C *** Example #5 INTEGER J J = 10 10 IF (J.GE.0) THEN PRINT *, J J = J - 1 GOTO 10 ENDIF END Exercise #5: Write a program that inputs a value, outputs it, and then repeats these steps until a negative value is entered. The input of a negative value terminates than program. ------------------------------------------------------------------------------- Concept #6: Post-test loop C *** Example #6 INTEGER J J = 10 10 CONTINUE PRINT *, J J = J - 1 IF (J.GE.0) GOTO 10 END Exercise #6: Write a program that inputs a value, outputs it, and then repeats these steps until a negative value is entered. The input of a negative value terminates than program. ------------------------------------------------------------------------------- Concept #7: Functions C *** Example #7 INTEGER J, DECR J = 10 10 CONTINUE PRINT *, J J = DECR(J) IF (J.GE.0) GOTO 10 END INTEGER FUNCTION DECR(J) DECR = J - 1 RETURN END Exercise #7: Write a program that inputs a value, calls a function that doubles the value, and then outputs the (returned) doubled value. ------------------------------------------------------------------------------- Check-out Exercise: Write a program that outputs the first 20 values of the following sequence: 0, 1, 3, 6, 10, 15, 21, 28, etc. Your program starts the output with 0 (i.e., it outputs that above values and then the next 12 values. ---