150 likes | 352 Views
ICoCSIS. Fortran: Control Structures. Session Three. Outline. The if Construct and Statement The case Construct The do Construct The go to Construct and Statement. Introduction: Data Processing Flow Control. Sequential execution Passing control to another part of the program
E N D
ICoCSIS Fortran: Control Structures Session Three
Outline • The ifConstruct and Statement • The case Construct • The do Construct • The go to Construct and Statement
Introduction: Data ProcessingFlow Control • Sequential execution • Passing control to another part of the program • Repeating operations • Stopping execution LOGICAL CONTROL on DATA PROCESSING • Blocks of executing statements: block construct • Nested blocks
The ifConstruct and Statement (1) The if construct contains one or more sequences of statements (blocks), at most one of which is chosen for execution. General form: [name:] if (scalar-logical-expr) then block [else if (scalar-logical-expr) then [name] block]... [else [name] block] end if [name] Notes: • [] are used to represent optional items. • Naming is optional, but an else or else if statement may be named only if the corresponding if and end if statements are named, and must be given the same name. swap: if (x < y) then temp = x x = y y = temp end if swap if (x < y) then x = -x else y = -y end if
The ifConstruct and Statement (2) A nested if construct. if (i < 0) then if (j < 0) then x = 0.0 y = 0.0 else z = 0.0 end if else if (k < 0) then z = 1.0 else x = 1.0 y = 1.0 end if if (x-y > 0.0) x = 0.0 if (cond .or. p<q .and. r<=1.0) s(i,j) = t(j,i) An if construct of the form if (scalar-logical-expr) then action-stmt end if may be written in the form If (scalar-logical-expr) action-stmt
Example 4 Consider Example4.f Update your program that calculate the sum of all even numbers between 12 and 124 by • Fill an array a(1:60) with the above even numbers, by changing alternatively their signs (- and +); • After that, test each element of array a(1:60) starting from 1; if the element is negative - print a message to the screen; if it is positive – don’t; • After counting 10 negative numbers - leave the loop
The caseConstruct (1) Selecting one of several options, rather similar to that of the nested if construct: [name:] select case (expr) [case selector [name] block]... end select [name] Notes: • The leading and trailing statements must either both be unnamed or both bear the same name; a case statement within it may be named only if the leading statement is named and bears the same name. • The expression exprmust be scalar and of type character, logical, or integer, and the specified values in each selector must be of this type. • In the character case, the lengths are permitted to differ, but not the kinds. In the logical and integer cases, the kinds may differ. • Overlapping values are not permitted within one selector, nor between different ones in the same construct. select case (number) ! number is of type integer case (:-1) ! all values below 0 n_sign= -1 case (0) ! only 0 n_sign= 0 case (1:) ! all values above 0 n_sign= 1 end select
The case Construct (2) The general form of selector is a list of non-overlapping values and ranges, all of the same type as expr, enclosed in parentheses, such as case (1, 2, 7, 10:17, 23) The form case default is equivalent to a list of all the possible values of exprthat are not included in the other selectors of the construct. There may be only a single case default selector in a given case construct. At most one selector may be satisfied; if none is satisfied, control passes to the next executable statement following the end select statement.
The doConstruct (1) LOOPS - The ability to iterate. Instead of sum the elements of an array of length 10 by writing sum = a(1) sum = sum+a(2) : sum = sum+a(10) sum = 0.0 do i = 1,10 ! i is of type integer sum = sum+a(i) end do FORTRAN offers Execution of the statements between the do statement and the end do statement shall be repeated as many times as the index vary (index is i, and it varies 10 times). Each time, the statement will be executed with different value of the index. The index variable must not be explicitly modify within the do construct. General form of do construct has three parameters. The parameters could be scalars or expressions. If no third parameter is given it is assumed equal to 1. [name:] do [,] variable = expr1, expr2 [,expr3] block end do [name]
The do Construct (2) If a loop begins with the statement do i = 1, n then its body will not be executed at all if the value of n on entering the loop is zero or less. This is an example of the zero-trip loop. Unbounded loop [name:] do which specifies an endless loop.To exit from an endless loop (required) the exit statement is using exit [name] nameis optional and specify from which do construct the exit should be taken in the case of nested constructs. Control to be transferred to the next executable statement after the end do statement to which it refers. The exit statement is also useful in a bounded loop when all iterations are not always needed. cycle [name] Transfers control to the end do statement of the corresponding construct.
The do Construct (3) do i = 1, n : if (i==j) exit : end do l = i The value of a do construct index (if present) is incremented at the end of every loop iteration for use in the subsequent iteration. As the value of this index is available outside the loop after its execution, we have three possible situations: • If, at execution time, n has the value zero or less,iis set to 1 but the loop is not executed, and control passes to the statement following the end do statement. • If n has a value which is greater than or equal to j, an exit will be taken at the if statement, and l will acquire the last value of i, which is of course j. • If the value of n is greater than zero but less than j, the loop will be executed ntimes, with the successive values of i being 1,2, . . ., etc. up to n. When reaching the end of the loop for the nth time, i will be incremented a final time, acquiring the value n+1, which will then be assigned to l.
The do Construct (4) The do block is the sequence of statements between the do statement and the end do statement. From anywhere outside a do block, it is prohibited to jump into the block or to its end do statement. It is similarly illegal for the block of a do construct (or any other construct, such as an ifor caseconstruct), to be only partially contained in a block of another construct. The construct must be completely contained in the block. Legal statements: if (scalar-logical-expr) then do i = 1, n : end do else : end if do i = 1, n if (scalar-logical-expr) then : end if end do Any number of do constructs may be nested. do i = 1, n do j = 1, m a(i,j) = 0.0 do l = 1, k a(i,j) = a(i,j)+b(i,l)*c(l,j) end do end do end do
The do Construct (5) It should be noted that many short do-loops can be expressed alternatively in the form of array expressions and assignments. However, this is not always possible, and a particular danger to watch for is where one iteration of the loop depends upon a previous one. Thus, the loop do i = 2, n a(i) = a(i-1) + b(i) end do cannot be replaced by the statement a(2:n) = a(1:n-1) + b(2:n) ! Beware
The go to statement The form of the unconditional go to statement is go to label where label is a statement label. This statement label must be present on an executable statement (a statement which can be executed, as opposed to one of an informative nature, like a declaration). x = y + 3.0 go to 4 3 x = x + 2.0 4 z = x + y Notes: • If the statement following an unconditional go to is unlabeled – it can never be reached and executed, creating dead code, normally a sign of incorrect coding. • The statements within a block of a construct may be labeled, but the labels must never be referenced in such a fashion as to pass control into the range of a block from outside it. It is permitted to pass control from a statement in a construct to the terminal statement of the construct, or to a statement outside its construct.
Write your own program 1. Enter an integer number x. 2. Write case statement to do: • if mod_x =mod2(x) display the message “x is even” • if mod_x =mod3(x) display the message “x is divided by 3” • if mod_x =mod5(x) display the message “x is divided by 5” • if mod_x =mod7(x) display the message “x is divided by 7” • If neither of the above cases takes place display “none of the above” • Repeat steps 1 and 2 until you enter 0. Note: for calculating mod use the properties of integer division.