Fortran: Control Structures Session Three ICoCSIS.

15
Fortran: Control Structures Session Three ICoCSIS

description

Introduction: Data Processing Flow 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

Transcript of Fortran: Control Structures Session Three ICoCSIS.

Page 1: Fortran: Control Structures Session Three ICoCSIS.

Fortran: Control Structures

Session Three

ICoCSIS

Page 2: Fortran: Control Structures Session Three ICoCSIS.

Outline1. The if Construct and Statement2. The case Construct3. The do Construct4. The go to Construct and Statement

Page 3: Fortran: Control Structures Session Three ICoCSIS.

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

Page 4: Fortran: Control Structures Session Three ICoCSIS.

The if Construct 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:1. [] are used to represent optional items.2. 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) thentemp = xx = yy = temp

end if swap

if (x < y) thenx = -x

elsey = -y

end if

Page 5: Fortran: Control Structures Session Three ICoCSIS.

The if Construct and Statement (2)

An if construct of the formif (scalar-logical-expr) then

action-stmtend if

may be written in the formIf (scalar-logical-expr) action-stmt

if (x-y > 0.0) x = 0.0if (cond .or. p<q .and. r<=1.0) s(i,j) = t(j,i)

A nested if construct.if (i < 0) then if (j < 0) then x = 0.0 y = 0.0 else z = 0.0 end ifelse if (k < 0) then z = 1.0 else x = 1.0 y = 1.0end if

Page 6: Fortran: Control Structures Session Three ICoCSIS.

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

Page 7: Fortran: Control Structures Session Three ICoCSIS.

The case Construct (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:1. 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.2. The expression expr must be scalar and of type character, logical, or integer, and the specified values in each

selector must be of this type.3. In the character case, the lengths are permitted to differ, but not the kinds. In the logical and integer cases, the kinds

may differ.4. 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 = 0case (1:) ! all values above 0 n_sign = 1end select

Page 8: Fortran: Control Structures Session Three ICoCSIS.

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 ascase (1, 2, 7, 10:17, 23)The formcase defaultis equivalent to a list of all the possible values of expr that 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.

Page 9: Fortran: Control Structures Session Three ICoCSIS.

The do Construct (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)

FORTRAN offerssum = 0.0do i = 1,10 ! i is of type integer sum = sum+a(i)end do

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]

Page 10: Fortran: Control Structures Session Three ICoCSIS.

The do Construct (2)If a loop begins with the statement

do i = 1, nthen 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:] dowhich specifies an endless loop. To exit from an endless loop (required) the exit statement is using

exit [name]name is 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.

Page 11: Fortran: Control Structures Session Three ICoCSIS.

The do Construct (3)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, i is 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 n times, 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.

do i = 1, n:if (i==j) exit:end dol = i

Page 12: Fortran: Control Structures Session Three ICoCSIS.

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 if or case construct), 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 ifend 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 doend do

Page 13: Fortran: Control Structures Session Three ICoCSIS.

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, na(i) = a(i-1) + b(i)end do

cannot be replaced by the statementa(2:n) = a(1:n-1) + b(2:n) ! Beware

Page 14: Fortran: Control Structures Session Three ICoCSIS.

The go to statementThe form of the unconditional go to statement is

go to labelwhere 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 43 x = x + 2.04 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.

Page 15: Fortran: Control Structures Session Three ICoCSIS.

Write your own program

1. Enter an integer number x.2. Write case statement to do:

1. if mod_x =mod2(x) display the message “x is even”

2. if mod_x =mod3(x) display the message “x is divided by 3”

3. if mod_x =mod5(x) display the message “x is divided by 5”

4. if mod_x =mod7(x) display the message “x is divided by 7”

5. If neither of the above cases takes place display “none of the above”

3. Repeat steps 1 and 2 until you enter 0.

Note: for calculating mod use the properties of integer division.