본문 바로가기
IoT 디바이스 활용/C++

C++ - 제어 구조

by cooluk 2020. 10. 9.

제어 구조

제어 구조

  • 제어 구조

    image-20200904142514496

  • 관계 연산자

    image-20200904142539073

  #include <iostream>
  using namespace std;
  int main(int argc, char const *argv[])
  {
      bool b;
      b = (1 == 2);
      cout << std::boolalpha; // 부울린을 true, false로 출력
      cout << b << endl;
      return 0;
  }

false


cout << std::boolalpha; 부분은 설정 : 출력에 나오지 않음

  • 논리 연산자

    image-20200904143147311

  #include <iostream>
  using namespace std;

  int main(int argc, char const *argv[])
  {
      bool b;
      int x = 3;
      int y = 3;
      cout << std::boolalpha; // 부울린을 true, false로 출력

      b = (x == 3) && (y == 3);
      cout << b << endl;

      y = 2;
      b = (x == 3) && (y == 3);
      cout << b << endl;

      b = (x == 3) || (y == 3);
      cout << b << endl;

      x = 2;
      b = (x == 3) || (y == 3);
      cout << b << endl;

      b = !(x == 3);
      cout << b << endl;

      return 0;
  }

true
false
true
false
true


  • if ~ else 문

    image-20200904143405937

    • if 문

      #include <iostream>
      using namespace std;
      
      int main(int argc, char const *argv[]) {
          int x = 100;
      
          if(x == 100)
              cout << "x가 100입니다." << endl;
      
          if(x == 100) {
              cout << "x의 값을 출력합니다." << endl;
              cout << "x가 100입니다." << endl;
          }
      
          return 0;
      }

      x가 100입니다.
      x의 값을 출력합니다.
      x가 100입니다.


    • if ~ else 문(1)

      #include <iostream>
      using namespace std;
      
      int main(int argc, char const *argv[]) {
          int x = 120;
      
          if(x == 100)
              cout << "x가 100입니다." << endl;
          else
          {
              cout << "x가 100이 아닙니다" << endl;
          }
      
          return 0;
      }

      x가 100이 아닙니다


    • if ~ else 문(2)

      #include <iostream>
      using namespace std;
      
      int main(int argc, char const *argv[])
      {
          int x, y;
      
          cout << "x값을 입력하세요 ";
          cin >> x;
      
          cout << "y값을 입력하세요 ";
          cin >> y;
      
          if (x > y)
              cout << "x가 y보다 큽니다." << endl;
          else
              cout << "y가 x보다 크거나 같습니다." << endl;
          return 0;
      }

      x값을 입력하세요 30
      y값을 입력하세요 40
      y가 x보다 크거나 같습니다.


  • 다중 if ~ else 문

    image-20200904150620828

    #include <iostream>
    using namespace std;
    int main(int argc, char const *argv[])
    {
        int x, y;
    
        cout << "x값을 입력하세요";
        cin >> x;
    
        cout << "y값을 입력하세요";
        cin >> y;
    
        if (x > y)
            cout << "x가 y보다 큽니다." << endl;
        else if (x < y)
            cout << "x가 y보다 작습니다." << endl;
        else
            cout << "x와 y가 같습니다." << endl;
    
        return 0;
    }

    x값을 입력하세요 30
    y값을 입력하세요 40
    y가 x보다 크거나 같습니다.


  • switch 문

    • switch 문(1)

      #include <iostream>
      using namespace std;
      int main(int argc, char const *argv[])
      {
          int number;
          cout << "숫자를 입력하세요:";
          cin >> number;
      
          switch (number)
          {
          case 0:
              cout << "zero\n";
              break;
          case 1:
              cout << "one\n";
              break;
          case 2:
              cout << "two\n";
              break;
          default:
              cout << "many\n";
              break;
          }
          return 0;
      }

      숫자를 입력하세요:3
      many


    • switch 문(2) - break

      #include <iostream>
      using namespace std;
      int main(int argc, char const *argv[])
      {
          int number;
          cout << "숫자를 입력하세요:";
          cin >> number;
          switch (number)
          {
          case 0:
              cout << "zero\n";
          case 1:
              cout << "one\n";
          case 2:
              cout << "two\n";
          default:
              cout << "many\n";
              break;
          }
          return 0;
      }

      숫자를 입력하세요:1
      one
      two
      many


  • while 루프

    • 조건이 참일 동안 반복

      image-20200904151621273

      )

      image-20200904151643620

      #include <iostream>
      using namespace std;
      int main(int argc, char const *argv[])
      {
          int n = 10;
          while (n > 0)
          {
              cout << n << " ";
              n--;
          }
          cout << "fire!" << endl;
          return 0;
      }

    10 9 8 7 6 5 4 3 2 1 fire!


    case안은 정수 타입만 가능하다.

  • 구구단

    #include <iostream>
    using namespace std;
    
    int main(int argc, char const *argv[])
    {
        int n;
        int i = 1;
    
        cout << "구구단 중에서 출력하고 싶은 단을 입력하세요: ";
        cin >> n;
    
        while (i <= 9)
        {
            cout << n << " * " << i
                 << " = " << n * i << endl;
            i++;
        }
    
        return 0;
    }

    구구단 중에서 출력하고 싶은 단을 입력하세요: 4
    4 * 1 = 4
    4 * 2 = 8
    4 * 3 = 12
    4 * 4 = 16
    4 * 5 = 20
    4 * 6 = 24
    4 * 7 = 28
    4 * 8 = 32
    4 * 9 = 36


  • do ~ while 루프

    • 조건이 참일 동안 반복

    image-20200904152706290

    #include <iostream>
    using namespace std;
    
    int main(int argc, char const *argv[])
    {
        string str;
    
        do
        {
            cout << "문자열을 입력하세요:";
            getline(cin, str);
    
            // cin >> str;  // 입력 문자열에 공백이 있는 경우 테스트 -> 예: 안녕 하세요.
    
            cout << "사용자의 입력: " << str << endl;
        } while (str != "종료");
    
        return 0;
    }

    문자열을 입력하세요:안녕하세요.
    사용자의 입력: 안녕하세요.
    문자열을 입력하세요:종료
    사용자의 입력: 종료


    cin >> str;
    문자열을 입력하세요:안녕 하세요.
    사용자의 입력: 안녕
    문자열을 입력하세요:사용자의 입력: 하세요.
    문자열을 입력하세요:종료
    사용자의 입력: 종료

  • for 루프

    • 조건이 참일 동안 반복

    image-20200904153108744

    )

    image-20200904153112875

    • for 반복문(1)

      #include <iostream>
      using namespace std;
      int main()
      {
          int sum = 0;
          for (int i = 0; i <= 10; i++)
          {
              sum += i;
          }
          cout << "1부터 10까지 정수의 합 = " << sum << endl;
          return 0;
      }

      1부터 10까지 정수의 합 = 55


    • for 반복문(2)

      #include <iostream>
      using namespace std;
      int main()
      {
          long fact = 1;
          int n;
          cout << "정수를 입력하세요: ";
          cin >> n;
          for (int i = 1; i <= n; i++)
          {
              fact = fact * i;
          }
          cout << n << "! = " << fact << endl;
          return 0;
      }

      정수를 입력하세요: 5
      5! = 120


    • break 문

      #include <iostream>
      using namespace std;
      int main()
      {
          for (int i = 1; i < 10; i++)
          {
              cout << i << " ";
              if (i == 4)
                  break;
          }
          return 0;
      }

      1 2 3 4


    • continue 문

      #include <iostream>
      using namespace std;
      int main()
      {
          for (int i = 1; i < 5; i++) {
              cout << "continue 문장 전에 있는 문장" << endl;
              continue;
              cout << "continue 문장 이후에 있는 문장" << endl;
          }
          return 0;
      }

      continue 문장 전에 있는 문장
      continue 문장 전에 있는 문장
      continue 문장 전에 있는 문장
      continue 문장 전에 있는 문장


'IoT 디바이스 활용 > C++' 카테고리의 다른 글

C++ - 문자열  (0) 2020.10.11
C++ - 함수  (0) 2020.10.11
C++ - 배열  (0) 2020.10.11
C++ - 기초 사항  (0) 2020.10.09
C++ - 개발환경 구축  (0) 2020.10.09

댓글