0% found this document useful (0 votes)
7 views

Fortran Lesson4

Uploaded by

kumarroy07203010
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)
7 views

Fortran Lesson4

Uploaded by

kumarroy07203010
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/ 5

FORTRAN

Types of loop statements:


 Running index (For-loops)
 Condition-based (While-loops)

FOR-loops (FORTRAN: DO-loops):


DO variable = startValue, StopValue [, StepValue]

! loop statements

END DO

program odd_even

integer :: n

write (*,*) 'Input end limit:'

read (*,*) n

write (*,*) '1 to',n,'even numbers are:'

do i = 2, n, 2

write (*,*) i

end do

write (*,*) '1 to',n,'odd numbers are:'

do i = 1, n, 2

write (*,*) i

end do

end program odd_even


WHILE-loops (Fortran: DO-WHILE-loops):
DO WHILE (condition)

! loop statements

END DO

program sod

integer :: n

integer :: rem, sum=0

write (*,*) 'Input any integer number:'

read (*,*) n

do while(n .NE. 0)

rem=Mod(n,10)

sum=sum+rem

n=n/10

end do

write (*,*) 'Sum of Digits is:',sum

end program sod


EXIT: break out of loops:
DO X = 1, 100

....

IF (X == 44 ) THEN

EXIT ! Terminate loop when X == 44

END IF

END DO

program ex_exit

write (*,*) ':: Example of EXIT statement ::'

do i = 1, 10

if (i .EQ. 5) then

write (*,*) 'Exit from loop'

EXIT

end if

write (*,*) i

end do

end program ex_exit


CYCLE: skip a single loop cycle:
DO X = 1, 100

....

IF ( X == 44 ) THEN

CYCLE ! Skip when X == 44

END IF

END DO

program ex_cycle

write (*,*) ':: Example of CYCLE statement ::'

do i = 1, 10

if (i .EQ. 5) then

write (*,*) 'Continue the loop'

CYCLE

end if

write (*,*) i

end do

end program ex_cycle

You might also like