变量#
变量存储程序可以操作的信息。Fortran 是一种强类型语言,这意味着每个变量都必须具有一个类型。
Fortran 中有 5 种内置数据类型
integer
– 用于表示整数数据,正数或负数real
– 用于浮点数数据(非整数)complex
– 由实部和虚部组成的一对character
– 用于文本数据logical
– 用于表示布尔值(真或假)的数据
在我们可以使用变量之前,我们必须声明它;这告诉编译器变量类型和任何其他变量属性。
Fortran 是一种静态类型语言,这意味着每个变量的类型在程序编译时就已确定——变量类型在程序运行时不能更改。
声明变量#
声明变量的语法为
<variable_type> :: <variable_name>
其中<variable_type>
是上面列出的内置变量类型之一,而<variable_name>
是您想为变量指定的名称。
变量名必须以字母开头,并且可以包含字母、数字和下划线。在下面的示例中,我们为每种内置类型声明了一个变量。
示例:变量声明
program variables
implicit none
integer :: amount
real :: pi
complex :: frequency
character :: initial
logical :: isOkay
end program variables
Fortran 代码不区分大小写;您不必担心变量名称的大小写,但保持一致是一个好习惯。
请注意程序开头处的附加语句:implicit none
。此语句告诉编译器所有变量都将被显式声明;如果没有此语句,变量将根据其开头的字母隐式类型化。
始终在每个程序和过程的开头使用
implicit none
语句。在现代编程中,隐式类型化被认为是不好的做法,因为它隐藏了信息,从而导致更多程序错误。
声明变量后,我们可以使用赋值运算符=
为其赋值和重新赋值。
示例:变量赋值
amount = 10
pi = 3.1415927
frequency = (1.0, -0.5)
initial = 'A'
isOkay = .false.
字符用单引号 ('
) 或双引号 ("
) 括起来。
逻辑或布尔值可以是.true.
或.false.
。
重要
注意声明时的赋值
integer :: amount = 1
这不是正常的初始化;它暗示了save
属性,这意味着变量在过程调用之间保留其值。好的做法是将变量的初始化与声明分开。
标准输入/输出#
在我们的Hello World示例中,我们将文本打印到命令窗口。这通常称为写入standard output
或stdout
。
我们可以使用前面介绍的print
语句将变量值打印到stdout
print *, 'The value of amount (integer) is: ', amount
print *, 'The value of pi (real) is: ', pi
print *, 'The value of frequency (complex) is: ', frequency
print *, 'The value of initial (character) is: ', initial
print *, 'The value of isOkay (logical) is: ', isOkay
类似地,我们可以使用read
语句从命令窗口读取值
program read_value
implicit none
integer :: age
print *, 'Please enter your age: '
read(*,*) age
print *, 'Your age is: ', age
end program read_value
此输入源通常称为standard input
或stdin
。
表达式#
可以使用通常的一组算术运算符,按优先级顺序排列
运算符 |
描述 |
---|---|
|
指数 |
|
乘法 |
|
除法 |
|
加法 |
|
减法 |
示例
program arithmetic
implicit none
real :: pi
real :: radius
real :: height
real :: area
real :: volume
pi = 3.1415927
print *, 'Enter cylinder base radius:'
read(*,*) radius
print *, 'Enter cylinder height:'
read(*,*) height
area = pi * radius**2
volume = area * height
print *, 'Cylinder radius is: ', radius
print *, 'Cylinder height is: ', height
print *, 'Cylinder base area is: ', area
print *, 'Cylinder volume is: ', volume
end program arithmetic
浮点精度#
可以使用kind
参数显式声明所需的浮点精度。iso_fortran_env
内在模块为常用的 32 位和 64 位浮点类型提供kind
参数。
示例:显式实数kind
program float
use, intrinsic :: iso_fortran_env, only: sp=>real32, dp=>real64
implicit none
real(sp) :: float32
real(dp) :: float64
float32 = 1.0_sp ! Explicit suffix for literal constants
float64 = 1.0_dp
end program float
始终为浮点文字常量使用
kind
后缀。
示例:C 可互操作kind
program float
use, intrinsic :: iso_c_binding, only: sp=>c_float, dp=>c_double
implicit none
real(sp) :: float32
real(dp) :: float64
end program float
在下一部分中,我们将学习如何使用数组在一个变量中存储多个值。
使用block
结构的局部作用域变量#
2008 年 Fortran 标准引入了block
的概念,它允许在程序或过程中使用局部作用域变量。
示例
module your_module
implicit none
integer :: n = 2
end module
program main
implicit none
real :: x
block
use your_module, only: n ! you can import modules within blocks
real :: y ! local scope variable
y = 2.0
x = y ** n
print *, y
end block
! print *, y ! this is not allowed as y only exists during the block's scope
print *, x ! prints 4.00000000
end program