출처: Leeloo Thefirst
C# 기본 문법과 구조 연산자
서문
안녕하세요! IT Insight 블로그의 ‘아두이노 프로그래밍 설명해주는 블로거’입니다. 오늘은 C#의 기본 문법과 구조 연산자에 대해 알아보도록 하겠습니다.
1. C# 기본 문법
C#은 객체 지향 프로그래밍 언어로, 강력하고 유연한 문법을 제공합니다. 다음은 C#의 몇 가지 기본 문법 요소입니다.
1.1 변수 선언과 할당
C#에서 변수를 선언하고 값을 할당하는 방법은 다음과 같습니다.
int number; // 변수 선언
number = 10; // 값 할당
string name = John; // 변수 선언과 동시에 값 할당
1.2 조건문과 반복문
C#은 조건문과 반복문을 사용하여 프로그램의 흐름을 제어할 수 있습니다.
1.2.1 조건문
if (number > 0)
{
Console.WriteLine(양수입니다.);
}
else if (number < 0)
{
Console.WriteLine(음수입니다.);
}
else
{
Console.WriteLine(0입니다.);
}
1.2.2 반복문
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
while (number < 100)
{
Console.WriteLine(number);
number++;
}
1.3 함수 정의와 호출
C#에서 함수를 정의하고 호출하는 방법은 다음과 같습니다.
int Add(int a, int b)
{
return a + b;
}
int result = Add(3, 5);
Console.WriteLine(result); // 출력: 8
2. 구조 연산자
C#에서는 구조 연산자를 사용하여 다양한 작업을 수행할 수 있습니다. 구조 연산자에는 다음과 같은 종류가 있습니다.
2.1 멤버 선택 연산자 (.)
멤버 선택 연산자는 객체의 멤버에 접근하는데 사용됩니다.
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Person person = new Person();
person.Name = John;
person.Age = 30;
Console.WriteLine(person.Name); // 출력: John
Console.WriteLine(person.Age); // 출력: 30
2.2 산술 연산자 (+, -, *, /, %)
산술 연산자는 숫자형 데이터를 다룰 때 사용됩니다.
int a = 10;
int b = 5;
int sum = a + b; // 덧셈
int difference = a - b; // 뺄셈
int product = a * b; // 곱셈
int quotient = a / b; // 나눗셈
int remainder = a % b; // 나머지
Console.WriteLine(sum); // 출력: 15
Console.WriteLine(difference); // 출력: 5
Console.WriteLine(product); // 출력: 50
Console.WriteLine(quotient); // 출력: 2
Console.WriteLine(remainder); // 출력: 0
2.3 비교 연산자 (==, !=, >, , =, <=)
비교 연산자는 두 값의 비교 결과를 반환합니다.
int a = 10;
int b = 5;
bool isEqual = a == b; // 같은지 비교
bool isNotEqual = a != b; // 다른지 비교
bool isGreater = a > b; // 큰지 비교
bool isLess = a < b; // 작은지 비교
bool isGreaterOrEqual = a >= b; // 크거나 같은지 비교
bool isLessOrEqual = a <= b; // 작거나 같은지 비교
Console.WriteLine(isEqual); // 출력: False
Console.WriteLine(isNotEqual); // 출력: True
Console.WriteLine(isGreater); // 출력: True
Console.WriteLine(isLess); // 출력: False
Console.WriteLine(isGreaterOrEqual); // 출력: True
Console.WriteLine(isLessOrEqual); // 출력: False
마무리
이상으로 C#의 기본 문법과 구조 연산자에 대해 알아보았습니다. C#은 많은 기능과 풍부한 문법을 제공하기 때문에 다양한 프로그래밍 작업을 수행할 수 있습니다. 추가적인 내용이 있으시다면 언제든지 댓글로 남겨주시기 바랍니다. 감사합니다!