Introduction to C++ Programming Language Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea
About The Lecture Note This lecture note is intended to be used by students in “Introduction to C++ Programming Language” course at the Kyung Hee University. Originally made by Professor Sungwon Lee for the same course, but slightly modified by me. The slides are mainly based on the class textbook, “Computer Science : A Structured Approach Using C++, 2nd Edition”
CH.3 EXPRESSIONS, OPERATORS, STATEMENTS
Expressions 2 + 5 Operand Expression Operator A sequence of operands and operators that reduces to a single value Example: Operand 2 + 5 Expression Operator
C++ Expression Format Just look, and don’t remember! We will see specific examples.
Primary Expressions Three types of primary expressions
Binary Expressions
Binary Expression -- Multiplicative Expressions 10 * 12 20 / 4 5 % 2 ??? Modulus Operator (%) (나머지 연산) 5 % 2 1 5 % 3 2 6 % 3 0
Binary Expression -- Additive Expressions -- Sample Codes 3 + 5, 4 – 6
Assignment expressions The assignment expression has a value and a result. The value of the total expression is the value of the expression on the right of the assignment operator (=). The result places the expression value in the operator on the left of the assignment operator. The left operand in an assignment expression must be a single variable.
Simple Assignment Consists of simple algebraic expressions Examples
Compound Assignment Shorthand notation for a simple assignment Examples
Sample Code for Assignment Expression
Postfix Expressions Remember! (a++) is (a = a + 1)
5) Unary expressions Remember! (++a) is (a = a + 1)
Example code reading (Program 3-4) #include <iostream> #include <iomanip> using namespace std; int main () { int a = 4; cout << "value of a : " << setw(2) << a << endl; cout << "value of ++a : " << setw(2) << ++a << endl; cout << "new value of a: " << setw(2) << a << endl; return 0; } // main value of a : 4 value of ++a : 5 new value of a: 5
Operator Precedence Which operator will be evaluated first? Each operator has one of 18 precedence levels Look at the cover page of the book
Operator Precedence Examples 2 + 3 * 4 ( 2 + ( 3 * 4) ) -b++ ( -( b++ ) )
Operator Associativity Determine the evaluation order of the operators having the same precedence Left associativity vs. Right associativity
Operator Associativity Examples Left associativity Right associativity
Statements A block of instructions Types of statements
Expression Statements Examples a = 2; a = b = 3; a = 4 + 5; a = b + (45 / c) + 22; a++; * An expression statement is terminated with a semicolon (;). The semicolon is a terminator, and it tells the compiler that the statement is finished.
Compound Statements A block of multiple statements