박가방

[C++] Expression(표현식)과 Statement(명령문) 차이 본문

프로그램 언어/C++

[C++] Expression(표현식)과 Statement(명령문) 차이

박가방 2022. 8. 7. 10:32

1. 서론 

Expression(표현식)은 수식을 의미하며, Statement(명령문)은 실행가능한(Excutable) 최소의 독립적인 코드를 말한다.

 

2. Expression

Expression은 수식으로, 하나 이상의 값으로 표현할 수 있는 코드이다.

수식은 숫자와 연산자만으로 이루어지는 것과 달리 컴퓨터 과학에서 수식은 리터럴(literal) 값 6이나 "Hello, world", 함수변수 이름 등의 식별자, 배열 등의 할당 연산자, 수학연산자(+) 등을 포함한 식을 의미한다.

#include <iostream>

int main() {
    using namespace std;
    int x = 1, y = 2, z = 3;
    int arr[] = { 2,4,6,8,10 };

    cout << 1 + 2 + 3 << endl;
    cout << x + y + z << endl;
    cout << arr[2] << endl;
    cout << 6 << endl;
    cout << "Hello, world" << endl;

}
1+2+3
x+y+z
arr[2]
6
 "Hello, world" 

위의 수식들은 모두 다른형태의 Expression이다. (세미콜론 X)

 

expression은 statement 내에서 사용되므로 그 자체로 컴파일 될 수 없다.

 

3. Statement

Statement(명령문)는 실행가능한 최소한의 독립적인 코드이다.

컴파일러가 이해하고 실행할 수 있는 모든 구문은 Statement이다. 

#include <iostream>

int main() {
    using namespace std;
    int x = 1, y = 2, z = 3;
    int arr[] = { 2,4,6,8,10 };

    cout << 1 + 2 + 3 << endl;
    cout << x + y + z << endl;
    cout << arr[2] << endl;
    cout << 6 << endl;
    cout << "Hello, world" << endl;

}
int x,y,z;                                        // 선언문(declartion statement)
x=1, y=2, z=3;                              // 각 변수에 값을 할당하는 명령문.
if(true)                                          // if 명령문
cout << "Hello, world" << endl;    // 변수 x의 값을 콘솔에 출력하는 명령문

 

Reference
1: https://shoark7.github.io/programming/knowledge/expression-vs-statement
2: https://swk3169.tistory.com/315

'프로그램 언어 > C++' 카테고리의 다른 글

[C++] 리터럴 상수 (Literal Constant)  (0) 2022.08.07
[C++] 함수  (0) 2022.08.07