Chapter 04. 연산자 (Operator)

Post on 19-Jan-2016

47 views 5 download

description

Chapter 04. 연산자 (Operator). 04-1. 이항 연산자.  대입 연산자 (=) 와 산술 연산자 (+, -, *, /, %).  대입연산과 산술연산의 예. 실행결과. 연산과정에서 일어나는 자동 형 변환. 두 피연산자의 타입이 다르면 먼저 형변환을 통해 타입을 일치시킨다 .  피연산자가 정수면 정수형 연산진행  피연산자가 실수면 실수형 연산진행. 실행결과. 연산과정에서 일어나는 자동 형 변환. 피연산자가 정수일 때 - PowerPoint PPT Presentation

Transcript of Chapter 04. 연산자 (Operator)

1

Chapter 04. 연산자 (Operator)

2

04-1. 이항 연산자

3

대입 연산자 (=) 와 산술 연산자 (+, -, *, /, %)

4

대입연산과 산술연산의 예

실행결과

5

연산과정에서 일어나는 자동 형 변환

두 피연산자의 타입이 다르면 먼저 형변환을 통해 타입을 일치시킨다 .

피연산자가 정수면 정수형 연산진행 피연산자가 실수면 실수형 연산진행

실행결과

6

연산과정에서 일어나는 자동 형 변환

• 피연산자가 정수일 때 • 피연산자 중 하나라도 long 타입이면 long 타입 연산• 그렇지 않으면 int 타입 연산

long m = 10 + 10L;

short i = 1;short j = 2;short k = i + j;

short k = (short) (i + j);

// Type mismatch: cannot convert from int to short

7

복합대입 연산자

해석의 원칙은 동일

실행결과

8

관계 연산자

연산의 결과로 true or false 반환

9

관계연산의 예

실행결과

10

논리 연산자

실행결과

연산의 결과로 true or false 반환

11

논리 연산자의 오른쪽에 있는 표현은 필요할 때만 계산된다 .

실행결과

Short-Circuit Evaluation

12

04-2. 단항 연산자

13

부호 연산자로서의 + 와 -

실행결과

14

증가 , 감소 연산자

pre-increment post-incre-ment

15

증가 감소 연산의 예

실행결과실행결과

16

04-3. 비트 연산자

17

비트 연산자

18

비트연산 진리 표

&(and)

|(OR)

^(XOR)

~(NOT)

19

비트연산의 예

실행결과

20

비트 쉬프트 (Shift) 연산자

왼쪽으로의 비트 열 이동은 곱하기 2

오른쪽으로의 비트 열 이동은 나누기 2

비트연산의 특징

21

비트 쉬프트 (Shift) 연산의 예

22

3 항 연산자

23

condition ? exp1 : exp2;

condition 이 참일때exp1 계산값을 반환

condition 이 거짓일때 exp2 계산값을 반환

max = num1>num2 ? num1 : num2

24

boolean isGood = true;

String mood = “”;if (isGood)

mood = “I'm Happy!”;else

mood = “I'm Sad!”;

boolean isGood = true;

String mood = isGood ? “I'm Happy!” : “I'm Sad!”;

25

자바의 연산자와 연산의 과정

연산의 과정

26

for loop

27

for 반복문의 실행흐름

실행결과

28

29

public class ForLoop {

public static void main(String[] args) {

int sum = 0;

for (int i = 1; i <= 10; i++)

sum = sum + i;

System.out.println(" 합 = " + sum);

}

}

합 = 55

30

들여쓰기

31

int i = 0;

while (i < 10) {

if (i < 5)

System.out.println(i);

i = i + 1;

}

int i = 0;

while (i < 10)

{

if (i < 5)

System.out.println(i);

i = i + 1;

}

32

int i = 0;

while (i < 10) {

if (i < 5)

System.out.println("L");

else if (i < 10)

System.out.println("M");

else

System.out.println("H");

i = i + 1;

}

int i = 0;

while (i < 10) {

if (i < 5)

System.out.println("L");

else if (i < 10)

System.out.println("M");

else

System.out.println("H");

i = i + 1;

}

33

int i = 0;

while (i < 10) {

if (i < 5)

System.out.println("L");

else if (i < 10)

System.out.println("M");

else

System.out.println("H");

i = i + 1;

}

int i = 0;

while (i < 10) {

if (i < 5)

System.out.println("L");

else if (i < 10)

System.out.println("M");

else

System.out.println("H");

i = i + 1;

}

34

int i = 0;

while (i < 10) {

if (i < 5)

System.out.println("L");

else if (i < 10)

System.out.println("M");

else

System.out.println("H");

i = i + 1;

}

int i = 0;

while (i < 10) {

if (i < 5)

System.out.println("L");

else if (i < 10)

System.out.println("M");

else {

System.out.println("H");

System.out.println("H");

}

i = i + 1;

}

35

int i = 1;

if (i > 0) {

if (i < 5)

System.out.println("L");

else if (i < 10)

System.out.println("M");

else

System.out.println("H");

}

int i = 1;

if (i > 0)

if (i < 5)

System.out.println("L");

else if (i < 10)

System.out.println("M");

else

System.out.println("H");

if… else if… else 는 한 문장 !

36

문자열 내 특수문자Special charac-ters

Display

\' Single quotation mark

\" Double quotation mark

\\ Backslash

\t Tab

\b Backspace

\r Carriage return

\f Formfeed

\n Newline

37

\' Single quotation mark

\" Double quotation mark

\\ Backslash

\t Tab

\b Backspace

\r Carriage return

\f Formfeed

\n Newline

System.out.println(“ 오늘 “대박” 났다 .”); // 컴파일 에러System.out.println(“ 오늘 \“ 대박 \” 났다 .”); 오늘 “대박” 났다 .

System.out.println(“ 강원대 \n 홍길동” ); 강원대 홍길동

38