C# Basic Syntax


기본 문법 (Basic Syntax)

변수 및 자료형 (Variables and Data Types)

변수는 데이터를 저장하는 공간으로, 각 변수는 특정 자료형을 갖습니다. C#에는 다양한 기본 자료형이 있으며, 이를 통해 다양한 데이터를 저장할 수 있습니다.

예제: 변수 선언 및 초기화 (Variable Declaration and Initialization)

int number = 10;           // 정수형 변수
double price = 99.99;      // 실수형 변수
char letter = 'A';         // 문자형 변수
string name = "Alice";     // 문자열 변수
bool isTrue = true;        // 불리언 변수

연산자 (Operators)

연산자는 변수나 값에 대해 수학적, 논리적 연산을 수행합니다. C#에서 주로 사용하는 연산자는 다음과 같습니다.

예제: 산술 연산자 (Arithmetic Operators)

int a = 10;
int b = 3;
int sum = a + b;           // 더하기
int difference = a - b;    // 빼기
int product = a * b;       // 곱하기
double quotient = (double)a / b; // 나누기
int remainder = a % b;     // 나머지

예제: 비교 연산자 (Comparison Operators)

bool isEqual = (a == b);   // 같음
bool isNotEqual = (a != b); // 같지 않음
bool isGreater = (a > b);  // 큼
bool isLesser = (a < b);   // 작음
bool isGreaterOrEqual = (a >= b); // 크거나 같음
bool isLesserOrEqual = (a <= b);  // 작거나 같음

예제: 논리 연산자 (Logical Operators)

bool andResult = (a > b) && (b > 0); // AND 연산
bool orResult = (a > b) || (b < 0);  // OR 연산
bool notResult = !(a > b);           // NOT 연산

제어 구조 (Control Structures)

제어 구조는 코드의 흐름을 제어하는 데 사용됩니다. 조건문과 반복문이 주요 제어 구조입니다.

조건문 (Conditional Statements)

조건문은 조건에 따라 다른 코드를 실행합니다.

예제: if 문 (if Statement)
if (a > b)
{
    Console.WriteLine("a는 b보다 큽니다.");
}
else if (a == b)
{
    Console.WriteLine("a와 b는 같습니다.");
}
else
{
    Console.WriteLine("a는 b보다 작습니다.");
}
예제: switch 문 (switch Statement)
int day = 3;
switch (day)
{
    case 1:
        Console.WriteLine("월요일");
        break;
    case 2:
        Console.WriteLine("화요일");
        break;
    case 3:
        Console.WriteLine("수요일");
        break;
    default:
        Console.WriteLine("유효하지 않은 요일");
        break;
}

반복문 (Loops)

반복문은 조건이 충족되는 동안 코드를 반복 실행합니다.

예제: for 문 (for Loop)
for (int i = 0; i < 5; i++)
{
    Console.WriteLine("i의 값: " + i);
}
예제: while 문 (while Loop)
int count = 0;
while (count < 5)
{
    Console.WriteLine("count의 값: " + count);
    count++;
}
예제: do-while 문 (do-while Loop)
int number = 0;
do
{
    Console.WriteLine("number의 값: " + number);
    number++;
} while (number < 5);

예제: 기본 문법 종합 예제 (Comprehensive Example of Basic Syntax)

using System;

namespace BasicSyntaxExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // 변수 선언 및 초기화
            int a = 10;
            int b = 20;

            // 산술 연산자
            int sum = a + b;
            Console.WriteLine("Sum: " + sum);

            // 비교 연산자
            if (a != b)
            {
                Console.WriteLine("a와 b는 같지 않습니다.");
            }

            // 논리 연산자
            if (a > 0 && b > 0)
            {
                Console.WriteLine("a와 b는 모두 양수입니다.");
            }

            // 반복문
            for (int i = 1; i <= 5; i++)
            {
                Console.WriteLine("반복: " + i);
            }

            // 조건문
            switch (a)
            {
                case 10:
                    Console.WriteLine("a는 10입니다.");
                    break;
                default:
                    Console.WriteLine("a는 10이 아닙니다.");
                    break;
            }
        }
    }
}

이 종합 예제는 변수 선언, 연산자 사용, 조건문, 반복문을 모두 포함한 기본 문법을 포괄적으로 보여줍니다. 이러한 기초를 바탕으로 더 복잡한 프로그램을 작성할 수 있습니다.


Posted in C#

Leave a Reply

Your email address will not be published. Required fields are marked *