Expressions and Assignment Statements
๐ก Expression assign์ ์ด๋ป๊ฒ ํ ๊ฒ์ธ๊ฐ
- ๊ณ์ฐ์ ํ๋ ํต์ฌ ๋ฐฉ๋ฒ: expression
- expression ์ข
๋ฅ 4๊ฐ์ง
- arithmetic
- boolean(logic)
- relational(comparison)
- conditional (์กฐ๊ฑด๋ถ ์ฐ์ฐ)
- ์ผํญ์ฐ์ฐ์: c, c++, java ์ง์, python์ ์์
- ์์ผ๋ฉด writability๋ ๋์์ง์ง๋ง readability๊ฐ ๋จ์ด์ง๋ค.
Arthmetic Expressions
- operator์ operand, parentheses(๊ดํธ), function call๋ก ๊ตฌ์ฑ
- operator: ๊ตฌ์ฑ๋ฐฉ์๊ณผ ๋์ ๋ฐฉ์์ ๋ฐ๋ผ ๋จํญ,2ํญ, 3ํญ ์ฐ์ฐ์๋ก ๊ตฌ๋ถ๋จ
Arithmetic Expressions ์ง์ํ ๋ ๊ณ ๋ฏผํด์ผํ ์ฌํญ
- Operator precedence rules?
- Operator associativity rules? โ ์ ๋์ ์ธ์ด์์ ์ด๋ป๊ฒ ์ ์ํ๋๋์ ๋ฐ๋ผ ๋ฌ๋ผ์ง
- Order of operand evaluation?
- ์ฐ์ฐ์๋ฅผ ๋จผ์ ๋ค ํ๊ฐํ๊ณ operation์ ํ ๊ฑด์ง, ์์๋๋ก ๊ฐ๊ฑด์ง ์ธ์ด๊ฐ ์ด๋ป๊ฒ ์ ์ํ๋๋์ ๋ฐ๋ผ ๋ฌ๋ผ์ง
- Operand evaluation side effects?
- function์ด ํผ์ฐ์ฐ์ ๊ฐ์ ์ํฅ ๋ฏธ์นจ โ functionํธ์ถ๋ก ์ธํ ์ฌ์ด๋ ์ดํํธ ๋ฐ์, ์ธ์ด์ ๋ฐ๋ผ ๋ฐ์์ ๋ง์์ง ํ์ฉํ ์งโฆ
- ํ์ฉํ๋ค๋ฉด ์คํผ๋๋์ ํ๊ฐ๋ฐฉ๋ฒ์ ์ผ์ชฝ๋ถํฐ ๊ฐ์ง, ์ค๋ฅธ์ชฝ๋ถํฐ ๋คํด๋๊ณ ์์ํ ์ง
- Operator overloading?
- c++ ํ์ฉ, cํ์ฉx
- c++, java ํ์ฉ
- Type mixing in expressions?
- ์์์์ ํผ์ฐ์ฐ์๋ค์ ํ์
์ ์์์ง ๋ง์ง
- ์๋๋ค๋ฉด ์ด๋ค์์ผ๋ก ๊ฒฐ๊ณผ ๋์ถํ ์ง โ strong, weak type์ ๋ฐ๋ผ ๋ค๋ฆ
- ๋น์ฐํ ํ์ฉ๋ ๊ฒ
Operator Precedence Rules
- precedence: ์ฐ์ฐ์ ์ฐ์ ์์
- typical associativity rules: ๊ฐ์ ์ฐ์ ์์์ ์ฐ์ฐ์๋ค์ ๋ํด ๋ญ๋ถํฐ ๊ณ์ฐํ ์ง
- ๊ดํธ๋ ๋จผ์ ์ฒ๋ฆฌ๋์ด์ผ ํจ
- *Functional side effects:* expression์ ์ฌ์ฉ๋ ๋ณ์์ค์ ํ๋๋ผ๋ ํจ์ ํธ์ถ๋ก ์ธํด ๊ฐ์ด ๋ฐ๋ ๊ฐ๋ฅ์ฑ์ด ์กด์ฌํ๋ฉด ๊ฒฐ๊ณผ์ ์ผ๊ด์ฑ ๊นจ์ง๋ค
- ํด๊ฒฐ์ฑ
1. functional side effect์์ฒด๋ฅผ ํ์ฉํ์ง ์๋๋ค
- ์ตํต์ฑ ์๋ ์ธ์ด๊ฐ ๋จ
- ํด๊ฒฐ์ฑ
2. ํผ์ฐ์ฐ์ ํ๊ฐ ์์๋ฅผ ๊ณ ์
- Java๋ ํผ์ฐ์ฐ์์ ํ๊ฐ๊ฐ ์ผ์ชฝ์์ ์ค๋ฅธ์ชฝ ๋ฐฉํฅ์ผ๋ก ์ด๋ฃจ์ด์ง๋ค
- ์ผ๋ถ ์ปดํ์ผ๋ฌ์ ์ต์ ํ๋ฅผ ์ ํ
- ๋ค๊ฐ์ง ์ธ์ด ๋ชจ๋ referential transparency ์ ๊ณตํ์ง ์๋๋ค
- ์์์ด ๋๊ฐ์ด ์๊ฒผ์ผ๋ฉด ๊ฒฐ๊ณผ ๋ฌด์กฐ๊ฑด ๋๊ฐ์์ผํ๋ค๋ ๊ฒ
- pure functional language๋ ๋ณ์๊ฐ ์๊ธฐ ๋๋ฌธ์, referential transparency ๋ณด์ฅ
- functional side effect โ referential transparency
Overloaded Operators
- c, c++, java ์ง์
- ์ฅ์ : writability ๋์์ง
- ๋จ์ : reliability ๋จ์ด์ง
- c,c++์์๋ user define ovaloaded operator์ง์
- ์๋ฏธ์ ๋ง๊ฒ ์์ฑํด์ผํจ
- readability๊ฐ ๋จ์ด์ง ์๋
Type Conversion
- Narrowing conversion
- float โ int
- narrowing explicit conversion์ 4๊ฐ์ง ์ธ์ด ๋ค ๋์ง๋ง, Java๋ narrowing implicit conversion์ด ์๋๋ค
- Widening conversion
- int โ float
- 4๊ฐ์ง ์ธ์ด ๋ชจ๋ widening explicit, widening implicit conversion ๋จ
- Casting: explicit conversion
- c based languages ์์๋ casting์ด๋ผ๊ณ ํจ
- C,Java,C++: (int) angle
- Coercion: implicit conversion
- flexibility ๋์์ง, reliability ๋ฎ์์ง
- ์ปดํ์ผ๋ฌ๊ฐ type error๋ฅผ ๊ฐ์งํ๊ธฐ๊ฐ ์ด๋ ค์์ง๋ค
Errors in Expressions
- ์์ธ
- inherent limitations of arithmetic (์ํ ์๋ฆฌ) โ division by zero
- limitations of computer arithmetic (๋ฆฌ์์ค๊ฐ ์๋ผ์) โ overflow, underflow
- ํด๊ฒฐ
Relational, Boolean Expressions
- relational, boolean expression์ ์๋ก ์ฐ๊ด ์์
- boolean์ ๋ชจ๋ relational expression์
- ๊ฒฐ๊ณผ๊ฐ true or false๋ก ๋์ค๋ ์ฐ์ฐ โ logic operation์ ์ํํ ๊ฒ
- chained relation:
4 < 5 < 6
- c/c++ : true
- java: ํ์ฉ์๋จ
- python: true
4 < 5 > 3
- c/c++: false ( ๋ฌด์กฐ๊ฑด left to right, 4 < 5 ๊ฐ 1์ด๋ฏ๋ก )
- python: true ( 4<5 && 5>3 ์ผ๋ก ๋ฌถ์ด์ ํด์)
- short circuit evaluation: ์๋ถ๋ถ์ด ํ์คํ๋ฉด ๋ท๋ถ๋ถ ํ์ธ ์ํ๋ ๊ฒ์ ์ง์
- 4๊ฐ์ง ์ธ์ด ๋ชจ๋ ์ง์
- &&: ํ๋๋ผ๋ false๋ฉด ๋ค ๊ณ์ฐ ์ํจ
- || : ํ๋๋ผ๋ true๋ฉด ๋ค ๊ณ์ฐ ์ํจ
- &, | ๋ ๋จ์ถํ๊ฐX
- assignment statements
- assign operator๋ ๋ชจ๋ ์ธ์ด์์ =
- compound assignment operator (๋ณตํฉ์ฐ์ฐ์)
- =๊ธฐํธ์ ๋ค๋ฅธ ์ฐ์ฐ์ ํฉ์ณ๋์ ๊ฒ. (+=, -= ๋ฑ)
- c,c++, java, python๋ชจ๋ ์ง์
- unary assignment operator (๋จํญ์ฐ์ฐ์)
- ++ a, a ++
- python๋นผ๊ณ ๋ชจ๋ ์ง์ ๋จ
- assignment as an expression
- ๋์
๋ฌธ์ด ์์์ ํผ์ฐ์ฐ์๋ก ์ฐ์ด๋ ๊ฒ
while ((ch=getchar()) โ EOF ) { โฆ }
- python์๋จ
- multiple assignment
- assignment in functional languages
- functional languages์์ assignment ์ง์์ ํ์ง๋ง, ๋ณ์๊ฐ ์๊ธฐ ๋๋ฌธ์ ์์ ํ ๋น๋ง ๋๋ค.
- Mixed-Mode Assignment
- ์ฌ๋ฌ๊ฐ์ ํ์
์ด ์์ฌ์๋ ์์์ด ์์ผ๋ฉด mixed-mode๋ผ๊ณ ํจ
- ํน์ ๋ณ์์ mixed mode expression ๋์
ํ๋ฉด mixed mode assignment๊ฐ ๋จ โ c,c++,java, python ๋ชจ๋ ์ง์
- java๋ widening assignment coercion๋ง ์ง์๋๋ค.