Sunteți pe pagina 1din 25

Algoritma

Unsur-unsur
Algoritma

Adam pamudji rahardjo


Algoritma

Bentuk Program
A simple program takes the form:

[PROGRAM name ]
statements
END [PROGRAM name]

Optional items are shown in square brackets.

Comments may be included as required, for example:

PROGRAM Prog1
PRINT *, "Welcome to FTN95" ! Output a simple message.
END PROGRAM Prog1

Adam pamudji rahardjo


Algoritma

Program dalam bahasa Fortran


Fortran is designed for formula translation.
Many elementary Fortran programs will include 3
(three) components:
1. The input of data which is stored as variables
2. The evaluation of formulae using the input
data
3. The output of the results

Hitungan /
Input Output
evaluasi formula

Adam pamudji rahardjo


Algoritma

Contoh Program dlm Fortran


Here is a simple example:

PROGRAM Prog2 input


INTEGER i,j
READ *, i
Evaluasi formula
j = 2*i+3
PRINT *, j
END PROGRAM Prog2 output

Adam pamudji rahardjo


Algoritma

Contoh Program dlm Fortran


The next example is similar but uses REAL numbers:

PROGRAM Prog3

REAL x
WRITE(*,"(a)",ADVANCE="NO") "Enter a real
number:"
READ *,x
PRINT *, "The result is:", x/3.0-0.9E6

END PROGRAM Prog3


Adam pamudji rahardjo
Algoritma

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

Pengaturan Langkah Perintah / Flow Control

• The statements within a Fortran program are


implemented in their natural order.
• Elementary programs of the kind discussed so far
(containing input, formula translation and output) do
not have the ability to apply conditions to the
process or to repeat sections of the code.
• For this we need mechanisms for controlling the
order and the conditions under which the statements
are executed.
• This section describes such flow control constructs
together with the IF statement, the first of which is
the IF construct.

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

Adam pamudji rahardjo


Algoritma
IF Construct
A simple form for the IF construct is:

IF(logical_expression) THEN
statement_block 1
[ELSE
statement_block 2 ]
ENDIF

The ELSE section is optional. If the logical


expression is true, the first block is executed,
otherwise the second.

Adam pamudji rahardjo


Algoritma
IF Construct
For example:

IF(x > 0.0) THEN


y=x
ELSE
y = 0.0
ENDIF

If the ELSE section consists of one IF construct,


then ELSE and IF can be reduced to ELSEIF
provided that one of the associated ENDIFs is
deleted.
Sometimes it is simpler to use a CASE construct.
Adam pamudji rahardjo
Algoritma

Logical IF statement

The general form for the IF statement is:

IF(logical_expression) simple_statement

For example the statement:

IF( x < 0.0) x = - x

makes x positive.

Adam pamudji rahardjo


Algoritma
CASE Construct
The CASE construct provides an alternative to an IF
construct containing multiple ELSEIFs. It takes the
general form:

SELECT CASE(expression)
CASE(case_selector 1)
statement_block 1
CASE(case_selector 2)
statement_block 2
...
CASE DEFAULT
statement_block
END SELECT

Adam pamudji rahardjo


Algoritma

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.

Adam pamudji rahardjo


Algoritma
CASE Construct
For example:
! Month and Days are of type INTEGER
! Month has been assigned a value.
SELECT CASE(Month)
CASE(4,6,9,11)
Days = 30
CASE(1,3,5,7:8,10,12)
Days = 31
CASE(2)
Days = 28
! Code to test for a leap year
CASE DEFAULT
! Code to flag an error
END SELECT
Adam pamudji rahardjo
Algoritma
CASE Construct
For example:
! Month and Days are of type INTEGER
! Month has been assigned a value.
SELECT CASE(Month)
CASE(4,6,9,11)
Days = 30
CASE(1,3,5,7:8,10,12)
Days = 31
CASE(2)
Days = 28
! Code to test for a leap year
CASE DEFAULT
! Code to flag an error
END SELECT
Adam pamudji rahardjo
Algoritma
CASE Construct
A range takes the form CASE(low: high) where low or
high (but not both) may be omitted. For example, if
high is omitted then the condition becomes
expression>low. For example:

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

Adam pamudji rahardjo


Algoritma

CASE Construct

Each case must be unique with no possible overlap.


In the case of a CHARACTER expression, it may
represent a single character or a character string.

Adam pamudji rahardjo


Algoritma

DO contsruct

There are two forms for the DO construct.


1. the simple DO construct
2. the iterated DO construct

A simple DO construct takes the form:


DO
statement_block
END DO
This causes the block of statements to be repeated.

Adam pamudji rahardjo


Algoritma

DO contsruct
Typically, within the block of statements, there will
be an IF statement of the form:

IF(logical_expression) EXIT

The EXIT statement causes control to pass to the


statement following END DO.
The CYCLE statement is an alternative to EXIT. It
causes control to pass to a point immediately
after the last statement of the block, allowing
repetition to continue.

Adam pamudji rahardjo


Algoritma

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.

Adam pamudji rahardjo


Algoritma

DO contsruct
A DO WHILE construct provides an alternative
construction in the form:

DO WHILE(logical_expression)
statement_block
END DO

This is equivalent to:

DO
IF(.NOT.logical_expression) EXIT
statement_block
END DO
Adam pamudji rahardjo
Algoritma

Iterated DO Construct
The iterated DO construct takes the form:

DO variable=start_value, end_value [, increment]


statement_block
END DO

This is equivalent to:

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

EXIT and CYCLE can be used with all forms of DO


Adam pamudji rahardjo
Algoritma

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.

Adam pamudji rahardjo


Algoritma

STOP statement

The form for the STOP statement is:


STOP [message]
message is optional and is a character string.
STOP causes a normal (successful) termination of the
program.
If present, the message is output to the standard
output device.
STOP does not flag an error condition to the operating
system.
The terminating END of a PROGRAM implies STOP.

Adam pamudji rahardjo

S-ar putea să vă placă și