鍍金池/ 教程/ Java/ Fortran基本語法
Fortran還原功能
Fortran關(guān)系運(yùn)算符
Fortran運(yùn)算符優(yōu)先級(jí)
Fortran基本語法
Fortran文件輸入輸出
Fortran嵌套select case結(jié)構(gòu)
Fortran變量
Fortran Cycle語句
Fortran語言環(huán)境設(shè)置
Fortran數(shù)據(jù)類型
Fortran數(shù)組
Fortran字符
Fortran if...else if...else 語句
Fortran調(diào)試程序
Fortran編程風(fēng)格
Fortran if...then語句結(jié)構(gòu)
Fortran嵌套循環(huán)
Fortran常量
Fortran循環(huán)
Fortran導(dǎo)出數(shù)據(jù)類型
Fortran字符串
Fortran操作函數(shù)
Fortran do...while循環(huán)結(jié)構(gòu)
Fortran內(nèi)部函數(shù)
Fortran數(shù)字精度
Fortran選擇決策
Fortran重塑函數(shù)
Fortran運(yùn)算符
Fortran構(gòu)造函數(shù)
Fortran模塊
Fortran位置函數(shù)
Fortran數(shù)字
Fortran指針
Fortran算術(shù)運(yùn)算符
Fortran exit語句
Fortran動(dòng)態(tài)數(shù)組
Fortran嵌套if結(jié)構(gòu)
Fortran select case結(jié)構(gòu)
Fortran向量和矩陣乘法函數(shù)
Fortran邏輯運(yùn)算符
Fortran if...then...else 結(jié)構(gòu)
Fortran教程
Fortran過程
Fortran Stop語句
Fortran基本輸入輸出
Fortran do循環(huán)結(jié)構(gòu)
Fortran查詢函數(shù)

Fortran基本語法

Fortran程序是由程序單元,如一個(gè)主程序,模塊和外部子程序或程序的集合。

每個(gè)程序包括一個(gè)主程序和可以或可以不包含其它程序單元。主程序的語法如下:

program program_name
implicit none      

! type declaration statements      
! executable statements  

end program program_name

一個(gè)簡單的Fortran程序

讓我們來寫一個(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ū)分大小寫的,除了字符串常量。

基礎(chǔ)知識(shí)

Fortran語言的基本字符集包括:

  • 字符包括 A ... Z 和 a ... z
  • 數(shù)字 0 ... 9
  • 下劃線(_)字符
  • 特殊字符 = : + 空格- * / ( ) [ ] , . $ ' ! " % & ; < > ?

令牌Tokens基本字符集中的字符。令牌可以是一個(gè)關(guān)鍵字,標(biāo)識(shí)符,常量,字符串文字或符號(hào)。

程序語句作出標(biāo)記。

標(biāo)識(shí)符

一個(gè)標(biāo)識(shí)符是用于標(biāo)識(shí)一個(gè)變量,過程或任何其它用戶定義的項(xiàng)目的名稱。在Fortran語言中名稱必須遵循以下規(guī)則:

  • 它不能超過31個(gè)字符長。

  • 它必須由字母數(shù)字字符(字母的所有字母,以及數(shù)字0到9)和下劃線(_)。

  • 名稱第一個(gè)字符必須是字母。

  • 名稱是區(qū)分大小寫

關(guān)鍵字

關(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 print read rewind Write