C# 코드의 흐름 제어

POSI·2022년 12월 2일
0

C#

목록 보기
3/6

해당 문서는 인프런 [C#과 유니티로 만드는 MMORPG 게임 개발 시리즈] Part1: C# 기초 프로그래밍 입문을 듣고 정리한 필기 노트입니다.

if else

int hp = 100;
bool isDead = (hp <= 0);
    
if (isDead)
{
	Console.WriteLine("You are Dead!");
} else {
	Console.WriteLine("You are alive!");
}
    
// else if ()

switch

int choice = 0;
    
switch (choice)
{
	case 0:
    	Console.WriteLine("가위입니다.");
    	break;
    case 1:
    	Console.WriteLine("바위입니다.");
    	break;
    case 2:
    	Console.WriteLine("보입니다.");
    	break;
    case 3:
    	Console.WriteLine("치트키입니다.");
    	break;
    default:
    	Console.WriteLine("다 실패했습니다.");
    	break;
}

switch에서 변수를 사용하려면 상수화 필요

const int ROCK = 1;
const int PAPER = 2;
const int SCISSORS = 0;
        
switch (choice)
{
    case SCISSORS:
        Console.WriteLine("가위");
        break;
    case ROCK:
        Console.WriteLine("바위");
        break;
    case PAPER:
        Console.WriteLine("보");
        break;
}

삼항연산자

```csharp
int number = 25;
bool isPair = ((number % 2) == 0 ? true : false);
```

열거형 enum

enum Choice
{
    Rock = 1;
    Paper = 2;
    Scissors = 0
}
    
static void Main(string[] args)
{
    switch (choice)
    {
    	case (int)Choice.Scissors:
    		...
    }
}

while

int count = 5;
    
while (count > 0) // 조건 확인 후 시작
{
    Console.WriteLine("HelloWorld");
    count--;
}
    
string answer;
    
do // 일단 한 번 실행 후
{
    Console.WriteLine("잘생김? (y/n) : ");
    answer = Console.ReadLine();
} while (answer != "y"); // 다시 돌아갈 때 조건 확인

for

// i = 0 일 때, i < 5 인지 확인 후 실행, i++
// 따라서 i++나 ++i나 상관없음
for (int i = 0; i < 5; i++)
{
	Console.WriteLine("GO");
}

break / continue

  • break : 루프 탈출
  • continue : 아래 코드를 실행하지 않고 다음 루프로 넘어감

함수 Method

한정자 반환형식 이름(매개변수목록)
{
    	
}
static void HelloWorld()
{
	Console.WriteLine("Hello World");
}
    
static void Main(string[] args)
{
	Program.Helloworld();
}

매개변수를 복사로 넘기기

static void AddOne(int number)
{
	number = number + 1;
}
        
static void Main(string[] args)
{
	int a = 0;
	Program.AddOne(a);
        
	Console.WriteLine(a);
	// Output : 0
}

매개변수를 참조로 넘기기

static void AddOne(ref int number)
{
	number = number + 1;
}
        
static void Main(string[] args)
{
	int a = 0;
	Program.AddOne(ref a);
        
	Console.WriteLine(a);
	// Output : 1
}

ref / out

static void AddOne(ref int number)
{
	number = number + 1;
}
        
static int AddOne2(int number)
{
	return number = number + 1;
}
        
static void Main(string[] args)
{
	int a = 0;
	Program.AddOne(ref a);
	Program.AddOne2(a); // 추천
        
	Console.WriteLine(a);
	// Output : 1
}

out : 값 여러개 반환 (참조)

static void Divide(int a, int b, out int result1, out int result2)
{
	result1 = a / b;
	result2 = a % b;
}
        
static void Main(string[] args)
{
	int num1 = 10;
	int num2 = 3;
        	
	int result1;
	int result2;
	Divide(10, 3, out result1, out result2);
}

오버로딩

= 함수 이름의 재사용

static int Add(int a, int b)
{
	return a + b;
}
    
static int Add(int a, int b, int c)
{
	return a + b + c;
}
    
static void Add(int a, int b)
{
	Console.WriteLine("Add int void");
}
    
static float Add(float a, float b)
{
	return a + b;
}
    
static void Main(string[] args)
{
	int ret = Program.Add(2, 3);
	float ret2 = Program.Add(2.0f, 3.0f);
    
	Console.WriteLine(ret);
}

선택적 매개변수

static int Add(int a, int b, int c = 0, float d = 1.0f, double e = 3.0)
{
	return a + b + c;
}
    
static void Main(string[] args)
{
	int ret = Program.Add(2, 3, d:2.0f);
    
	Console.WriteLine(ret);
}
=```
profile
고양이가 운영하는 테크블로그

0개의 댓글