Chapter 5 迴圈

1 / 11

# Chapter 5 迴圈 - PowerPoint PPT Presentation

Chapter 5 迴圈. 迴圈每次的增量，若增量為 1 ，則可省略. 變數 I 的終止數值 N. I=1→2→3→…→N 迴圈共跑 N 次. 設定變數 I 的初值 1. 指定一個行代碼，兩行代碼間的程式會變成一個迴圈. 5-1 Do. 程式說明 do 10, I = 1, N, 1 … 10 continue. 迴圈每次的增量 2. 變數 I 的終止數值 N. I=2→4→6→…→N 迴圈共跑 N/2 次. 設定變數 I 的初值 2. 也可以用 do - end do 當作一個迴圈. 5-1 Do. 程式說明 do I = 2, N, 2 …

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Chapter 5 迴圈' - althea

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

I=1→2→3→…→N迴圈共跑N次

5-1 Do

do 10, I = 1, N, 1

10 continue

I=2→4→6→…→N迴圈共跑N/2次

5-1 Do

do I = 2, N, 2

end do

< Ex. 完整程式>

program ex0501

implicit none

integer :: I

integer, parameter :: N=10

do 10 I = 1, N, 1

write(*,*) 'Do - Loop Demo'

10 continue

stop

end program ex0501

< Ex. 執行結果>

Do - Loop DemoDo - Loop DemoDo - Loop DemoDo - Loop DemoDo - Loop DemoDo - Loop DemoDo - Loop DemoDo - Loop DemoDo - Loop DemoDo - Loop Demo

5-1 Do

Do使用時機

do while - end do之間為一個迴圈

5-2 Do while

do while (logical_expr)

end do

• do while (1)
• i=5do while(i>4) i=i+1end do
• i=4do while(i=4) i=i+1end do
.FALSE.

logical_expr判斷式

.TRUE.

statement 1statement 2…

5-2 Do while

< Ex. 完整程式>

program ex0502

implicit none

integer :: input = 0

end do

write(*,*) 'You are right!'

stop

end program ex0502

< Ex. 執行結果>

answer=10 <輸入 1 0 [ENTER] > answer=45 <輸入 4 5 [ENTER] > You are right!

5-2 Do while

Do while使用時機

< Ex. 完整程式>

program ex0503

implicit none

integer :: i

integer, parameter :: n=10

do i = 1, n

if (i == 4) cycle

write(*, '(I3)') i

end do

stop

end program ex0503

1235678910

< Ex. 執行結果>

5-3 Cycle

Cycle使用時機

< Ex. 完整程式>

program ex0504

implicit none

integer :: input

do while (.true.)

end do

write(*,*) "You are right!"

stop

end program ex0504

< Ex. 執行結果>

answer=10 <輸入 1 0 [ENTER] > answer=45 <輸入 4 5 [ENTER] > You are right!

5-4 Exit

Exit使用時機

< Ex. 完整程式>

program ex0506

implicit none

integer :: i, j

loop1: do i = 1,3

loop2: do j = 1,3

if(j == 2) cycleloop2

if(i == 2) exit loop1

write(*,'(t2,A1,I3,A1,I3,A1)') '(',i,',',j,')'

end do loop2

end do loop1

stop

end program ex0506

< Ex. 執行結果>

( 1, 1)( 1, 3)

5-5 具名的迴圈