目录

Fortran - Basic 语法

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程序都以关键字program开头,以关键字end program,后跟end program,名称。

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

  • Fortran中的注释以感叹号(!)开头,因为编译器会忽略此后的所有字符(字符串除外)。

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

  • 压缩代码行是保持程序可读性的好方法。

  • Fortran允许使用大写和小写字母。 除字符串文字外,Fortran不区分大小写。

Basics

Fortran的basic character set包含 -

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

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

程序语句由令牌组成。

识别 Identifier

标识符是用于标识变量,过程或任何其他用户定义项的名称。 Fortran中的名称必须遵循以下规则 -

  • 它不能超过31个字符。

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

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

  • 名称不区分大小写

关键字 (Keywords)

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

下表列出了Fortran关键字 -

非I/O关键字
allocatableallocateassignassignment 阻止数据
callcasecharactercommoncomplex
containscontinuecycledatadeallocate
defaultdo 双精度 else 否则如果
elsewhere 结束块数据 结束了 结束功能 万一
结束界面 结束模块 结束计划 结束选择 结束子程序
结束类型 结束在哪里 entryequivalenceexit
externalfunctionif implicit
ininoutintegerintentinterface
intrinsickindlenlogicalmodule
namelistnullifyonlyoperatoroptional
outparameterpausepointerprivate
programpublicrealrecursiveresult
returnsave 选择案例 stopsubroutine
targetthentypetype()use
WhereWhile
与I/O相关的关键字
backspacecloseendfileformatinquire
openprintreadrewindWrite
↑回到顶部↑
WIKI教程 @2018