Syntax error when using “X != 0” in Fortran

前端 未结 3 1425
无人共我
无人共我 2021-01-19 00:59

I have a problem with my Fortran program that does nothing more than calculating a prime factorization (or should do). That\'s the error:

C:\\MinGW\\Fortran>         


        
相关标签:
3条回答
  • 2021-01-19 01:39

    The token for "not equals" in fortran is /= . ! starts a comment, hence the compiler reading line 15 as

            if (prim(i) 
    

    and so is confused because there is no bracket to close the logical expression in the if statement. So simply replace != with /= and it should get rid of this problem.

    0 讨论(0)
  • 2021-01-19 01:46

    You're using the wrong notation for 'not equal to'. Fortran syntax is /= or .NE..

    So you should be using:

    if (prim(i) /= 0 .and. modulo(n, prim(i)) == 0) then
    

    and

    if (prim(i) /= 0) then
    

    Furthermore, your syntax of integer(sqrt(real(m))) is incorrect, perhaps you mean NINT(sqrt(real(m)))?

    0 讨论(0)
  • 2021-01-19 01:47

    Well, this is Fortran and ! denotes a comment. So the compiler actually sees

    if (prim(i) 
    

    which is no valid statement. The error message you see reflects that.

    "Not equal" in Fortran is /= or .ne.:

     if (prim(i) /= 0 .and. modulo(n, prim(i)) == 0) then
    

    and, later on:

    if (prim(i) /= 0) then
    
    0 讨论(0)
提交回复
热议问题