Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unsur-unsur Algoritma

Similar presentations


Presentation on theme: "Unsur-unsur Algoritma"— Presentation transcript:

1 Unsur-unsur Algoritma

2 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

3 Program dalam bahasa Fortran
Fortran is designed for formula translation. Many elementary Fortran programs will include 3 (three) components: The input of data which is stored as variables The evaluation of formulae using the input data The output of the results Input Hitungan / evaluasi formula Output

4 Contoh Program dlm Fortran
Here is a simple example: PROGRAM Prog2 INTEGER i,j READ *, i j = 2*i+3 PRINT *, j END PROGRAM Prog2 input Evaluasi formula output

5 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/ E6 END PROGRAM Prog3

6 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).

7 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.

8 Flow Controls IF construct atau skema perintah kondisional IF
Logical IF statement atau kondisional IF diikuti perintah secara langsung CASE construct atau skema perintah kondisional CASE DO construct atau skema perintah berulang DO Iterated DO construct GOTO statement STOP statement

9 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.

10 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.

11 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.

12 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 CASE DEFAULT statement_block END SELECT

13 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.

14 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

15 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

16 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

17 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.

18 DO contsruct There are two forms for the DO construct.
the simple DO construct 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.

19 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.

20 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.

21 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

22 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

23 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

24 GOTO statement Any statement can be given a label in the form of an integer constant in the range 1 to 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.

25 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.


Download ppt "Unsur-unsur Algoritma"

Similar presentations


Ads by Google