asp.net运算符之逻辑运算符以及其他运算符
逻辑(布尔型)运算符用于对boolean型的结果的表达式进行运算,运算的结果都是boolean型。其运算结果如下所示:
运算符 运算 例子 结果
& AND(与) false&true false
| OR(或) false|true true
^ XOR(异或) false^true true
! NOT(非) !false true
&& AND(短路) false&&true false
|| OR(短路) false||true true
下面对一些比较容易出现问题进行简单解释:
1、“^“ 将计算操作数的逻辑“异或”;也就是说,当且仅当只有一个操作数为 true时,结果才为true。
2、“&”与”&&“的区别在于,如果使用前者连接,那么无论任何情况下,“&”两边的表达式都会参与计算。如果使用后者连接,当“&&”的左边为false,则将不会计算其右边的表达式。
3、“|”与“||”的区别在于,“|”表示两边任何一个布尔表达式为真,该组合就会返回true值;而对于“||”,跟第二差不多,若左边是true则返回true,若左边是falsh,则看右边,若右边为true则为true否则为falsh。
例
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4707')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4707>using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int x = 0;
string name = "搜索吧";
//&运算,两个中任何一个为假时则为假
Response.Write("x != 0 & name = /"搜索吧/"的运算结果是:" (x != 0 & name == "搜索吧"));
Response.Write("<br>");
//|运算,当两个中任何一个为真时,运算结果为真,否则为加
Response.Write("x != 0 | name = /"搜索吧/"的运算结果是:" (x != 0 | name == "搜索吧"));
Response.Write("<br>");
//^运算,当且仅当有一个是True时,为真,否则为假
Response.Write("x != 0 ^ name = /"搜索吧/"的运算结果是:" (x != 0 ^ name == "搜索吧"));
Response.Write("<br>");
Response.Write("x == 0 ^ name = /"搜索吧/"的运算结果是:" (x == 0 ^ name == "搜索吧"));
Response.Write("<br>");
//!运算,若真则假,若假则真
Response.Write("x != 0 的运算结果是:" !(x != 0));
Response.Write("<br>");
//&&短路运算,若左边为假则退出,若左边为真,则看右边
Response.Write("x != 0 && name = /"搜索吧/"的运算结果是:" (x != 0 && name == "搜索吧"));
Response.Write("<br>");
//||短路运算,若左边为真,则为真退出;若左边为假,则看右边,右边为真,则为真,否则为假
Response.Write("x != 0 || name = /"搜索吧/"的运算结果是:" (x != 0 || name == "搜索吧"));
}
}
运算符
您可能感兴趣的文章:
VBScript 运算符
javascript的常用运算符
VBScript的运算符
php运算符及运算符优先级-php入门教程(4)
javascript中的逻辑运算符
php中比较常用的特殊运算符号和函数
php中运算符的优先级是什么?
《Go语言四十二章经》第九章 运算符
asp.net运算符之逻辑运算符以及其他运算符
Go 语言运算符