You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Fortran IF语句编译报错求助:排查IF子句语法错误

问题分析与解决

你的编译错误源于Fortran的if-then结构语法规范问题:当把整个if逻辑写在同一行时,then关键字之后的执行语句必须用分号与then分隔开,或者将执行语句单独换行。

修正后的代码(两种写法均可行)

写法1:用分号分隔同一行的语句

program hi
    
    implicit none
    
    integer :: s=2
    if (s == 2) then; print* 'hi'; end if

end program hi

写法2:拆分多行(更贴合Fortran常规代码风格)

program hi
    
    implicit none
    
    integer :: s=2
    if (s == 2) then
        print* 'hi'
    end if

end program hi

错误原因说明

Fortran编译器解析if (s == 2) then print* 'hi' end if时,会将then print*视为一个无法识别的语法单元,因此抛出语法错误。通过分号分隔或拆分换行,能让编译器正确区分then关键字与后续执行语句,从而解决问题。

内容的提问来源于stack exchange,提问作者Lorenzo Huang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 16:52:00