מבוא לשפת C תרגול 4: ביטויים לוגיים ומשפטי תנאי מבוסס על השקפים שחוברו ע"י שי ארצי, גיתית רוקשטיין, איתן אביאור וסאהר אסמיר עבור הקורס "מבוא למדעי המחשב"

Embed Size (px)

Text of מבוא לשפת C תרגול 4: ביטויים לוגיים ומשפטי תנאי מבוסס על...

  • Slide 1
  • C 4: " , , " " " , " .
  • Slide 2
  • 4 . 2 if
  • Slide 3
  • 4 . 3 : * -Turbo C . ! , int 4 . / "* sizeof %d %c1 char %d2 int %ld4 long int %f4 float %lf8 double
  • Slide 4
  • 4 . 4 char: ? : char ( ) -128 -127. -256 ASCII. char %c, . char %c, scanf , ASCII . (+, -, *,/,%, / %d ') char !
  • Slide 5
  • 4 . 5 char , : char ch = 'B'; ch "", ASCII B. ch %c B : printf("Numeric value is %d, character is %c", ch, ch); : Numeric value is 66, character is B !
  • Slide 6
  • 4 . 6 C " ". , . : % - , ( !). ++ - ( , ) -- - (") : +=, -=, ' x+=y; ;x=x+y, '
  • Slide 7
  • 4 . 7 : - . cast type-cast. " . : (type)expression
  • Slide 8
  • 4 . 8 - ? int a = 1,b = 2; double result = (double) a / b; 1. cast , a -int -double. 2. , . b - int -double. 3. : 1.0/2.0=0.5. 4. double double. .
  • Slide 9
  • 4 . 9 -BUG : #include int main(void) { char c="Y"; int price = 1999999; double half_price = (double)(price/2); printf("UPPER CASE: %c, lower case: %c\n", c, c-A+a); printf("Half price of 1,999,999 is %.2lf\n", half_price); return 0; } ? ?
  • Slide 10
  • Slide 11 54, '', ''." - , : (, " x -100") (, " x>54") (, " x>54 homework>60")">
  • 4 . 11 ? , . , " x>54, '', ''." - , : (, " x -100") (, " x>54") (, " x>54 homework>60")
  • Slide 12 54 true x -54, false . true, "". . C : 0 false. -0 (, 1) true.">
  • 4 . 12 . true false ("" ""). , x>54 true x -54, false . true, "". . C : 0 false. -0 (, 1) true.
  • Slide 13
  • 4 . 13 : , 1 (, true). , 0 (, false). , ? int x = 79; int passing = (x > 54); printf(Passing status: %d\n, passing); , . "" .
  • Slide 14
  • 4 . 14 ... (, x -54). , . , : x + 5 > 3 * y 35 > 42 x < x + 1 ... ? !
  • Slide 15 = y ( , x y) --: x 0 temp54, '', ''." C if. " ".
  • Slide 26
  • 4 . 26 if if : if (expression) statement; expression ... , if. , statement. , statement if. : if (x < 55) printf(You have failed!); -1 0. -if !
  • Slide 27
  • 4 . 27 double temp; int is_liquid; printf(Enter temprature: ); scanf(%lf, &temp); is_liquid = (0 < temp) && (temp < 100); if (is_liquid) printf(In liquid state.); if (temp < 0) printf(Frozen.); if (temp > 100) printf(Boiled.);
  • Slide 28
  • 4 . 28 int grade; printf(Enter test grade: ); scanf(%d, &grade); if (grade < 55) printf(Failed.); if (grade >= 55) printf(Passed.); ( grade 55) : , , . (... ...) .
  • Slide 29
  • 4 . 29 ... ... , C . : if (expression) statement 1 ; else statement 2 ; expression , statement 1. , statement 2. ( ) !
  • Slide 30
  • 4 . 30 ... int grade; printf(Enter test grade: ); scanf(%d, &grade); if (grade < 55) printf(Failed.); else printf(Passed.); , . !
  • Slide 31
  • 4 . 31 if , ifelse , . , : if (grade < 55) printf(Failed.); else printf(Passed.); -statement (indentation). : , ; .
  • Slide 32
  • 4 . 32 , " ", . , -, . . , : , ! ...
  • Slide 33
  • 4 . 33 , . , ? int grade = 25; printf(The grade is %d\n, grade); if (grade > 54) printf(Passed.\n); printf(Very good!\n); !?
  • Slide 34 54) printf(Passed.\n); printf(Very good!\n); -printf -if!">
  • 4 . 34 ... , " ". , : int grade = 25; printf(The grade is %d\n, grade); if (grade > 54) printf(Passed.\n); printf(Very good!\n); -printf -if!
  • Slide 35 54) { printf(Passed.\n); printf(Very good!\n); }">
  • 4 . 35 , if? : . , "" statements , -if. : int grade = 25; printf(The grade is %d\n, grade); if (grade > 54) { printf(Passed.\n); printf(Very good!\n); }
  • Slide 36
  • 4 . 36 -else... if (grade > 54) { printf(Passed.\n); printf(Very good!\n); } else { printf(Failed.\n); printf(What a pity!\n); } "".
  • Slide 37
  • 4 . 37 ... . , . : if (grade < 55) { printf(Failed.); } else { printf(Passed.); }
  • Slide 38
  • 4 . 38 : if (grade < 55) { printf(Failed.); } else { printf(Passed.); } , . !
  • Slide 39
  • 4 . 39 grade=40? if (grade < 55) if (grade > 52) grade = 55; /* factor */ else printf(Passed without factor.); else -if ! Passed without factor. . . ! -C
  • Slide 40 = 53) grade = 55; } else printf(Passed without factor.); Passed without factor. grade>=55.">
  • 4 . 40 -else -if ? " : if (grade < 55) { if (grade >= 53) grade = 55; } else printf(Passed without factor.); Passed without factor. grade>=55.
  • Slide 41 10000) { if (airspeed < 100) { printf(Warning! Crash danger!); } !">
  • 4 . 41 . 10,000 , -100 ", . ? if (altitude > 10000) { if (airspeed < 100) { printf(Warning! Crash danger!); } !
  • Slide 42
  • 4 . 42 , visibility . - ( ). , . ? ? if (visibility == 0) { turn_on_red_light(); }