Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis...

Preview:

Citation preview

[CSE10200] Programming Basis

(프로그래밍 기초)

Chapter 3

Seungkyu Lee

Assistant Professor, Dept. of Computer Engineering

Kyung Hee University

Expressions

• A sequence of operands and operators that reduces to a single value

• Example:

2 + 5

Operand

Operator

Expression

C++ Expression Format

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

Try!!!

Binary Expression -- Additive Expressions

• Additive expression – 3 + 5, 4 – 6

Assignment expressions • The assignment expression has a value and a result.

– Value: the value of the expression on the right of the assignment operator (=).

– Result: the result copies the expression value to 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

Try!!!

Compound Assignment

• Shorthand notation for a simple assignment

• Examples

Try!!!

Postfix Expressions

Remember!

(a++) is (a = a + 1)

(a– –) is (a = a – 1)

Try!!!

5) Unary expressions

Remember!

(++a) is (a = a + 1)

(– –a) is (a = a – 1)

Try!!!

Example code

#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; cout << "value of a++ : " << setw(2) << a++ << endl; cout << "new value of a: " << setw(2) << a << endl; return 0; }

Try!!!

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

Try!!!

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

Sample Programs

Try!!!

Sample Programs

Try!!!

• Additional Practice!!!

Sample Programs

Try!!!

Sample Programs

Try!!!

Try!!!

Try!!!

Try!!!

Try!!!

Recommended