Download presentation
Presentation is loading. Please wait.
1
Introduction to C++ Programming Language
Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea
2
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”
3
CH.3 EXPRESSIONS, OPERATORS, STATEMENTS
4
Expressions 2 + 5 Operand Expression Operator
A sequence of operands and operators that reduces to a single value Example: Operand 2 + 5 Expression Operator
5
C++ Expression Format Just look, and don’t remember! We will see specific examples.
6
Primary Expressions Three types of primary expressions
7
Binary Expressions
8
Binary Expression -- Multiplicative Expressions
10 * 12 20 / 4 5 % 2 ??? Modulus Operator (%) (나머지 연산) 5 % 2 1 5 % 3 2 6 % 3 0
9
Binary Expression -- Additive Expressions -- Sample Codes
3 + 5, 4 – 6
10
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.
11
Simple Assignment Consists of simple algebraic expressions Examples
12
Compound Assignment Shorthand notation for a simple assignment
Examples
13
Sample Code for Assignment Expression
14
Postfix Expressions Remember! (a++) is (a = a + 1)
15
5) Unary expressions Remember! (++a) is (a = a + 1)
16
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
17
Operator Precedence Which operator will be evaluated first?
Each operator has one of 18 precedence levels Look at the cover page of the book
18
Operator Precedence Examples
2 + 3 * 4 ( 2 + ( 3 * 4) ) -b++ ( -( b++ ) )
19
Operator Associativity
Determine the evaluation order of the operators having the same precedence Left associativity vs. Right associativity
20
Operator Associativity Examples
Left associativity Right associativity
22
Statements A block of instructions Types of statements
23
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.
24
Compound Statements A block of multiple statements
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.