Introduction to FORTRAN A Brief Summary of GNU FORTRAN Ashik Iqubal - - PowerPoint PPT Presentation

introduction to fortran
SMART_READER_LITE
LIVE PREVIEW

Introduction to FORTRAN A Brief Summary of GNU FORTRAN Ashik Iqubal - - PowerPoint PPT Presentation

Introduction to FORTRAN A Brief Summary of GNU FORTRAN Ashik Iqubal Department of Physics Ramakrishna Mission Vivekananda University Belur Math, Howrah ashik.iqubal@gmail.com August 31, 2012 FORTRAN: Data Types INTEGER REAL COMPLEX


slide-1
SLIDE 1

Introduction to FORTRAN

A Brief Summary of GNU FORTRAN Ashik Iqubal

Department of Physics Ramakrishna Mission Vivekananda University Belur Math, Howrah ashik.iqubal@gmail.com

August 31, 2012

slide-2
SLIDE 2

FORTRAN: Data Types

INTEGER REAL COMPLEX CHARACTER LOGICAL

  • A. Iqubal

FORTRAN

slide-3
SLIDE 3

FORTRAN: Data Type Examples

Integer INTEGER :: variable1, variable2, .... Real REAL :: variable1, variable2, .... Complex COMPLEX :: variable1, variable2, .... Character CHARACTER(len=character length) :: variable1, variable2, .. Logical LOGICAL :: variable1, variable2, .... LOGICAL :: FLAG FLAG = .TRUE. or .FALSE Arrays REAL, DIMENSION(10) :: VAR

  • A. Iqubal

FORTRAN

slide-4
SLIDE 4

FORTRAN: Arithmetic Operators

+ Addition – Subtraction * Multiplication / Division ** Exponentiation

  • A. Iqubal

FORTRAN

slide-5
SLIDE 5

FORTRAN: Conditional IF Statement

Code IF (condition) THEN statements END IF statements are evaluated if condition is true

  • A. Iqubal

FORTRAN

slide-6
SLIDE 6

FORTRAN: Nested Conditional Statement

Code IF (condition1) THEN statements block 1 ELSE IF (condition2) THEN statements block 2 ..... ELSE statements END IF

  • A. Iqubal

FORTRAN

slide-7
SLIDE 7

FORTRAN: Named Block IF Conditional Statement

Code [label:] IF (condition1) THEN statements block 1 ELSE IF (condition2) THEN [label] statements block 2 ..... ELSE [label] statements END IF

  • A. Iqubal

FORTRAN

slide-8
SLIDE 8

FORTRAN: Relational Operators

< less than <= less than or equal to > greater than >= greater than or equal to == equal to / = not equal to

  • A. Iqubal

FORTRAN

slide-9
SLIDE 9

FORTRAN: Logical Operators

.AND. AND .OR. OR .EQV. Logical Equivalence .NEQV. Logical Non-Equivalence .NOT. NOT

  • A. Iqubal

FORTRAN

slide-10
SLIDE 10

FORTRAN: Order of Evaluation

1 All arithmetic operations are evaluated first from left to right 2 All relational operators are evaluated working from left to right 3 All .NOT. operators are evaluated 4 All .AND. operators are evaluated working from left to right 5 All .OR. operators are evaluated working from left to right 6 All .EQV. and .NEQV. operators are evaluated working from

left to right Parenthesis can be used to change the default order of evaluation

  • A. Iqubal

FORTRAN

slide-11
SLIDE 11

FORTRAN: DO Loops

Code DO statements IF (exit-condition) EXIT statements END DO (Repeatedly) executes statements between DO and END DO until exit-condition is true

  • A. Iqubal

FORTRAN

slide-12
SLIDE 12

FORTRAN: DO WHILE Loops

Code DO WHILE (condition) statements END DO If condition is true, repeatedly executes statements between DO and END DO

  • A. Iqubal

FORTRAN

slide-13
SLIDE 13

FORTRAN: Iterative Loops

Code DO index = istart, iend, increment statements END DO

1 index = istart 2 if index*increment < iend*increment , then it executes the

statements

3 index = index + increment 4 Repeat steps 2 - 3

  • A. Iqubal

FORTRAN

slide-14
SLIDE 14

FORTRAN: Named Loops

Code [label:] DO index = istart, iend, increment statements IF (cycle-condition) CYCLE [label] statements IF (exit-condition) EXIT [label] statements END DO

  • A. Iqubal

FORTRAN

slide-15
SLIDE 15

FORTRAN: Named Loops contd.

Code [label:] DO statements IF (cycle-condition) CYCLE [label] statements IF (exit-condition) EXIT [label] statements END DO

  • A. Iqubal

FORTRAN

slide-16
SLIDE 16

FORTRAN: CYCLE and EXIT Statements

EXIT statement exits loops block, jumping immediately to the next statement outside of the loop. CYCLE statement continues the loop after skipping the remaining statements in its current iteration. GOTO statement transfers control to another part of the program

  • A. Iqubal

FORTRAN

slide-17
SLIDE 17

FORTRAN: Function

Code FUNCTION function-name (input-variables) IMPLICIT NONE REAL/INTEGER, INTENT(IN) :: input-variables REAL/INTEGER, :: function-name statements function-name = expression END FUNCTION function-name

  • A. Iqubal

FORTRAN

slide-18
SLIDE 18

FORTRAN: Recursive Function

Code RECURSIVE FUNCTION function(input-var) RESULT(answer) IMPLICIT NONE REAL/INTEGER, INTENT(IN) :: input-var REAL/INTEGER :: answer statements answer = expression END FUNCTION function

  • A. Iqubal

FORTRAN

slide-19
SLIDE 19

FORTRAN: Subroutine

Code SUBROUTINE subroutine-name (input-variables, output-variables) IMPLICIT NONE REAL/INTEGER, INTENT(IN) :: input-variables REAL/INTEGER, INTENT(OUT) :: output-variables REAL/INTEGER, INTENT(INOUT) :: common input/output-variables statements END SUBROUTINE subroutine-name Using RETURN in the subroutine returns to the calling program Subroutines can be called anywhere in the program by using : Code CALL subroutine-name(input-variables, output-variables)

  • A. Iqubal

FORTRAN

slide-20
SLIDE 20

FORTRAN: Recursive Subroutine

If the subroutine is used recursively, then use Code RECURSIVE SUBROUTINE subroutine-name (variables) declarations and statements END SUBROUTINE subroutine-name

  • A. Iqubal

FORTRAN

slide-21
SLIDE 21

FORTRAN: Subroutine contd.

Subroutines/Functions are generally placed at the end of the program after using a CONTAINS statement Code main program ....... CONTAINS SUBROUTINE subroutine-name (variables) ........ END SUBROUTINE subroutine-name END

  • A. Iqubal

FORTRAN