注意项

  • 避免滥用:Fortran 90引入了结构化编程的概念,切记不要滥用goto语句
  • 明确标签:在使用goto语句时,标签要明确
  • 避免跳转过多:过多的跳转会增加代码的复杂性和可读性
  • 避免跳转到循环内部:在循环内部使用goto语句可能会导致程序陷入无限循环或者跳出循环
  • 谨慎使用跳转到其他子程序:在跳转到其他子程序时,需要确保目标子程序的执行顺序和状态是正确的

goto is considered bad practice

Code Readability

        The use of goto makes the code less readable because it introduces jumping points or labels that disrupt the linear flow of code

Debugging Difficulties

        Tracing and debugging errors become more challenging since the execution flow can jump around unpredictably. This makes it harder to identify the source of bugs, leading to longer debugging sessions.

Program Structure and Maintainability

​​​​​​​        The use of goto ends to encourage spaghetti-like code(结构上完全不可理解的系统) with tangled control flow. This lack of structure makes the program difficult to maintain and modify over time. A well-organized codebase with clearly defined functions and control flow enhances readability and understandability.

goto使用实例

goto代替loop

program goto_loop
    implicit none
    integer :: i

    i = 1
10  if (i <= 10) then
        print *, i
        i = i + 1
        goto 10
        write(*,*) "this sentence just under goto"
    end if
    write(*,*) "this sentence just under end if"
    read(*,*)
end program goto_loop

Fortran 中的 goto 语句-LMLPHP

goto进入loop内部

program goto_example
    implicit none
  
    integer :: i

    i = 1

10  continue

    print *, "i =", i

    i = i + 1

    if (i <= 5) then
        goto 10
    end if

    print *, "Loop finished"
    read(*,*)
end program goto_example

goto在子程序

  • 注意:
    • fortran 90 不支持从子程序 goto 到主程序中去
program goto_example
    implicit none

    print *, "Starting program"

    call subprogram

    print *, "Exiting program"
    read(*,*)

end program goto_example
    
subroutine subprogram
        implicit none
        integer :: i

        print *, "Entering subprogram"

    10  do i = 1, 5
            print *, "Loop iteration:", i

            goto 20
        end do

    20  print *, "Reached label 20"

        print *, "Exiting subprogram"
end subroutine subprogram

goto优势

  • goto语句在下列情况中有显著的优势
    • 跳出多层循环:使用goto语句可以直接跳转到指定的标签处,从而避免了使用复杂的逻辑结构或者引入额外的变量来控制循环的退出。
    • 错误处理:使用goto语句可以直接跳转到错误处理的标签处,从而简化了错误处理的逻辑。

跳出多重循环

program example
    implicit none
    integer :: i, j, k

    do i = 1, 10
      do j = 1, 10
         do k = 1, 10
            if (k == 5) then
                goto 10
            endif
            print *, i, j, k
         enddo
       enddo
     enddo


10  print *, "Exited loop at k = 5"
    read(*,*)
end program example

错误处理

program error_handling
    implicit none
    
    integer :: num, result
    
    write(*, *) "input an integer here:"
    read(*, *) num
    
    if (num < 0) then
        goto 10
    end if
    
    result = num * num
    
    write(*, *) "the square of your input number:", result
    
    goto 20
    
10  write(*, *) "Negative number is not allowed!"
    
20  write(*,*)"program is over.press any key to exit!"
    read(*,*)
end program error_handling

Fortran 中的 goto 语句-LMLPHP

Fortran的发展历史

  • Fortran(Formula Translation)是一种于1957年由IBM公司开发出高级程序设计语言。它是世界上第一种广泛应用的高级程序设计语言
  • Fortran的第一个版本(Fortran I)于1957年发布,主要用于数值计算和科学研究。
  • Fortran I的语法相对简单,但它的出现极大地简化了程序编写过程,使得科学家和工程师能够更加高效地进行计算和模拟。
  • 1960年,Fortran II发布,引入了子程序和函数的概念
  • 1966年,Fortran IV发布,引入了更多的语言特性
  • 1977年,Fortran 77发布,引入了一些新的特性,如字符处理和动态内存分配,使得Fortran在处理更复杂的问题时更加方便和强大。Fortran 77也成为了许多科学计算软件的主要开发语言。
  • 1990年,Fortran 90发布,引入了许多新的特性,如动态内存分配、模块化编程和面向对象编程等,使得Fortran在软件开发中更加灵活和强大。
  • Fortran仍然是科学计算和工程领域中最常用的编程语言之一。它在气象学、地球物理学、核物理学等领域有着广泛的应用。

Fortran编写的核物理库

古早的代码山

  • 事实上,绝大多数的fortran90代码库全部装配的都是goto语句
    • 如果不能阅读goto语句的话,其实很难进一步操作和学习

一些参考书

  • 量子物理学中的常用算法与程序  井孝功 赵永芳 蒿凤有 哈尔滨工业大学出版社
07-06 08:14