[C#] 3. ์—ฐ์‚ฐ์ž

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , ,




C# ์‚ฌ์ „ ๋ฌธ๋ฒ• ๊ธฐ์ดˆ



1. ์—ฐ์‚ฐ์ž(Operator)








2. ์‚ฐ์ˆ ์—ฐ์‚ฐ

image

int x = 10;
int result;

result = x + 3; // result : 13      ๋”ํ•˜๊ธฐ

result = x - 5; // result : 5       ๋นผ๊ธฐ

result = x * 2; // result : 20      ๊ณฑํ•˜๊ธฐ

int result_1;
int result_2;

result_1 = x / 4; // result_1 : 2   ๋ชซ
result_2 = x % 4; // result_2 : 2   ๋‚˜๋จธ์ง€

x++;    // x : 11                   ์ฆ๊ฐ€ +1

int y = 10;
y--;    // y : 9                    ๊ฐ์†Œ -1

  • ์—ฐ์‚ฐ์„ ํ•˜๋Š” ๊ฒƒ ๋ณด๋‹ค * ์—ฐ์‚ฐ์„ ํ•˜๋Š”๊ฒŒ ๋น ๋ฆ…๋‹ˆ๋‹ค. ๊ฐ€๋Šฅํ•˜๋ฉด *๋ฅผ ์ด์šฉ
  • x = 10 / 2 -> x = 10 * 0.5f ( ๋” ๋น ๋ฅด๋‹ค.)
  • ++x ( ๋”ํ•œ ํ›„ ์ถœ๋ ฅ ), x++ ( ์ถœ๋ ฅ ํ›„ ๋”ํ•˜๊ธฐ ) ์€ ์กฐ๊ธˆ๋‹ค๋ฅด๋‹ค

ย ย ย  ์‚ฐ์ˆ ์—ฐ์‚ฐ - ๋ฌธ์ž์—ด

๋ฌธ์ž์—ด์—๋Š” +์—ฐ์‚ฐ์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์ˆซ์ž,๋ฌธ์ž์—ด +์—ฐ์‚ฐ

string hello = "์•ˆ๋…•";
string academy = "ํ•˜์„ธ์š”!";

string result  = hello + academy;       //์•ˆ๋…•ํ•˜์„ธ์š”!
string result  = hello + "ํ•˜์„ธ์š”!";     //์•ˆ๋…•ํ•˜์„ธ์š”!

int year = 2023;

string result  = year + "๋…„ ์ž…๋‹ˆ๋‹ค.";   //2023๋…„ ์ž…๋‹ˆ๋‹ค.

Console.WriteLine(result);

  • ๋ฌธ์ž์—ด + ๋ฌธ์ž์—ด, ์ˆซ์ž + ๋ฌธ์ž์—ด ์˜ˆ์‹œ








3. ๋…ผ๋ฆฌ ์—ฐ์‚ฐ

๋…ผ๋ฆฌ์ ์œผ๋กœ ํŒ๋‹จํ•˜๋Š” ์—ฐ์‚ฐ์ž

๊ฐ™์Œ ์—ฐ์‚ฐ์ž, ๋น„๊ต ์—ฐ์‚ฐ์ž

  • ๋‘ ๊ฐ’์ด ๊ฐ™์€์ง€ ( == )
  • ๋‘ ๊ฐ’์ด ๋‹ค๋ฅธ์ง€ ( != )
  • ๋‘ ๊ฐ’์ด ํฐ์ง€, ์ž‘์€์ง€ ( >, >=, <, <=)
    ๋ฅผ ์ฒดํฌํ•˜๋Š” ์—ฐ์‚ฐ์ž. ๊ฒฐ๊ณผ๋Š” bool ํ˜•ํƒœ
int num = 10;
bool isSame = num == 10;    //  true
bool isSame1 = num != 10;   //  false
int age = 23;
bool isAdult = age > 19;    //   true
bool isKid = age < 19;      //   false

  • ๊ฒฐ๊ณผ๋Š” bool ํ˜•ํƒœ

ย ย ย  ๋…ผ๋ฆฌ์—ฐ์‚ฐ ์ •๋ฆฌํ‘œ

image








3. ๋น„ํŠธ์—ฐ์‚ฐ์ž

image








4. ์กฐ๊ฑด๋ถ€ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž

&& (And ์—ฐ์‚ฐ์ž) ์™€ || (Or ์—ฐ์‚ฐ์ž)

ย ย ย  && (And ์—ฐ์‚ฐ์ž)

๋‘๊ฐ€์ง€ ์กฐ๊ฑด์„ ๋ชจ๋‘ ๋งŒ์กฑํ•˜๋Š”๊ฐ€?
true && true -> true
true && false -> false
false && false -> flase

ย ย ย  || (Or ์—ฐ์‚ฐ์ž)

๋‘๊ฐ€์ง€ ์กฐ๊ฑด ์ค‘ ํ•˜๋‚˜๋ผ๋„ ๋งŒ์กฑํ•˜๋Š”๊ฐ€?
true || true -> true
true || false -> true
false || false -> flase








5. ์—ฐ์Šต๋ฌธ์ œ

{
    // 1. ์ˆซ์ž์˜ ์‚ฌ์น™์—ฐ์‚ฐ
    int ten = 10;
    
    int result = ten + 7;           // 7 ๋”ํ•˜๊ธฐ
    int result1 = ten - 3;          // 3 ๋นผ๊ธฐ
    int result2 = ten * 2;          // 2 ๊ณฑํ•˜๊ธฐ
    float result3 = ten * 1.5f;     // 1.5 ๊ณฑํ•˜๊ธฐ
    float result4 = ten / 3;        // 3 ์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ
    float result5 = ten % 4;        // 4 ๋กœ ๋‚˜๋ˆด์„๋•Œ ๋‚˜๋จธ์ง€

    Console.WriteLine(result);      //~5


    // 2. ๋ฌธ์ž์˜ ๊ณ„์‚ฐ
    string name = "๊น€์ข…์šฑ"; 
    int year = 2023;

    string introduce = "์•ˆ๋…•ํ•˜์„ธ์š”. ์ œ ์ด๋ฆ„์€ "+ name +" ์ž…๋‹ˆ๋‹ค"; // ์•ˆ๋…•ํ•˜์„ธ์š”. ์ œ ์ด๋ฆ„์€ "chad" ์ž…๋‹ˆ๋‹ค.
    string thisYear = "์˜ฌํ•ด๋Š” " + year +"๋…„ ์ž…๋‹ˆ๋‹ค."; // ์˜ฌํ•ด๋Š” '2023๋…„' ์ž…๋‹ˆ๋‹ค.

    Console.WriteLine(introduce);
    Console.WriteLine(thisYear);


    // 3. ๋…ผ๋ฆฌ ์—ฐ์‚ฐ
    ten = 10;

    bool result_1 = ten == 10;    // ten ์ด 10 ์ด๋ž‘ ๊ฐ™๋‹ค
    bool result_2 = ten != 11;    // ten ์ด 11 ์ด๋ž‘ ๊ฐ™์ง€ ์•Š๋‹ค
    bool result_3 = ten <  20;    // ten ์ด 20 ๋ณด๋‹ค ์ž‘๋‹ค
    bool result_4 = ten >   5;    // ten ์ด 5 ๋ณด๋‹ค ํฌ๋‹ค
    
    Console.WriteLine(result_1);    // ~4

}



[C#] ์—ฐ์‚ฐ์ž


์ฐธ๊ณ  : ์œ ๋‹ˆํ‹ฐ TOP


๐Ÿ“”

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ