Fortran:if语句

声明:本博文翻译自:https://www.tutorialspoint.com/fortran/fortran_decisions.htm

fortran语言有下面几种if结构语句
if...then语句结构
if...then...else语句结构
if...else if...else语句结构
nested if语句结构
select case语句结构
nested select case语句结构
接下来详细讲解这六种语句
1. if...then
这是最简单的if语句结构。
if...then的语句结构如下:
if (logical expression) then      
   statement  
end if
还可以对if块进行命名:
[name:] if (logical expression) then      
   ! various statements           
   . . .  
end if [name]
这里方括号以及里面的内容都是可选(即可写可不写)
示例代码如下:
Program testif
  implicit none  
  integer :: a

  a = 10
  if ( a == 10 ) then
    write(*,*) a 
  end if

End program testif
还有署名if语句。可将上面的代码改写成如下代码:
Program testif
  implicit none  
  integer :: a

  a = 10
  if1: if ( a == 10 ) then
    write(*,*) a 
  end if if1

End program testif

2. if...then...else
if...then...else语句的基本语法如下:
if (logical expression) then      
   statement(s)  
else
   other_statement(s)
end if
类似地,此语句也可以写成署名结构:
[name:] if (logical expression) then      
   ! various statements           
   . . . 
   else
   !other statement(s)
   . . . 
end if [name]
示例代码如下:
Program testif
  implicit none  
  integer :: a

  a = 10
  if ( a == 10 ) then
    write(*,'(1x,a)') "a = 10"
  else
    write(*,'(1x,a)') "a /== 10" 
  end if

End program testif
署名if的示例代码如下:
Program testif
  implicit none  
  integer :: a

  a = 10
  if1: if ( a == 10 ) then
    write(*,'(1x,a)') "a = 10"
  else
    write(*,'(1x,a)') "a /== 10" 
  end if if1

End program testif

3. 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]
这里举一个例子。设变量grad为一个学生的成绩,现在要判断其优秀等级。
假设在90--100,其等级为I级;80--89为II级;60--79为III级;0--59为不及格。则示例代码为:
Program testif
  implicit none
  real :: grad = 76.5

  if ( grad>=90 .and. grad <=100 ) then  !.. 90--100
    write(*,'(1x,a)') "I level"
  else if ( grad >= 80 ) then  !.. 注意含义80--89
    write(*,'(1x,a)') "II level"
  else if ( grad >= 60 ) then  !.. 60--79
    write(*,'(1x,a)') "III level"
  else 
    write(*,'(1x,a)') "不及格"
  end if
End program testif
署名if与上述类似,这里不再累赘。

4. nested if
这种叫嵌套if语句:即if语句中含有一个if语句。代码如下:
Program testif
  implicit none
  integer :: a
  a = 68

  if ( a > 50 ) then
    if ( a < 100 ) then
      write(*,'(1x,a)') " 50 < a < 100"
    end if
  end if
End program testif

5. select case
select case基本语法如下所示:
[name:] select case (expression) 
   case (selector1)          
   ! some statements          
   ... case (selector2)           
   ! other statements           
   ...       
   case default          
   ! more statements          
   ...   
end select [name]
示例代码如下:
Program selectCaseProg
  implicit none
  character :: grade = 'B'

  select case (grade)

    case ('A') 
      write(*,'(1x,a)') "Excellent!" 

    case ('B') 
      write(*,'(1x,a)') "Well done" 

    case ('C')
      write(*,'(1x,a)') "You passed" 

    case ('D')
      write(*,'(1x,a)') "Better try again" 

    case default
      write(*,'(1x,a)') "Invalid grade" 

    end select

    write(*,'(1x,a,g0)') "Your grade is ", grade 

End program selectCaseProg
其中case语句中的表达式也可以是一个范围
program selectCaseProg
  implicit none
  integer :: marks = 78

  select case (marks)

    case (91:100) 
      write(*,'(1x,a)') "Excellent!" 

    case (81:90)
      write(*,'(1x,a)') "Very good!"

    case (71:80) 
      write(*,'(1x,a)') "Well done!" 

    case (61:70)
      write(*,'(1x,a)') "Not bad!" 

    case (41:60)
      write(*,'(1x,a)') "You passed!"  

    case (:40)
      write(*,'(1x,a)') "Better try again!"  

    case default
      write(*,'(1x,a)') "Invalid marks" 

    end select
    write(*,'(1x,a,g0)')  "Your marks is ", marks

end program selectCaseProg

6. nested select case
基本语法结构如下:
select case(a) 

   case (100) 
      write(*,'(1x,a)') "This is part of outer switch", a 

   select case(b) 
      case (200)
         write(*,'(1x,a)') "This is part of inner switch", a 

   end select

end select

示例代码如下:
Program nestedSelectCase
  implicit none
  integer :: a = 100
  integer :: b = 200

  select case(a) 
     case (100) 
        write(*,'(1x,a,g0)') "This is part of outer switch", a 

     select case(b) 
        case (200)
          write(*,'(1x,a,g0)') "This is part of inner switch", a 
     end select

  end select

  write(*,'(1x,a,g0)') "Exact value of a is : ", a 
  write(*,'(1x,a,g0)') "Exact value of b is : ", b 

End program nestedSelectCase
用ivf,执行结果如下:
This is part of outer switch 100
This is part of inner switch 100
Exact value of a is : 100
Exact value of b is : 200

你可能感兴趣的:(FortranNote,Fortran教程)