30
[CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

[CSE10200] Programming Basis

(프로그래밍 기초)

Chapter 3

Seungkyu Lee

Assistant Professor, Dept. of Computer Engineering

Kyung Hee University

Page 2: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Expressions

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

• Example:

2 + 5

Operand

Operator

Expression

Page 3: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

C++ Expression Format

Page 4: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Primary Expressions

• Three types of primary expressions

Page 5: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Binary Expressions

Page 6: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Binary Expression -- Multiplicative Expressions

10 * 12

20 / 4

5 % 2 ??? Modulus Operator (%) (나머지 연산)

5 % 2 1

5 % 3 2

6 % 3 0

Try!!!

Page 7: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Binary Expression -- Additive Expressions

• Additive expression – 3 + 5, 4 – 6

Page 8: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

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.

Page 9: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Simple Assignment

• Consists of simple algebraic expressions

• Examples

Try!!!

Page 10: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Compound Assignment

• Shorthand notation for a simple assignment

• Examples

Try!!!

Page 11: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Postfix Expressions

Remember!

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

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

Try!!!

Page 12: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

5) Unary expressions

Remember!

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

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

Try!!!

Page 13: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

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!!!

Page 14: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Operator Precedence • Which operator will

be evaluated first?

• Each operator has one of 18 precedence levels

• Look at the cover page of the book

Page 15: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Operator Precedence Examples

• 2 + 3 * 4

( 2 + ( 3 * 4) )

• -b++

( -( b++ ) )

Page 16: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Operator Associativity • Determine the

evaluation order of the operators having the same precedence

• Left associativity vs. Right associativity

Page 17: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Operator Associativity Examples

• Left associativity

• Right associativity

Page 18: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Try!!!

Page 19: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Statements

• A block of instructions

• Types of statements

Page 20: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

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.

Page 21: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Compound Statements

• A block of multiple statements

Page 22: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Sample Programs

Try!!!

Page 23: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Sample Programs

Try!!!

Page 24: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

• Additional Practice!!!

Page 25: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Sample Programs

Try!!!

Page 26: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Sample Programs

Try!!!

Page 27: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Try!!!

Page 28: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Try!!!

Page 29: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Try!!!

Page 30: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture4.pdf · 2016-09-19 · [CSE10200] Programming Basis (프로그래밍 기초) Chapter 3 Seungkyu Lee Assistant Professor, Dept. of Computer

Try!!!