My Drive

04. Loops 본문

programming

04. Loops

sunnyeo.park 2014. 10. 25. 02:07

1. do-loop ( increment : 옵션, default = 1 )

◦ example :

do i=1,10,2     begin, end, increment

write(*,*) i,i**2

enddo                or, end do

만약, begin이 end보다 크면, increment가 주어져야 함. 그렇지 않으면 실행 안됨

◦ example :

do i=10,1            not executed

write(*,*) i,i**2

enddo


do i=10,1,-1        ! executed

write(*,*) i,i**2

enddo


2. while-loop

◦ example :

x=.2

do while (x.lt..95)

x=3.8*x*(1.-x)

write(*,*) x

enddo


3. 무한 루프

◦ example :

do          ! "do forever". Exit required.

write(*,*) ’Enter a number’

read(*,*) x

if(x.lt.0.) exit

write(*,*) ’The square root of ’,x,’ is ’,sqrt(x)

enddo


4. 함축된 do-loop

◦ example :

write(*,*) (i,i**2,i=1,100)

아래와 동일한 결과

do i=1,10,2

write(*,*) i,i**2

enddo


i=1

do

if(i.gt.10) exit

write(*,*) i,i**2

i=i+2

enddo


5. exit : loop 종료(c의 break)

◦ example :

do i=1,327

if(a(i).gt.1.2345) exit

enddo


6. cycle : 새로운 loop 시작 (c의 continue)

◦ example :

outer: do i=1,5              all matrix rows

inner: do j=1,5        matrix columns, search loop:

    searches for first number > 0.8 in row i

if(a(i,j).gt.0.8) then

write(*,*) ’row’,i,’: column’,j,’:’,a(i,j)

cycle outer

endif

enddo inner

write(*,*) ’row ’,i,’: nothing found’

enddo outer






'programming' 카테고리의 다른 글

06. Input / Output  (0) 2014.10.27
05. Decisions  (0) 2014.10.25
03. Expressions  (0) 2014.10.25
02. Data types (자료형)  (0) 2014.10.24
01. Fortran Syntax  (0) 2014.10.24
Comments