ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • C++_if (conditional statement, 조건문)
    Programming/_C++ 2023. 7. 20. 03:29

     조건문은 값을 비교하여 그 조건이 'true' 또는 'false'라는 사실을 표현한다.

     

    1. 관계 연산자(relational operator)

    연산자  
    == 우변과 좌변이 같음
    != 우변과 좌변이 다름
    > 우변이 큼
    >= 우변이 크거나 같음
    < 좌변이 큼
    <= 좌변이 크거나 같음

     

    2. if문(if statement)

     '조건'의 값이 'true' 또는 'false'에 따라 작업을 수행한다.

    다음의 예시를 보면 이해하기 쉬울 것이다.

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int num = 5;
    
        // num이 4보다 크면 true를 출력
        if (num > 4)
            cout << "true";
        return 0;
    }

     

    3. if~else문

     if문은 특정 조건이 'true' 또는 'false'에 따라 작업을 수행했다.

    if~else문은 if문이 실행되지 않을 경우, else문이 실행되도록 한다.

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int num = 3;
    
        // num이 4보다 크면 true를 출력 그게 아니라면 false를 출력
        if (num > 4)
            cout << "true";
        else
        	cout << "false";
        return 0;
    }

     

    4. else if문

     만약 추가적인 조건이 붙는다면 어떻게 할까?

    추가적인 조건을 넣기 위해서 if와 else사이에 else if문을 추가하여 조건을 넣을 수 있다.

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int num = 3;
    
        // num이 4보다 크면 true를 출력
        if (num > 4)
            cout << "true";
        // num이 2보다 크고 4보다 작다면 mid를 출력
        else if (num > 2 and num < 4)
        	cout << "mid";
        // 그게 아니라면 false를 출력
        else
        	cout << "false";
        return 0;
    }

    else if는 여러개를 사용해도 상관없다.

     

    5. switch문(switch statement)

     if~else문과 비슷하게 조건문을 다루지만, switch문은 코드를 조금 더 간결하게 만들어주며 특별한 경우에 사용이 된다.

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int input;
        cout << "0~2 사이의 값을 입력하세요" << endl;
        cin >> input;
        
        switch (input)
        {
         case 0:
         	cout << "Zero";
            break;
         case 1:
         	cout << "One";
            break;
         case 2:
         	cout << "Two";
            break;
        }    
        cout << endl;
    	return 0;
    }

    여기서 break문은 꼭 붙이도록 하자.

    또한 case에 해당하지 않는 부분은 default를 사용하여 처리를 한다.

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int input;
        cout << "값을 입력하세요" << endl;
        cin >> input;
        
        switch (input)
        {
        case 0:
            cout << "Zero";
            break;
        case 1:
            cout << "One";
            break;
        case 2:
            cout << "Two";
            break;
        default:
            cout << "Not case";
            break;
        }    
        cout << endl;
    	return 0;
    }

     

    6. 삼항연산자(조건연산자: conditional operator)

     만약 조건이 간단하다면, if문을 사용하지 않아도 조건연산자 '?'를 사용하여 표현할 수 있습니다.

    구조: (conditional operator) ? a : b

    간단한 조건문

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int a = 1;
        int b = 0;
        
        if (a>b)
        	cout << a;
        else
        	cout << b;
    
    	return 0;
    }

    삼향연산자

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int a = 1;
        int b = 0;
        
        //참이면 a, 거짓이면 b를 출력
        int answer = (a>b) ? a:b;
    
    	return 0;
    }

    'Programming > _C++' 카테고리의 다른 글

    C++_function  (0) 2023.07.21
    C++_for_while_(loop statement)  (0) 2023.07.20
    C++_upper_lower  (0) 2023.06.16
    C++?  (0) 2023.06.05
    ios::sync_with_studio, cin.tie, cout.tie  (0) 2023.05.14
Designed by Tistory.