ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [TIL] C# 사전문법 기초 (연산자, 조건문)
    게임 기획/내일배움캠프 2025. 4. 3. 19:02
    수식 산술 연산
    기호
    기능
    예시
    +
    더하기
    int x = 10;
    int result;

    result = x + 3; // result: 13
    -
    빼기
    int x = 10;
    int result;

    result = x - 3// result: 7
    *
    곱하기
    int x = 10;
    int result;

    result = x * 3// result: 30
    /
    나누기
    int x = 17;
    int result_1;
    int result_2;

    result_1 = x / 5// result_1: 3
    result_2 = x %5
    // result_2: 2

     
    %
    나머지
    ++
    증가
    int x = 10;
    x++; // x: 11
    Console.WriteLine(x); // 11 출력

    int x = 10;
    Console.WriteLine(x+10); // 20출력
    Console.WriteLine(x); // 10출력(증감연산을 사용하지 않으면 x에 값을 다시 할당해주지 않을 시 기존 값 출력)

    증감 연산자는 변수의 앞 뒤 둘 다 사용 가능
    연산자가 변수의 뒤에 붙으면 해당 라인 이후부터 적용
    연산자가 변수의 앞에 붙으면 해당 라인부터 적용

    int x = 10;
    Console.WriteLine(x++); // 10 출력
    Console.WriteLine(x); // 11 출력

    int x = 10;
    Console.WriteLine(++x); // 11 출력
    Console.WriteLine(x); // 11 출력

    --
    감소

     

    문자 산술 연산
    string hello = "안녕하세요!";
    string academy = "스파르타 코딩클럽 입니다!";

    string result = hello + academy;
    Console.WriteLine(result); // 안녕하세요!스파르타 코딩클럽 입니다! (출력)

    string hello = "안녕하세요!";

    string result = hello + 스파르타 코딩클럽 입니다!";
    Console.WriteLine(result); // 안녕하세요!스파르타 코딩클럽 입니다! (출력)

    int year = 2025;
    string result = year + "년 입니다.";

    Console.WriteLine(result); // 2025년 입니다. (출력)

     

    같음 연산자

     

    "=" 는 변수에 데이터를 저장할 때 실제로 활용되는 부호이기 때문에 다른 부호 활용

    ==: 같다

    !=: 다르다

    int num = 10;
    bool isSame = num == 10; // true

     

    비교 연산자

     

    <, <= : 크다, 크거나 같다.

    >, >= : 작다, 작거나 같다.

    int age = 23;
    bool isAdult = age > 19; // true

    int age = 23;
    bool isAdult = age < 19; // false

     

    논리 연산 정리표
    기호
    사용
    의미
    참고
    >
    a > b
    a 가 b보다 크면 true
     
    >=
    a >= b
    a 가 b보다 크거나 같으면 true
     
    <
    a < b
    b 가 a보다 크면 true
     
    <=
    a <= b
    b 가 a보다 크거나 같으면 true
     
    ==
    a == b
    a 와 b 가 같으면 true
    = 이랑 헷갈리지 마세요!!!!!!
    !=
    a != b
    a 와 b 가 다르면 true
     
    &&
    a && b
    두가지 조건이 모두 만족하면 true
    ||
    a || b
    둘 중 하나만 만족하면 true

     

    if, else if, else

     

    if(조건)
    {
    실행내용
    }
    else if(추가 조건)
    {
    실행내용
    }

    if와 (else if, else)는 둘 중 하나만 실행!!
    if 조건이 성립되었다면 (else if, else)는 실행되지 않음


    int x = 20;

    if(x>10)
    {
    // 조건이 성립돼 if문 안의 내용을 실행합니다.
    Console.WriteLine("x는 10보다 크다!");
    }
    else if(x>5)
    {
    // if문이 실행되었기에 실행x
    Console.WriteLine("x는 10보다는 작지만 5보다는 크다!");
    }

    if(0>1)
    {
    Console
    .WriteLine("0은 1보다 크다!");
    }
    else
    {
    // if문이 틀렸기 때문에 else가 실행됩니다.
    Console
    .WriteLine("틀렸습니다!!");
    }

    else는 위의 조건이 실패하면 무조건 실행 됨! (선택지가 마지막 하나만 남았을 때)


    int score = 90;
    if (score >= 90)
    {
    Console.WriteLine("A학점!");
    }
    else if (score >= 80)
    {
    Console.WriteLine("B학점!");
    }
    else if (score >= 70)
    {
    Console.WriteLine("C학점!");
    }
    else // 위의 조건이 전부 실패했을 때 else 값 출력
    {
    Console.WriteLine("낙제!");
    }

    else if는 if 조건이 실패했을 때 추가 조건을 확인 함! (선택지 여러개)

     

     

Designed by Tistory.