×
Fortran教程Fortran语言环境设置Fortran基本语法Fortran数据类型Fortran变量Fortran常量Fortran运算符Fortran算术运算符Fortran关系运算符Fortran逻辑运算符Fortran运算符优先级Fortran选择决策Fortran if...then语句结构Fortran if...then...else 结构Fortran if...else if...else 语句Fortran嵌套if结构Fortran select case结构Fortran嵌套select case结构Fortran循环Fortran do循环结构Fortran do...while循环结构Fortran嵌套循环Fortran exit语句Fortran Cycle语句Fortran Stop语句Fortran数字Fortran字符Fortran字符串Fortran数组Fortran向量和矩阵乘法函数Fortran还原功能Fortran查询函数Fortran构造函数Fortran重塑函数Fortran操作函数Fortran位置函数Fortran动态数组Fortran导出数据类型Fortran指针Fortran基本输入输出Fortran文件输入输出Fortran过程Fortran模块Fortran内部函数Fortran数字精度Fortran编程风格Fortran调试程序

Fortran基本语法


Fortran程序是由程序单元,如一个主程序,模块和外部子程序或程序的集合。

每个程序包括一个主程序和可以或可以不包含其它程序单元。主程序的语法如下:

program program_name
implicit none      

! type declaration statements      
! executable statements  

end program program_name

一个简单的Fortran程序

让我们来写一个程序,相加了两个数字,并打印出结果:

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        

当编译并执行上述程序,它会产生以下结果:

The total is 27.0000000    

请注意:

  • 所有Fortran程序start关键字程序和end关键字结束程序,然后是该程序的名称。

  • 隐无语句允许编译器检查所有的变量类型是正确声明。必须始终使用无隐在每个程序的开始。

  • 在Fortran语言注释开始使用感叹号(!),因为在这之后的所有字符(除字符串)被编译器忽略。

  • print*命令在屏幕上显示数据。

  • 代码行缩进,是保持一个程序读取一个很好的做法。

  • Fortran语言允许大写和小写字母。 Fortran语言是区分大小写的,除了字符串常量。

基础知识

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

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

令牌Tokens基本字符集中的字符。令牌可以是一个关键字,标识符,常量,字符串文字或符号。

程序语句作出标记。

标识符

一个标识符是用于标识一个变量,过程或任何其它用户定义的项目的名称。在Fortran语言中名称必须遵循以下规则:

  • 它不能超过31个字符长。

  • 它必须由字母数字字符(字母的所有字母,以及数字0到9)和下划线(_)。

  • 名称第一个字符必须是字母。

  • 名称是区分大小写

关键字

关键字是特殊的词语,这些是语言预留的。这些保留字不能用作标识符或名称。

下表列出了Fortran关键字:

非I/O相关关键字
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相关的关键字
backspace close endfile format inquire
open print read rewind Write


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)