if语句中的错误需要标量逻辑表达式 [英] error in if-statement, requires scalar logical expression

查看:977
本文介绍了if语句中的错误需要标量逻辑表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个子程序中,我尝试创建一个语句,但是只有当我直接输入一个数字时它才会起作用,只要我用变量替换数字,它就会给出错误:

  ||错误:IF子句需要标量LOGICAL表达式| 

在这个例子中,var是一个介于0和1之间的实数。

  if(var%type3 <0.5)then 
test = 1
end if

有人可能知道我们做错了什么。因为这个错误并没有给我们提供任何线索,哪些部分是错误的。 您试图使用DIMENSION(1 ,1)类型为REAL。

您可以添加(1,1)以访问DIMENSION(1,1)中包含的REAL。



使用:

  IF(var%type3(1,1)<0.5) THEN 
print *,'IT WORKS'
END IF

例子这个错误:

  MODULE vardef 
类型vartype
REAL :: type3(1,1)
END TYPE vartype
END MODULE vardef

程序测试
USE vardef

类型(vartype)var

var %type3(1,1)= 0

IF(var%type3 <0.5)THEN
print *,'IT WORKS'
END IF
RETURN

END PROGRAM测试


Within a subroutine i try to create a statement, however it will only work if i enter a number directly, as soon as i replace the number with a variable, it will give the error:

||Error: IF clause  requires a scalar LOGICAL expression|

In this example var is a real number between 0 and 1.

    if ( var%type3 < 0.5) then
            test = 1
    end if

Does someone maybe know what we are doing wrong. Because the error does not give us any clues which part of the statement is wrong.

解决方案

You try to use a DIMENSION(1, 1) type as a REAL.

You shoul add (1, 1) to access to yout REAL contained in the DIMENSION(1, 1)

Use :

  IF ( var%type3(1, 1) < 0.5 ) THEN
     print *, 'IT WORKS'
  END IF

Exemple to get this error :

MODULE vardef
  TYPE vartype
     REAL :: type3(1, 1)
  END TYPE vartype
END MODULE vardef

PROGRAM test
  USE vardef

  TYPE(vartype) var

  var%type3(1, 1) = 0

  IF ( var%type3 < 0.5 ) THEN
     print *, 'IT WORKS'
  END IF
  RETURN

END PROGRAM test

这篇关于if语句中的错误需要标量逻辑表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆