×
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...else if...else 语句


Fortran  if...else if...else 语句

if语句构建体可具有一个或多个可选的 else-if 结构。当 if 条件不满足,则紧跟的 else-if 被执行。当 else-if 还是失败,其继续下一个 else-if 语句(如果有的话)被执行,依此类推。

可选的 else 被放置在末端,当上述条件不为真时则执行。

  • 所有 else 语句 (else-if 和 else)都是可选的。
  • else-if 可以使用一次或多次
  • else 必须始终被放置在构建体的末端,应该只出现一次。

语法:

if...else if...else 语句的语法是:

[name:] 
if (logical expression 1) then 
   ! block 1   
else if (logical expression 2) then       
   ! block 2   
else if (logical expression 3) then       
   ! block 3  
else       
   ! block 4   
end if [name]

示例

program ifElseIfElseProg
implicit none

   ! local variable declaration
   integer :: a = 100
 
   ! check the logical condition using if statement
   if( a == 10 ) then
  
      ! if condition is true then print the following 
      print*, "Value of a is 10" 
   
   else if( a == 20 ) then
  
      ! if else if condition is true 
      print*, "Value of a is 20" 
  
   else if( a == 30 ) then
   
      ! if else if condition is true  
      print*, "Value of a is 30" 
  
   else
   
      ! if none of the conditions is true 
      print*, "None of the values is matching" 
      
   end if
   
   print*, "exact value of a is ", a
 
end program ifElseIfElseProg

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

None of the values is matching
exact value of a is 100

分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)