×
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 if...then...else 结构


if… then 语句可以后跟一个可选的 else 语句, 它执行时,逻辑表达式为假。

语法

 if… then… else 的基本语法:

if (logical expression) then      
   statement(s)  
else
   other_statement(s)
end if

但是,如果给定 if 块一个名字,然后命名 if-else 语句的语法是,这样的:

[name:] if (logical expression) then      
   ! various statements           
   . . . 
   else
   !other statement(s)
   . . . 
end if [name]

如果逻辑表达式的计算结果为代码里面的 if ... then 语句会被执行,对 else 块中的代码,否则该块将被执行 else 块。

流程图

Flow Diagram1

示例

program ifElseProg
implicit none
   ! local variable declaration
   integer :: a = 100
 
   ! check the logical condition using if statement
   if (a < 20 ) then
   
   ! if condition is true then print the following 
   print*, "a is less than 20"
   else
   print*, "a is not less than 20"
   end if
       
   print*, "value of a is ", a
	
end program ifElseProg

当上述代码被编译和执行时,它产生了以下结果:

a is not less than 20
value of a is 100

分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)