basis and practice in programming lecture notes (7/19)

27
1 반복문과 분기문 Prof. Seungjoo Kim Security Analysis aNd Evaluation Lab. Department of Cyber Defense Korea University http://www.KimLab.net

Upload: seungjoo-kim

Post on 12-Apr-2017

151 views

Category:

Engineering


2 download

TRANSCRIPT

Page 1: Basis and Practice in Programming Lecture Notes (7/19)

1

반복문과 분기문

Prof. Seungjoo Kim Security Analysis aNd Evaluation Lab.

Department of Cyber Defense Korea University

http://www.KimLab.net

Page 2: Basis and Practice in Programming Lecture Notes (7/19)

2

다양한 형태의 반복문

Page 3: Basis and Practice in Programming Lecture Notes (7/19)

3

반복문의 기능 특정 영역을 특정 조건이 만족하는 동안에 반복 실행하기 위한 문장

세 가지 형태의 반복문 while문에 의한 반복 do ~ while문에 의한 반복 for문에 의한 반복

반복문이란?

Page 4: Basis and Practice in Programming Lecture Notes (7/19)

4

while문의 기본 원리와 의미 예제 SimpleWhile.c 참조 예제 NineNineDan.c 참조

while문에 의한 문장의 반복

그림 7-1

그림 7-2

Page 5: Basis and Practice in Programming Lecture Notes (7/19)

5

while 문의 중괄호 반복하고자 하는 영역이 둘 이상의 문장으로 구성되는 경우에 필수

무한 루프(반복) 반복의 조건으로 true가 오면 발생

while문에 의한 문장의 반복

while(i<10) printf("Hello World! \ n"), i++;

while( 1 ) // 반복의 조건 대신 0 이 아닌 정수를 넣는다. { printf("Hello World! \ n"); i++; }

Page 6: Basis and Practice in Programming Lecture Notes (7/19)

6

while 문의 중첩 while문 안에 while문을 포함시킨다는 뜻 반복 구조 내에서 또 하나의 반복 구조 형성 예제 nested_while.c, TwoToNine.c 참조

while문에 의한 문장의 반복

int i=0, j=0; int num=0; while(i<10) { while(j<10) { num++; j++; } i++; j=0; }

Page 7: Basis and Practice in Programming Lecture Notes (7/19)

7

while문의 순서도

while문에 의한 문장의 반복

그림 7-5

Page 8: Basis and Practice in Programming Lecture Notes (7/19)

8

do~while문과 while문의 차이점 do~while문은 일단 한번 실행하고 나서 조건 검사를 진행

do~while에 의한 문장의 반복

Page 9: Basis and Practice in Programming Lecture Notes (7/19)

9

do~while문의 순서도

do~while에 의한 문장의 반복

그림 7-9

Page 10: Basis and Practice in Programming Lecture Notes (7/19)

10

for문의 기본 원리와 의미 초기문, 조건문, 증감문 모두를 기본적으로 포함!

가장 많이 사용되는 반복문

for문에 의한 문장의 반복

Page 11: Basis and Practice in Programming Lecture Notes (7/19)

11

for문과 while문의 비교

for문에 의한 문장의 반복

그림 7-10

Page 12: Basis and Practice in Programming Lecture Notes (7/19)

12

반복 과정의 이해

for문에 의한 문장의 반복

그림 7-11

Page 13: Basis and Practice in Programming Lecture Notes (7/19)

13

조건에 따른 흐름의 분기

Page 14: Basis and Practice in Programming Lecture Notes (7/19)

14

상황에 따른 프로그램의 유연성 부여

흐름의 분기가 필요한 이유

그림 8-1

Page 15: Basis and Practice in Programming Lecture Notes (7/19)

15

if문에 의한 조건적 실행 조건이 만족되는 경우에 한해서 실행 IfBasic.c, CalOne.c, Mul3Mul4.c

if와 else

그림 8-2

Page 16: Basis and Practice in Programming Lecture Notes (7/19)

16

if~else 에 대해서 예제 CalOne.c의 단점: 불필요한 연산을 하게 된다.

IfElseBasic.c 참조

if와 else

그림 8-5

Page 17: Basis and Practice in Programming Lecture Notes (7/19)

17

if, else if, else에 대해서 CalTwo.c 참조

if와 else

그림 8-6

Page 18: Basis and Practice in Programming Lecture Notes (7/19)

18

if, else if, else에 대한 진실

if~else문은 하나의 문장이다. if~else문의 중첩된 형태에 지나지 않는다.

if와 else

Page 19: Basis and Practice in Programming Lecture Notes (7/19)

19

조건 연산자 (삼항 연산자) if~else문을 간결히 표현하는데 사용될 수 있다. ThreeOperand.c 참조

if와 else

Page 20: Basis and Practice in Programming Lecture Notes (7/19)

20

이제 그만 break!(탈출) 반복문을 빠져 나올 때 사용 WhenOver5000.c

다음으로 넘어가자 continue!(생략) 다음 번 반복으로 넘어갈 때 사용 ExceptMul2Mul3.c

continue & break

그림 8-6

Page 21: Basis and Practice in Programming Lecture Notes (7/19)

21

switch문의 구조 EnglishSchool.c

switch에 의한 선택 실행

그림 8-8 그림 8-7

Page 22: Basis and Practice in Programming Lecture Notes (7/19)

22

switch문에서 break문의 의미 AdvancedEnglishSchool.c

switch에 의한 선택 실행

Page 23: Basis and Practice in Programming Lecture Notes (7/19)

23

switch vs. if~else 1 분기의 경우 수가 많아지면 가급적 switch 문으로...

switch에 의한 선택 실행

그림 8-13

Page 24: Basis and Practice in Programming Lecture Notes (7/19)

24

switch vs. if~else 2 switch문에서는 비교 연산이 올 수 없다.

switch에 의한 선택 실행

그림 8-14

Page 25: Basis and Practice in Programming Lecture Notes (7/19)

25

프로그램의 흐름을 복잡하게 한다. 사용하지 말자! GoToBasic.c 참조

원하는 곳으로 보내주마 goto!

그림 8-15

Page 26: Basis and Practice in Programming Lecture Notes (7/19)

26

에드거 다익스트라 (Edsger W. Dijkstra, 1930~2002)

구조적 프로그래밍(Structured Programming)의 발견.

“goto 문장은 해롭다”라는 에세이 한편으로 구조적 프로그래밍의 새벽을 밝힘.

구조적 프로그래밍

Page 27: Basis and Practice in Programming Lecture Notes (7/19)

27

Q & A