unity3D-游戏/AR/VR在线就业班 C#入门关系运算和逻辑运算
http://edu.youkuaiyun.com/lecturer/107
一、关系运算符
> >= < <= == !=
using System;
namespace Lesson10
{
class MainClass
{
public static void Main (string[] args)
{
/*关系运算符*/
int a = 4;
int b = 9;
//关系运算符包括:>(大于) <(小于)>=(大于等于) <= (小于等于)==(等于,完全相等) !=(不等于)
bool r =a>b;
Console.WriteLine (r);
r = 5 == 5; // r=true;
r = 4 != 4; // r=true;
//关系运算符高于赋值预算法,低于一般的算术运算符
Console.WriteLine (r);
//为了避免记错运算级别,可以通过口号来解决问题
//r = (5 == 5);
//r = (4 != 4);
}
}
}
二、逻辑运算符
1、逻辑或运算符——||
using System;
namespace Lesson10
{
class MainClass
{
public static void Main (string[] args)
{
/*关系运算符*/
int a = 4;
int b = 9;
//关系运算符包括:>(大于) <(小于)>=(大于等于) <= (小于等于)==(等于,完全相等) !=(不等于)
bool r =a>b;
Console.WriteLine (r);
r = 5 == 5; // r=true;
r = 4 != 4; // r=true;
//关系运算符高于赋值预算法,低于一般的算术运算符
Console.WriteLine (r);
//为了避免记错运算级别,可以通过口号来解决问题
//r = (5 == 5);
//r = (4 != 4);
//l逻辑运算符
//逻辑运算符—— ||
r =true || false;//两个操作数中,只要有一个是true,那么整个式子的结果就是true,否则就是fals
Console.WriteLine (r);
}
}
}
2、逻辑与运算符——&&(and符号)
using System;
namespace Lesson10
{
class MainClass
{
public static void Main (string[] args)
{
/*关系运算符*/
int a = 4;
int b = 9;
//关系运算符包括:>(大于) <(小于)>=(大于等于) <= (小于等于)==(等于,完全相等) !=(不等于)
bool r =a>b;
Console.WriteLine (r);
r = 5 == 5; // r=true;
r = 4 != 4; // r=true;
//关系运算符高于赋值预算法,低于一般的算术运算符
Console.WriteLine (r);
//为了避免记错运算级别,可以通过口号来解决问题
//r = (5 == 5);
//r = (4 != 4);
//l逻辑运算符
//逻辑或运算符—— ||
//r =true || false;//两个操作数中,只要有一个是true,那么整个式子的结果就是true,否则就是fals
r =( (5 > 7) || (4 < 9));// (5 > 7) false ,(4 < 9) true ,使用括号括起来避免运算符优先级问题(5 > 7) || (4 < 9)
Console.WriteLine (r);
//逻辑与运算符—— &&
//两个操作数中,只要有一个是false,那么整个式子的结果就是false,否则为true
//r=(true &&true);//r=true
r = (4 > 7) && (4 * 2 <= 6);//r=false
Console.WriteLine (r);
}
}
}
3、逻辑与运算符——!(单目运算符)
r=!true;//将它所连接的操作数取反
Console.WriteLine (r);