C++ 분기문 및 연산자 활용과 2차원 배열 구현

분기문

void CheckCondition() {
    bool flag = true;
    std::cout << std::boolalpha;
    if (flag) {
        std::cout << "flag 상태: " << flag << std::endl;
    }

    bool status = false;
    if (status) {
        std::cout << "status 값: " << status << std::endl;
        return;
    }
    std::cout << "status 조건 미충족" << std::endl;

    int num = 100;
    if (num == 100) std::cout << "num == 100: " << num << std::endl;
    if (num > 50) std::cout << "num > 50: " << num << std::endl;
}
void EvaluateCases() {
    int score = 85;
    if (score >= 90) {
        std::cout << "우수" << std::endl;
    } else if (score >= 70) {
        std::cout << "보통" << std::endl;
    } else {
        std::cout << "미흡" << std::endl;
    }
}
void ProcessSwitch(int code) {
    switch(code) {
        case 1: 
            std::cout << "코드 1 처리" << std::endl;
            break;
        case 2: {
            int temp = 5;
            std::cout << "코드 2: " << temp << std::endl;
            break;
        }
        default:
            std::cout << "기본 처리" << std::endl;
    }
}

연산자

void ExecuteOperators() {
    int arr[2] = {15, 25};
    std::cout << "배열[0]: " << arr[0] << std::endl;

    int result = (arr[0] * 2) - 5;
    std::cout << "계산 결과: " << result << std::endl;

    struct Point { int x; int y; };
    Point pt = {3, 7};
    std::cout << "좌표: " << pt.x << "," << pt.y << std::endl;

    int val = -30;
    std::cout << "증감 연산: " << ++val << std::endl;

    unsigned data = 0x0F;
    std::cout << "비트 반전: " << ~data << std::endl;
    
    int x = 15, y = 4;
    std::cout << "나머지: " << x % y << std::endl;
    std::cout << "시프트: " << (x << 2) << std::endl;
    
    bool cond1 = true, cond2 = false;
    std::cout << "논리 연산: " << (cond1 || cond2) << std::endl;
}

2차원 배열

void MatrixExample() {
    int grid[2][3] = {{1,2,3}, {4,5,6}};
    
    auto PrintMatrix = [](int mat[][3], int rows) {
        for(int i=0; i
void AddressAnalysis() {
    int table[2][2] = {10,20,30,40};
    std::cout << "배열 시작 주소: " << table << std::endl;
    std::cout << "행 단위 주소: " << table[1] << std::endl;
    std::cout << "전체 크기: " << sizeof(table) << " 바이트" << std::endl;
}

태그: C++조건문 C++연산자 2차원배열 메모리관리 비트연산

7월 11일 22:59에 게시됨