您的位置:首页 > 编程语言 > ASP

ASP VBscript使用逻辑运算中的 and or

2016-03-28 13:32 387 查看
if session("postTime")<>"" and minute(NOW() - session("postTime")) < 1 then
session("postTime")=NOW()
response.write("postisfast")
response.end

end if
如果 if 前面有 session("postTime") = ""

则报错 500

如果postTime = now() 已经赋过值 则 不会报错

再例如

<%

dim a

a = 1

if a=1 and test1() = 0 then

response.Write("in")

end if 

%>

<%

function test1()

test1 = 0

end function

%>

结果为 

<hr>in

若将1改为0

<%

dim a

a = 0

if a=1 and test1() = 0 then

response.Write("in")

end if 

%>

<%

function test1()

test1 = 0

response.Write("<hr>")

end function

%>

结果为

<hr>

说明  尽管 and 前面结果为false ,但and 后面都会执行 response.write("<hr>")

或 or

<%
dim a
a = 1
if a=1 or test1() = 0 then
response.Write("in")
end if 
%>
<%
function test1()
test1 = 0
response.Write("<hr>")
end function
%>

其结果也为

<hr>in

不管 前面是否为true 都会执行or后面的代码

结论 当有时 用到判断 and or 时要注意 条件变量是否为空

不与其他语言 一样 使用 && ||作为与或时 ,&&遇到false则结束,or遇到true则结束

例如

<script language="javascript">

var a,b;

a = 1;

if (a==1 || test1()>0){
alert("in");

}

function test1(){
alert("in test1");
return 1;

}

</script>

不会执行test1()

使用 && 时如果前面 a==1为false 也不会去执行test1()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: