unity3D-游戏/AR/VR在线就业班 C#入门关系运算和逻辑运算学习笔记

本文介绍Unity3D中的关系运算符(如大于、小于等)和逻辑运算符(如逻辑或、逻辑与及取反)的基础用法,并通过C#代码示例展示如何在实际开发中应用这些运算符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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); 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值