Documente Academic
Documente Profesional
Documente Cultură
Unsur-unsur
Algoritma
Bentuk Program
A simple program takes the form:
[PROGRAM name ]
statements
END [PROGRAM name]
PROGRAM Prog1
PRINT *, "Welcome to FTN95" ! Output a simple message.
END PROGRAM Prog1
Hitungan /
Input Output
evaluasi formula
PROGRAM Prog3
REAL x
WRITE(*,"(a)",ADVANCE="NO") "Enter a real
number:"
READ *,x
PRINT *, "The result is:", x/3.0-0.9E6
Penjelasan
In Fortran it is possible to use a variable without explicitly
declaring its type.
Variables that are not declared are assumed to be either
INTEGER or REAL depending on the first letter in the
name.
However, the use of this implicit typing is not recommended.
Moreover, if a variable name is spelled incorrectly, the
implicit typing rule will cause the rogue variable to be
declared leading to erroneous results. In order to avoid this
happening, it is recommended that the implicit typing
facility be switched off.
To do this, include the statement IMPLICIT NONE before any
type declarations.
Note: FTN95 can be configured so that IMPLICIT NONE is
the default for all program units (see the compiler options
/CONFIG and /IMPLICIT_NONE).
Adam pamudji rahardjo
Algoritma
Flow Controls
1. IF construct atau skema perintah kondisional IF
2. Logical IF statement atau kondisional IF diikuti
perintah secara langsung
3. CASE construct atau skema perintah
kondisional CASE
4. DO construct atau skema perintah berulang DO
5. Iterated DO construct
6. GOTO statement
7. STOP statement
IF(logical_expression) THEN
statement_block 1
[ELSE
statement_block 2 ]
ENDIF
Logical IF statement
IF(logical_expression) simple_statement
makes x positive.
SELECT CASE(expression)
CASE(case_selector 1)
statement_block 1
CASE(case_selector 2)
statement_block 2
...
CASE DEFAULT
statement_block
END SELECT
CASE Construct
CASE constructs can be named.
The expression provides a value upon which the
selection is based and must be a scalar of type
INTEGER, LOGICAL or CHARACTER.
CASE DEFAULT is optional and is not necessarily
placed last. It provides for the situation where none
of the given CASEs applies.
The case_selector values must be of the same type
as expression. They contain a single constant value
or a list of constant values. Ranges may also be
used.
SELECT CASE(n)
CASE( : -1)
Sign = - 1 ! if n < 0
CASE(0)
Sign = 0 ! if n = 0
CASE( 1: )
Sign = 1 ! if n > 0
END SELECT
CASE Construct
DO contsruct
DO contsruct
Typically, within the block of statements, there will
be an IF statement of the form:
IF(logical_expression) EXIT
DO contsruct
DO constructs can be named
If a DO construct is named then the same name
must be appended to the corresponding END
DO and may optionally be appended to an
enclosing EXIT or CYCLE.
If DO constructs are nested, one within another,
then a name attached to EXIT or CYCLE
provides an extra degree of control.
DO contsruct
A DO WHILE construct provides an alternative
construction in the form:
DO WHILE(logical_expression)
statement_block
END DO
DO
IF(.NOT.logical_expression) EXIT
statement_block
END DO
Adam pamudji rahardjo
Algoritma
Iterated DO Construct
The iterated DO construct takes the form:
variable=start_value
DO WHILE(variable <= end_value)
statement_block
variable=variable+increment
END DO
Adam pamudji rahardjo
Algoritma
Iterated DO Construct
variable is an INTEGER variable, whilst start_value,
end_value, and increment are INTEGER
expressions.
increment is optional and when omitted defaults to 1
(unity). increment may be negative in which case
start_value>end_value.
For example:
DO I = 10,0,-2
! Do something for i=10,8,6,4,2 and 0
END DO
GOTO statement
Any statement can be given a label in the form of
an integer constant in the range 1 to 99999 at
the beginning of a line.
The form for the GOTO statement is:
GOTO label
GOTO statements should only be used in
exceptional circumstances. For example:
1. To exit from deeply nested flow constructs.
2. To jump to one of a number of terminating error
conditions at the end of a program or procedure.
Wherever possible, use an IF construct in
preference to GOTO statements.
STOP statement