1 / 27

Flow of Control: Relational, Equality, and Logical Operators

Learn about the flow of control in programming, including relational, equality, and logical operators. Understand operator precedence and associativity. Explore examples and values of relational expressions.

calvo
Download Presentation

Flow of Control: Relational, Equality, and Logical Operators

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. 6주 강의 Flow of Control

  2. Relational, Equality, and Logical Operators

  3. Operator precedence and associativity

  4. Relational Operators and Expressions • Relational_expression ::= expr < expr | expr > expr | expr <= expr | expr >= expr • a < 3, a < b < c, -1.3>= (2.0 * x + 3.3) • a =< b, a < = b /* errors */ • a – b < 0  (a-b) < 0

  5. Values of relational expressions

  6. Examples

  7. 예제 설명 • 3 < j < 5 ::: 수학적 해석, C의 해석 j가 7이면 3<7  1, 1<5  1 (incorrect) • (3 < j) && (j < 5) • x < x+y  0<y 하지만 x가 크고 (7e+33), y가 작으면 (0.001)  ‘false(0)’이 될 수 있다  (x – (x+y)) < 0.0 • fabs(x-y) < 0.001와 (x-y) == 0

  8. Logical operators and Expressions • logical_negation_expression ::= ! expr • !a, !(x+7.7), !(a<b || c<d) • !0  1, others  0 • !!5  !(!5)

  9. 예제

  10. 예제 • a && b, a || b, !(a<b) && c, 3 && (a+b)

  11. 예제

  12. Short-circuit Evaluation • &&, || :: the evaluation process stops as soon as the outcome true or false • expr1 && expr2  expr1 || expr2 • int cnt = 0; while (++cnt <= 3 && (c=getchar() != EOF { ……. /* do something */  앞 부분이 false일 때

  13. The Compound Statement • compound_statement ::= { {declaration)0+ {statement}0+ } • 예 { a=1; { int b; b=2; c=3; } }

  14. The expression and empty expression • expr_statement ::= {expr}opt • a =b; a+b+c; /* legal but not useful */ ; /* an empty statement */ printf(“%d\n”, a); • for(i=1; I<=100; ++i) {} ;

  15. The if and if-else statement • if_statement ::= if (expr) statement | if (expr) statement else statement • if (grade >= 90) mark = ‘A’; • If (grade >= 60) pass = 1; else pass = 0; • 160, 161, 162 예제 설명

  16. The while statement • while_statement ::= while (expr) statement • while ( i++ < n) factorial *=i; • 예제 설명 (page 165, 166)

  17. The for statement • for_statement ::= for (expr; expr; expr) statement • for (i=1; i<=n; ++i) factorial *= i; • 168 page 그림 설명 • 169 page 설명 (실제 돌려보고 검사 :::  강의 끝나고 ; 숙제 AND, OR, XOR 표를 구한다…입력은 3개 변수 ::: A, B, C)

  18. The comma operator • comma_expression ::= expr , expr • expr1, expr2 expr1이 먼저 수행되고, expr2기 수행됨, 결과는 오른쪽의 형과 값을 가짐 • for (sum =0, i=1; i <= n; ++i) sum+=i;  for (sum =0, i=1; i <= n; sum+=i, ++i) ; /= for (sum =0, i=1; i <= n; ++i, sum+=i) ;

  19. 예제

  20. The do statement • do_statement ::= do statement while (expr) ; • i=sum=0; do { sum +=i; scanf(“%d”, &i); } while (i>0); • 174page 설명

  21. Fibonacci Numbers • F0 =0, f1=1, fi+1 = fi + fi-1 for i>=1 • 프로그램 개요설명 • 175page 프로그램 설명 temp = f1; f1 += f0; f0 = temp

  22. The goto statement • labeled_statement ::= label : statement label ::= identifier • bye: exit(0) ; L444: a=b+c; bug1: bug2: bug3: printf(“bug found\n”); ……. goto bye; …….

  23. The break and continue Statement • break; continue; • while(1) { scanf(“%lf”, &x); if (x <0.0) break; printf(“%f\n”, sqrt(x)); } • for (i=0; i<TOTAL;++i) { c=getchar(); if(c>= ‘0’ && c<= ‘9’) continue ….. }

  24. The switch statement • switch (c) { case ‘a’ : ++a_cnt; break; case ‘b’: case ‘B’: ++b_cnt; break; default : ++other_cnt; }

  25. The conditional operator • condotional_expression ::= expr ? expr : expr • if (y>z) x=y; else x=z;  x= (y<z) ? Y : z;

  26. 예제

  27. 숙제 • 수업시간 해결 ::: 6, 8, 9, 20, 25, 29 • 집에서 ::: 2, 3, 7, 12, 15, 22, 23, 26, 30, 31, 38

More Related