1 / 11

Chapter 5 迴圈

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 …

althea
Download Presentation

Chapter 5 迴圈

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5 迴圈

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

  3. 迴圈每次的增量2 變數I的終止數值N I=2→4→6→…→N迴圈共跑N/2次 設定變數I的初值2 也可以用do - end do當作一個迴圈 5-1 Do 程式說明 do I = 2, N, 2 … end do

  4. < 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使用時機 連續重複執行某一段程式碼

  5. 當判別式為真時,執行迴圈當判別式為假時,跳出迴圈當判別式為真時,執行迴圈當判別式為假時,跳出迴圈 do while - end do之間為一個迴圈 5-2 Do while 程式說明 do while (logical_expr) … end do

  6. 應改成i==4 5-2 Do while 永久迴圈 當判別式永遠成真時,會形成永久迴圈 • do while (1) • i=5do while(i>4) i=i+1end do • i=4do while(i=4) i=i+1end do

  7. .FALSE. logical_expr判斷式 .TRUE. statement 1statement 2… 5-2 Do while 程式流程圖

  8. < Ex. 完整程式> program ex0502 implicit none integer, parameter :: answer=45 integer :: input = 0 do while(input /= answer) write(*,*) 'answer =' read(*,*) input 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使用時機 不能事先預知 會執行幾次的迴圈時

  9. < 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 當i=4時,不會被印出來而繼續執行迴圈 1235678910 < Ex. 執行結果> 5-3 Cycle Cycle使用時機 略過迴圈中之後的敘述,直接 跳回迴圈的開頭來執行迴圈

  10. < Ex. 完整程式> program ex0504 implicit none integer, parameter :: answer=45 integer :: input do while (.true.) write(*,*) "answer =" read(*,*) input if (input == answer) exit end do write(*,*) "You are right!" stop end program ex0504 永久迴圈 < Ex. 執行結果> answer=10 <輸入 1 0 [ENTER] > answer=45 <輸入 4 5 [ENTER] > You are right! 等式input=answer成立則跳離此迴圈 5-4 Exit Exit使用時機 直接跳出一個在運作中的迴圈

  11. < 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 跳到loop2迴圈的起頭執行程式 跳出整個loop1迴圈(也會同時跳出loop2迴圈) < Ex. 執行結果> ( 1, 1)( 1, 3) 5-5 具名的迴圈

More Related