1 / 45

Chapter 5

Chapter 5. Selection-- Making Decision. 지금까지 배운 제어 (control) 흐름은 순서대로 (composition) 수행하는 것이었다 . 정상적 흐름을 벗어나 어떤 상황에서 판단을 하여 다른 흐름을 만드는 방법이 선택 (selection) 이다 . 선택은 논리적 판단에 기반한다 . 그러나 C 에는 논리형 (logical type) 이 없다 . 이에 따라 모든 비트가 ‘0’ 이면 ‘false’, 아니면 ‘true’ 로 보는 방법을 취한다 .

leola
Download Presentation

Chapter 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5 Selection-- Making Decision

  2. 지금까지 배운 제어(control)흐름은 순서대로(composition) 수행하는 것이었다. 정상적 흐름을 벗어나 어떤 상황에서 판단을 하여 다른 흐름을 만드는 방법이 선택(selection)이다. 선택은 논리적 판단에 기반한다. 그러나 C에는 논리형(logical type)이 없다. 이에 따라 모든 비트가 ‘0’이면 ‘false’, 아니면 ‘true’로 보는 방법을 취한다. 정수, 실수 등에서는 0, 문자에서는 ‘\0’가 false에 해당한다. Selection

  3. Figure 5-1

  4. not(!) > and(&&) >or(||)이 있음. 조심: ‘&’과 ‘|’은 추후에 배울 비트(bit)연산자이다. 그 외에 ‘^’(exclusive or)와 ‘~’(one’s complement)연산이 있다. --- 15장에서 배움 ‘!’은 ‘true’ 값은 0으로, ‘false’ 값은 1로 바꾼다. !7, !90, !1.2, !’a’ 논리연산자(logical operator)

  5. Figure 5-2

  6. ‘and’나 ‘or’의 구현방법 하위 expression을 모두 evaluation한 후 수행 왼쪽부터 evaluation하여 값을 알게 되면 더 이상 수행하지 않는 방법 --- short circuit 속도가 빠르고, 때에 따라서는 모든 하위식을 evaluation하면 수행될 수 없는 문장도 수행이 된다. true || …, false && …. ~a && (b/a>=1.0) x || y++ Short circuit

  7. Figure 5-3

  8. 두 값의 관계를 비교하여 논리적 결과를 생성하는 연산자 less than(<), less than or equal(<=), greater than(>), greater than or equal(>=) > equal(==), not equal(!=) 보완관계: !(x < y)  x >= y !(x >= y)  x < y !(x != y)  x == y !(x == y)  x != y 관계연산자(relational operator)

  9. Figure 5-4

  10. Figure 5-5

  11. 조건문의 결과에 따라 제어흐름이 바뀜 if (조건문) …. else if (x>=0) …. else …. ; 선택문

  12. Figure 5-6

  13. Figure 5-7

  14. Figure 5-8

  15. Figure 5-9

  16. Figure 5-10

  17. Figure 5-11

  18. Figure 5-12

  19. Figure 5-13

  20. if문 안에 if문이 있고, ‘else’가 하나밖에 없을 때 … 가까운 ‘if’에 연결 벗어나려면 if (..) {if (…) } else … 로 처리 Dangling else problem

  21. Figure 5-14

  22. Figure 5-15

  23. 3-nary operation expression ? expression1 : expression2 expression의 evaluation 결과가 ‘true’이면 ‘expression1’이 수행되고, 아니면 ‘ecpression2’가 수행된다. 예: a == b ? c-- : c++ If문과 비슷하지만 쓰임이 다름 numPerLine = (fileFlag == ‘M’ ? 10 : 15) 중첩해서 사용하지 않음이 바람직함. 조건식(conditional expression)

  24. Figure 5-16

  25. void exit(int completionStatus) 프로그램의 종료 (0이 아닌 값) 운영체제에 종료이유를 알림 void abort(void) 비정상적인 프로그램의 종료 GIGO(garbage-in garbage-out)

  26. 소득세 누진 적용 $10,000  2% $10,001 ~ $20,000  5% $20,001 ~ $30,000  7% $30,001 ~ $50,000  10% $50,001 ~  15% 이중선택, 중첩

  27. Figure 5-17

  28. Figure 5-18 Program Calculate Taxes Calculate taxes based on marginal tax brackets. 1 Get income data (total income, taxes paid, dependencies) 2 Calculate taxes (total tax, tax due) 3 Print information End Calculate Taxes ==================== calcTaxes ================== calcTaxes 1 taxable income = total income – dependent exemptions 2 total tax = tax for bracket 1 + tax for bracket 2 + tax for bracket 3 + tax for bracket 4 + tax for bracket 5 3 tax due = total tax – taxes paid End calcTaxes

  29. switch문 정수, enumeration형 등에 사용 특정 값일 때만 적용 default : 꼭 있어야 하지는 않지만 선택되지 않은 모든 경우를 나타냄 break: switch문 중간에서 빠져나갈 때 사용 if-else if 문 특정 값에 따른 선택을 할 때 실수값에 주로 사용하나, 정수에도 사용 가능 특정 범위 내의 값일 때 선택 switch문을 정수에 사용하지 않는 이유 오차 때문 실수에서 if(a==b/c)보다는 if(fabs(a-b/c)<0.0001) 다중선택(multiway selection)

  30. Figure 5-19

  31. Figure 5-20

  32. Figure 5-21

  33. Figure 5-22

  34. Figure 5-23

  35. 일종의 nested-if문임. if (score >= 90) grade = ‘A’; else if (score >= 80) grade = ‘B’; else if (score >= 70) grade = ‘C’; else if (score >= 60) grade = ‘D’; else grade = ‘F’; else-if

  36. Figure 5-24

  37. int is… (int testChar) iscntrl (0…31, 127), isprint(32…126) isspace (space: 32, vTab(9), line feed(10), hTab(11), form feed(12), carriage return(13) isgraph, isalnum, ispunc, isalpha, islower, isupper, isdigit, isxdigit int to… (int oldchar) toupper, tolower 표준라이브러리(추가)

  38. Figure 5-25

  39. 잘 조직화한 프로그램 작성, 쉬운 프로그램 보기 좋게 프로그램…. 중첩되면 한 칸씩 들여쓰기 비교시 복잡한 부정문 사용을 줄임 사고의 흐름에 따라 선택규칙을 작성 충분히 생각하고 프로그램을 한다. Software Engineering 잘 조직화하여 프로그램을 작성

  40. Figure 5-26

  41. Figure 5-27

  42. Figure 5-28

  43. Figure 5-29

  44. Figure 5-30

  45. 실험 Exclusive or의 truth table 만들기 추가된 표준라이브러리 중에서 3개 구현 (toupper포함) 55번, 60번, 62번 예습 6장 16, 17, 20, 24번 예습 및 실험

More Related