C# 연산자

m._.jooong·2023년 2월 23일
0

Unity C#

목록 보기
6/22
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 연산자
// 1. 수식연산자 : 사칙연산 (+, -, *, /, %)
// 2. 증감연산자 : ++, --
// 3. 할당연산자 : =, +=, -=, *=, /=, %=
// 4. 논리연산자 : &&, ||, !
// 5. 관계연산자 : <, >, ==, <=, >=, != 
// 6. 비트연산자 : &, |, ^ 

public class Test_1 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        // 1. 수식연산자 : +, -, *, /, %
        int a = 88, b = 22;
        int c = a + b;
        Debug.Log(a + " + " + b + " = " + c);

        c = a - b;
        Debug.Log(a + " - " + b + " = " + c);

        string str = string.Format("{0} * {1} = {2}", a, b, a * b);
        Debug.Log(str);

        str = string.Format("{0} / {1} = {2}", a, b, a / b);
        Debug.Log(str);

        // % 나머지 연산
        Debug.Log(string.Format("{0} % {1} = {2}", a, b, a % b));
        // 나누는 숫자보다 하나 작은 값까지 반복되는 특징이 있다.

        //  몫                나머지
        // 0 / 2 = 0        0 % 2 = 0
        // 1 / 2 = 0        1 % 2 = 1
        // 2 / 2 = 1        2 % 2 = 0
        // 3 / 2 = 1        3 % 2 = 1
        // 4 / 2 = 2        4 % 2 = 0
        // 5 / 2 = 2        5 % 2 = 1


        // 2. 증감연산자 : ++, --
        // C, C++, C#
        int cc = 0;
        cc++;   // cc = cc + 1; // 1증가 시키는 뜻
        ++cc;   // 단독으로 쓰일 때는 1증가 시키는 뜻
        Debug.Log("단독 사용일 경우 : " + cc);     // 2

        cc = 0;
        Debug.Log(string.Format("복합 명령어으로 사용될 경우 뒤에 붙일 때 : {0}", cc++));    // 0

        cc = 0;
        Debug.Log(string.Format("복합 명령어으로 사용될 경우 앞에 붙일 때 : {0}", ++cc));    // 1

        int ff = 10;
        ff--;   // ff = -1; // 단독으로 쓰일 경우 1감소 시키는 뜻
        --ff;
        Debug.Log(string.Format("ff : {0}", ff));   // 8

        // 3. 할당연산자 : =, +=, -=, *=, /=, %=
        int a_xx = 10;
        a_xx += 5;  // a_xx = a_xx + 5;
        // a_xx = a_xx + 1;     a_xx++;    a_xx +=1;
        a_xx -= 3;  // a_xx = a_xx -3;

        int a_yy = 10;
        a_yy *= 2; // a_yy = a_yy * 2;
        a_yy /= 2; // a_yy = a_yy / 2;
        a_yy %= 2; // a_yy = a_yy % 2;


        // 4. 논리연산자 : &&, ||, !
        int ggg = 50;
        int hhh = 60;
        bool a_Check = ggg > 40 && hhh > 50;    // && : And연산자, ~이고, 그리고
        Debug.Log("ggg > 40 && hhh > 50 : " + a_Check);
        // true  && true  = true
        // true  && false = false
        // false && true  = false
        // false && false = false

        a_Check = ggg > 40 || hhh > 70;         // || : or, ~이거나, 또는 
        Debug.Log("ggg > 40 || hhh > 70 : " + a_Check);     // true
        // true  || true  = true 
        // true  || false = true 
        // false || true  = true 
        // false || false = false

        a_Check = !(ggg > hhh);                 // ! : Not 결과값을 반전시키는 연산자
        Debug.Log(a_Check);     // true


        // 5. 관계연산자 : <, >, ==, <=, >=, != 
        int AAA = 50;
        int BBB = 60;
        Debug.Log("AAA < BBB : " + (AAA < BBB));    // true
        Debug.Log("AAA > BBB : " + (AAA > BBB));    // False
        Debug.Log("AAA == BBB : " + (AAA == BBB));  // False
        Debug.Log("AAA != BBB : " + (AAA != BBB));  // true
        Debug.Log("AAA <= BBB : " + (AAA <= BBB));  // true
        Debug.Log("AAA >= BBB : " + (AAA >= BBB));  // False


        // 6. 비트연산자 : &, |, ^
        int nnn = 5;    // 0101
        int mmm = 10;   // 1010

        int Result = nnn & mmm;     //  0000 --->   0
        Debug.Log("nnn & mmm : " + Result);     //  0

        Result = nnn | mmm;         //  1111 --->  15
        Debug.Log("nnn | mmm : " + Result);     // 15

        // ^ : xor 연산자 : 두 값이 같으면 0, 두 값이 다르면 1
        Result = nnn ^ mmm;         //  1111 --->  15
        Debug.Log("nnn ^ mmm : " + Result);    //  15

        int kkk = 2357;
        int a_SecVal = kkk ^ 6789;
        Debug.Log("a_SecVal : " + a_SecVal);    // 5040
        int a_MyVal = a_SecVal ^ 6789;
        Debug.Log("a_MyVal : " + a_MyVal);      // 2357



    }

    bool m_Check = false;   // 멤버변수

    // Update is called once per frame
    void Update()
    {
        // unity 에디터에서 키보드 입력이 안될 때 확인해 봐야할 사항
        // 1. Game View 창을 한번 클릭해 준다.
        //    (유니티 에디터 GameView 창에 포커스 상태를 가게 만들기 위해)
        // 2. 한 / 영 키가 눌려져 있는 상태에서 (한글 입력 상태는 안된다.)
        //    (영문키는 영어 입력 상태에서만 정상 입력 된다.)

        if (Input.GetKeyDown(KeyCode.K) == true)
        {
            m_Check = !m_Check;
            if(m_Check == true)
            {
                Debug.Log("라이트 On");
            }
            else
            {
                Debug.Log("라이트 Off");
            }
        }
    }
}

0개의 댓글