Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views22 pages

L2 Fortran

The document provides an overview of Fortran programming, covering its basic structure, including program elements, conditional statements, loops (DO and DO WHILE), and array operations. It also includes examples of using CYCLE and EXIT statements within loops, as well as tips for writing effective Fortran code. Additionally, it suggests reading materials for further learning about Fortran programming.

Uploaded by

som011001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views22 pages

L2 Fortran

The document provides an overview of Fortran programming, covering its basic structure, including program elements, conditional statements, loops (DO and DO WHILE), and array operations. It also includes examples of using CYCLE and EXIT statements within loops, as well as tips for writing effective Fortran code. Additionally, it suggests reading materials for further learning about Fortran programming.

Uploaded by

som011001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Topic: Fortran Programming

Objectives

∙ Basics of Fortran programming


∙ IF conditional statement
∙ DO loops
Topic: Fortran Programming

FORTRAN program has FOUR elements

Program name

Declaration and initialization of


variables

Main body of the program

Subprogram(s)

Structure of the FORTRAN program


Topic: Fortran Programming

IF conditional statement
Topic: Fortran Programming

Output
Sum 73.5999985
Topic: Fortran Programming

Output
Sum 73.5999985

Introducing the arrays


Syntax: array_name(length_array) (one-dimensional)
array_name(array_length, array_length) (two-dimensional)
Topic: Fortran Programming

How do you read this? 1D or 2D array?


Cartesian coordinates of 9 atoms (particles)
a(27) or a(9)
1.2361419 1.0137761 -0.0612424
0.5104418 0.8944555 0.5514190 …
1.9926927 1.1973129 0.4956931
-0.9957202 0.0160415 1.2422556 a(9,3) (1,1) (1,2) (1,3)
-1.4542703 -0.5669741 1.8472817
-0.9377950 -0.4817912 0.4267562 (2,1) (2,2) (2,3)
-0.2432343 -1.0198566 -1.1953808 (3,1) (3,2) (3,3)
0.4367536 -0.3759433 -0.9973297
-0.5031835 -0.8251492 -2.0957959 (4,1) (4,2) (4,3)

∙ Array operations: (5,1) (5,2) (5,3)

∙ a = a + 2.0 (6,1) (6,2) (6,3)


∙ a = a * 2.0 (7,1) (7,2) (7,3)
∙ a=a+a
(8,1) (8,2) (8,3)
∙ a=a*a
∙ a(1,:) = a(1,:) * 2 or a(1,:) = a(1,:) + 2 (9,1) (9,2) (9,3)
Topic: Fortran Programming

DO Loops

DO LOOP is used to repeat a block of statements.

Syntax

DO index_variable = start, end, step


---
---
END DO

•Index_variable must be 'integer' type


•'step' is optional
Topic: Fortran Programming

Example of DO Loops

do x = 1, 10
m=1
! block 1
n=10
enddo
x=1
do x= m, n*x
m=1
! block 1
n=10
enddo
do x = m, n
! block 1
enddo

m=1
n=10
do x = m, n, 1
! block 1
enddo
Topic: Fortran Programming

CYCLE and EXIT Statements

• EXIT statement helps in transferring the control outside the DO loop


• CYCLE statement takes the control to the beginning of the next
iteration in the DO loop

Syntax

DO index_variable = start, end, step


! block 1 statements
exit
---
! block 2 statements block 2 statements will not
END DO be executed

Usually both CYCLE and EXIT statements are used along with IF condition
Topic: Fortran Programming

CYCLE and EXIT Statements

• EXIT statement helps in transferring the control outside the DO loop


• CYCLE statement takes the control to the beginning of the next
iteration in the DO loop

do i = 1, 5
if (mod(i, 2) == 0) then
cycle ! Skip even numbers
end if
write (*,*) "Current value:", i
end do
Topic: Fortran Programming

CYCLE and EXIT Statements

• EXIT statement helps in transferring the control outside the DO loop


• CYCLE statement takes the control to the beginning of the next
iteration in the DO loop

do i = 1, 10
if (i > 5) then
exit ! Exit the loop when i exceeds 5
end if
write (*,*) "Current value:", i
end do
Topic: Fortran Programming

DO WHILE Loops

Syntax

DO WHILE (logical argument)

! block statements

END DO
Topic: Fortran Programming

Example of DO WHILE Loops

i=0
n=10
do while ( i < n)
! block 1
enddo

n=10
i=20
do while (.not. i < n)
! block 1
enddo
Topic: Fortran Programming

Infinite DO Loops

Syntax
do
DO count = count + 1
! block statements write (*,*) "Loop iteration:",
count
If (logical expression) then if (count >= 5) then
exit exit ! Exit the loop
endif end if
end do
! block statements

END DO
Topic: Fortran Programming

nested DO loops
Syntax

DO index_variable = start1, end1 do i = 1, 2


! block 1 statements do j = 1, 3
do k = 1, 4
DO index_variable = start2, end2 write (*,*) "i:", i,
! block 2 statements "j:", j, "k:", k
end do
DO index_variable = start3, end3 end do
! block 3 statements end do
END DO
! block 4 statements

END DO
! block 5 statements

END DO
Topic: Fortran Programming

Output
Sum 5050.00000
Topic: Fortran Programming

Output
sum = 0
do i = 1, 5
sum = sum + i
end do

write (*,*) "The SUM is:", sum


Topic: Fortran Programming

Output
do i = 1, 3
do j = 1, 2
write (*,*) i, " * ", j, " = ", (i * j)
end do
end do
Topic: Fortran Programming

Output

do i = 3, 2
write (*,*) i
end do
Topic: Fortran Programming

Write a program that computes the distance a ball travels when thrown at a certain
speed (v0) and angle (θ). Given the initial velocity input by the user, calculate the range
for angles ranging from 5 to 85 degrees in increments of 5 degrees.

Range = -(2*v02/g)cosθsinθ

Note that here θ is radians.


Topic: Fortran Programming

Tips
∙ Don’t worry about declaring variables initially. Identify the main part of the program
and start writing
∙ All real numbers should be in double precision (add d0 in the end), eg. 10.0d0
∙ Always use indentation, leave black spaces to improve readability
∙ Always use ‘parameter’ in case when assigning the values to integer datatype
∙ Use internal functions to convert datatypes, eg. real(x)
∙ Read compiler error messages more carefully
∙ For debugging, use ‘write’ statement at several places in the program and check for
the output

Topic: Fortran Programming

FORTRAN – Reading material


● Please go through this FORTRAN program for a quick overview,
● https://learnxinyminutes.com/docs/fortran95/
● Please go through this document for quick overview of FORTRAN
● https://www.ldeo.columbia.edu/~mspieg/mmm/Fortran.pdf
● Book: Computer Programming in Fortran 90 and 95, V. Rajaraman

You might also like