IF( expression ) statement
Only if expression (a logical variable or expression) has the value .TRUE. is statement executed. For example:
IF( x<0.0 ) x=0.0
Here, if x is less than zero then it is given a new value, otherwise x retains it's previous value. The IF statement is analogous to phrases like `if it is raining, take an umbrella'.
The structure of an IF construct depends on the number of conditions to be checked, and has the following general form:
[name:] IF (expression1) THEN
block1
ELSEIF (expression2) THEN [name]
block2
...
[ELSE [name]
block]
ENDIF [name]
Where expression# is a logical variable or expression.
The construct is used when a number of statements depend on the same condition. For example, `if it rains then phone for a taxi and take an umbrella'. This time the `then' part is required. Notice that an END IF (or ENDIF) part is required to indicate the end of the selection process. If it is raining the block of actions are executed and control passes to the next statement after END IF, otherwise the block of actions are skipped and control passes to the next statement after END IF.
A more complex situation is when one wants to perform alternative actions depending on the condition. For instance, both previous examples do not tell us what to do when it is not raining. The rules above can now be rephrased as: if it rains then phone taxi and take umbrella else walk.
Notice the use of the else part. The action-block parts may contain a single or more actions. The else part covers every other eventuality: sunshine, snowing etc. The passing of control follows the same rules as mentioned above.
There are situations though that alternative actions have to be taken depending on the value the condition takes. For instance, one might want to perform different action if it rains or snows or the sun is out. For example, if it is raining then phone taxi, take umbrella, else if it is snowing then stay at home, else if the sun is out then go to park, else walk. Notice the use of the ELSEIF part. The ELSE part acts as a default again in order to cover other eventualities. The same rules concerning passing of control apply.
The form can be used in a number of ways. For instance, multiple ELSEIFs can appear and/or the ELSE branch can be omitted and/or more IF constructs might follow ELSEIF or ELSE.
IF constructs can be labelled. Naming constructs can be useful when one is nested inside another, this kind of labelling makes a program easier to understand, for example:
outer: IF( x,0.0 ) THEN
...
ELSE outer
inner: IF( y<0.0 ) THEN
...
ENDIF inner
ENDIF outer
[name:] SELECT CASE( expression )
CASE( value ) [name]
block
...
[CASE DEFAULT
block]
END SELECT [name]
The result of expression may be of type character, logical or integer; value must be of the same type as the result of expression and can be any combination of:
For example:
INTEGER :: month
season: SELECT CASE( month )
CASE(4,5)
WRITE(*,*) `Spring'
CASE(6,7)
WRITE(*,*) `Summer'
CASE(8:10)
WRITE(*,*) `Autumn'
CASE(11,1:3,12)
WRITE(*,*) `Winter'
CASE DEFAULT
WRITE(*,*) `not a month'
END SELCET season
The above example prints a season associated with a given month. If the value of the integer month is not in the range 1-12 the default case applies and the error message `not a month' is printed, otherwise one of the CASE statements applies. Notice that there is no preferred order of values in a CASE statement.
GOTO label
The GOTO statement simply transfers control to the statement with the corresponding label. For example:
...
IF( x<10 ) GOTO 10
...
10 STOP
The GOTO statement should be avoided where ever possible, programs containing such statements are notoriously hard to follow and maintain.
[name:] DO [control clause]
block
END DO [name]
The DO construct may take two forms:
[name:] DO count = start, stop [,step]
block
END DO [name]
The control clause is made up of the following:
If stop is smaller than start and step is positive then count will take the value zero and the statement(s) will not be executed at all. The value of count is not allowed to change within the loop.
For example:
all: DO i=1,10
WRITE(6,*) i !write numbers 1 to 10
END DO all
even: DO j=10,2,-2
WRITE(6,*) j !write even numbers 10,8,6,4,2
END DO even
In the absence of a control clause the block of statements is repeated indefinitely.
[name:] DO
block
END DO [name]
The block of statements will be repeated forever, or at least until somebody stops the program. In order to terminate this type of loop the programmer must explicitly transfer control to a statement outside the loop.
The CYCLE statement is transferring control back to the beginning of the loop to allow the next iteration of the loop to begin.
Confusion can arise from multiple and nested (i.e. one inside another) DO loops, EXIT and CYCLE statements hence naming loops is highly recommended. As an example consider the following program:
PROGRAM averscore
REAL :: mark, average
INTEGER:: stid, loop
mainloop: DO
WRITE(*,*) 'Please give student id'
READ(*,*) stid
IF (stid==0) EXIT mainloop
average=0
innerloop: DO loop=1,3
WRITE(*,*) 'Please enter mark'
READ(*,*) mark
IF (mark==0) CYCLE innerloop
negs: IF (mark<0) THEN
WRITE(*,*) 'Wrong mark. Start again'
CYCLE mainloop
END IF negs
average=(average+mark)
END DO innerloop
average=(average)/5
WRITE(*,*) 'Average of student',stid,' is = ',average
END DO mainloop
END PROGRAM averscore
This program calculates the average mark of student given a series of 3 marks. It terminates when the user enters zero as the student id. In the case of a negative mark being entered the user has to re-enter all marks of that particular student (not only the wrong one!). In case of a zero mark the program asks for the next mark and saves adding a zero to the average total.
Notice that the labelling of DO and IF statements make the program not only easier to read and understand but more importantly able to perform the desired actions. Using EXIT or CYCLE without labels it would had made it difficult to comprehend which loop is referred to. Consider the case when the statement Cycle MainLoop was stripped from its label. The program thinks we refer to the InnerLoop and for every negative number we enter we miss a valid mark. Entering the following marks: 2 2 2 2 -4 results in an average of 1.6. The last value (-4) forces the program to go to the closest DO loop and to increase the counter. The counter then becomes five and the loop exits. So the logic of the program has been altered. If labels are not used then Exit will transfer control to the first statement after the END DO associated with the closest to Exit DO.
Similarly, Cycle will transfer control to the closest DO loop.This is a possible execution of the program:
Please give student id 1 ! This is a valid id
Please enter mark 40 ! valid
Please enter mark 53 ! valid
Please enter mark 65 ! valid
Average mark of student 1 is = 0000
Control returns to mainloop
Please give student id 2
Please enter mark 45
Please enter mark 45
Please enter mark 0 !ignored and control to InnerLoop
Average mark of student 2 is = 30.00000
Control returns to mainloop
Please give student id 3
Please enter mark 40
Please enter mark -1 ! Control to MainLoop
Wrong mark. Start again
Please give student id 3
Please enter mark 40
Please enter mark 85
Please enter mark 86
Please enter mark 87
Please enter mark 88
Average mark of student 3 is = 52.60000
Control returns to mainloop
Please give student id 0
Control to 1st statement after END DO mainloop
(a) DO loop=5, 3, 1
(b) DO loop=-6, 0
(c) DO loop=-6, 0, -1
(d) DO loop=-6, 0, 1
(e) DO loop=6, 0, 1
(f) DO loop=6, 0, -1
(g) DO loop=-10, -5, -3
(h) DO loop=-10, -5, 3
Where n is the size of the paper 0 to 6, and one inch=2.54cm.
where T is the temperature in centigrade, and t is the time in seconds. Write a program which prints the temperature of such a reaction at 1 minute intervals, The initial temperature is supplied by the user and the above equations should be re-calculated once every second. The program should terminate when the temperature reaches twice the initial temperature.