Chapter 02 Description of Simple Data
6. Basic Computation of Data - Expressions
6.1 Component of Expression
-
Expression consists of operators, operands and parentheses, which make up the primitive computation unit in a program.
-
Constants and Variables alone make up a special form of expression called primitive expression or atomic expression.
-
If there are two operators live next to each other, to make it not ambiguous, we use whitespace to separate them apart.
6.2 Types of Expression
1. According to Their Return Value
- Arithmetic Expression evaluates to integer type or real type.
- Relational/Boolean Expression evaluates to boolean type.
- Address Expression evaluates to an address.
2. According to Whether its Value is Determined When Compiling
- Constant Expression’s value is determined when compiling because its value doesn’t need the implementation of the whole program to define.
3. According to Whether it has an Explicit Address in the Memory
- Lvalue Expression has an address representing a room in the memory to store its value and its addressed can be accessed using
&
.- Lvalue expression can also be divided into modifiable and unmodifiable because a symbolic constant also has its address but its value can’t be modified.
- Rvalue Expression’s value is stored in temporary store unit whose address can’t be accessed during the program using
&
and when the expression is evaluated over, the space will be released.
6.3 Precedence and Associativity of an Operator
Each operator has its built-in associativity and precedence.
The precedence defines the priority of two nearby operator while the associativity defines who came first when their are of the same priority.
Basic Rule for Precedence:
-
unary > binary > ternary > assignment
-
arithmetic > bitwise shift > relational > bitwise logic > logic
6.4 Output
#include <iostream>
using namespace std;
int main(){
......
cout << a + b * c;
cout << a;
cout << b;
cout << endl; //another line
//or
cout << a + b * c << a << b << endl;
}