270 likes | 279 Views
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.
E N D
6주 강의 Flow of Control
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
예제 설명 • 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
Logical operators and Expressions • logical_negation_expression ::= ! expr • !a, !(x+7.7), !(a<b || c<d) • !0 1, others 0 • !!5 !(!5)
예제 • a && b, a || b, !(a<b) && c, 3 && (a+b)
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일 때
The Compound Statement • compound_statement ::= { {declaration)0+ {statement}0+ } • 예 { a=1; { int b; b=2; c=3; } }
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) {} ;
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 예제 설명
The while statement • while_statement ::= while (expr) statement • while ( i++ < n) factorial *=i; • 예제 설명 (page 165, 166)
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)
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) ;
The do statement • do_statement ::= do statement while (expr) ; • i=sum=0; do { sum +=i; scanf(“%d”, &i); } while (i>0); • 174page 설명
Fibonacci Numbers • F0 =0, f1=1, fi+1 = fi + fi-1 for i>=1 • 프로그램 개요설명 • 175page 프로그램 설명 temp = f1; f1 += f0; f0 = temp
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; …….
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 ….. }
The switch statement • switch (c) { case ‘a’ : ++a_cnt; break; case ‘b’: case ‘B’: ++b_cnt; break; default : ++other_cnt; }
The conditional operator • condotional_expression ::= expr ? expr : expr • if (y>z) x=y; else x=z; x= (y<z) ? Y : z;
숙제 • 수업시간 해결 ::: 6, 8, 9, 20, 25, 29 • 집에서 ::: 2, 3, 7, 12, 15, 22, 23, 26, 30, 31, 38