Fortran程序是由程序單元,如一個(gè)主程序,模塊和外部子程序或程序的集合。
每個(gè)程序包括一個(gè)主程序和可以或可以不包含其它程序單元。主程序的語法如下:
program program_name implicit none ! type declaration statements ! executable statements end program program_name
讓我們來寫一個(gè)程序,相加了兩個(gè)數(shù)字,并打印出結(jié)果:
program addNumbers ! This simple program adds two numbers implicit none ! Type declarations real :: a, b, result ! Executable statements a = 12.0 b = 15.0 result = a + b print *, 'The total is ', result end program addNumbers
當(dāng)編譯并執(zhí)行上述程序,它會(huì)產(chǎn)生以下結(jié)果:
The total is 27.0000000
請注意:
所有Fortran程序start關(guān)鍵字程序和end關(guān)鍵字結(jié)束程序,然后是該程序的名稱。
隱無語句允許編譯器檢查所有的變量類型是正確聲明。必須始終使用無隱在每個(gè)程序的開始。
在Fortran語言注釋開始使用感嘆號(hào)(?。?,因?yàn)樵谶@之后的所有字符(除字符串)被編譯器忽略。
print*命令在屏幕上顯示數(shù)據(jù)。
代碼行縮進(jìn),是保持一個(gè)程序讀取一個(gè)很好的做法。
Fortran語言允許大寫和小寫字母。 Fortran語言是區(qū)分大小寫的,除了字符串常量。
Fortran語言的基本字符集包括:
令牌Tokens基本字符集中的字符。令牌可以是一個(gè)關(guān)鍵字,標(biāo)識(shí)符,常量,字符串文字或符號(hào)。
程序語句作出標(biāo)記。
一個(gè)標(biāo)識(shí)符是用于標(biāo)識(shí)一個(gè)變量,過程或任何其它用戶定義的項(xiàng)目的名稱。在Fortran語言中名稱必須遵循以下規(guī)則:
它不能超過31個(gè)字符長。
它必須由字母數(shù)字字符(字母的所有字母,以及數(shù)字0到9)和下劃線(_)。
名稱第一個(gè)字符必須是字母。
名稱是區(qū)分大小寫
關(guān)鍵字是特殊的詞語,這些是語言預(yù)留的。這些保留字不能用作標(biāo)識(shí)符或名稱。
下表列出了Fortran關(guān)鍵字:
非I/O相關(guān)關(guān)鍵字 | ||||
---|---|---|---|---|
allocatable | allocate | assign | assignment | block data |
call | case | character | common | complex |
contains | continue | cycle | data | deallocate |
default | do | double precision | else | else if |
elsewhere | end block data | end do | end function | end if |
end interface | end module | end program | end select | end subroutine |
end type | end where | entry | equivalence | exit |
external | function | go to | if | implicit |
in | inout | integer | intent | interface |
intrinsic | kind | len | logical | module |
namelist | nullify | only | operator | optional |
out | parameter | pause | pointer | private |
program | public | real | recursive | result |
return | save | select case | stop | subroutine |
target | then | type | type() | use |
Where | While | |||
I/O相關(guān)的關(guān)鍵字 | ||||
backspace | close | endfile | format | inquire |
open | read | rewind | Write |