1 / 42

הרצאה 3

הודעות: http://www.jct.ac.il/~richards/intro-program.htm או דרך moodle – ושם לפנות ל: אתר מכון טל החומר המועלה ל moodle ישירות נועד למכון לב. יש להגיש תרגילים רק למייל הרשום בתרגיל ולא דרך המודל מועד ההגשה של כל תרגיל הוא יום שני לאחר קבלת הרגיל עד חצות (אלא אם כן נאמר אחרת ).

afi
Télécharger la présentation

הרצאה 3

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. הודעות: http://www.jct.ac.il/~richards/intro-program.htm או דרך moodle – ושם לפנות ל: אתר מכון טל החומר המועלה לmoodle ישירות נועד למכון לב. יש להגיש תרגילים רק למייל הרשום בתרגיל ולא דרך המודל מועד ההגשה של כל תרגיל הוא יום שני לאחר קבלת הרגיל עד חצות (אלא אם כן נאמר אחרת ). מועדי הגשה המפורטים ב moodle אינם רלונטיםלמכון טל מבוא למדעי המחשב לתעשייה וניהול הרצאה 3

  2. מפעל השעווה - לוגיקה ומשפטי תנאי • עד עכשיו טיפלנו בייצור נרות מסוג אחד, במחיר אחיד • המערכת הוגדרה באופן סדרתי (יש מסלול בודד) • המחיר חושב באופן אחיד לכל הזמנה: מספר נרות * מחיר לנר • נניח שרוצים לתת הנחה במקרה שההזמנה גדולה ?

  3. זרימת הבקרה • בהעדר הוראה אחרת התוכנית זורמת לפי סדר, • פקודה אחר פקודה • יש פקודות שמאפשרות לנו להחליט • אם לבצע פקודה או לא • לבצע פקודות שוב ושוב • ההחלטה מתבצעת על סמך ביטויים או תנאים בוליאניים שערכם אמת (true) או שקר (false) • סדר ביצוע הפקודות נקרא זרימת הבקרה(flow of control)

  4. זרימת הבקרה ללא פיצולים התחלת התוכנית הצהרה (statement) הצהרה (statement) הצהרה (statement) הצהרה (statement) סוף התוכנית

  5. If, Else

  6. הביטוי (condition)חייב להיות ביטוי בוליאני – כלומר ערכו אמת - true או שקר - false If, Then, End If מילים שמורות אם הביטוי (condition)אמת (true)מבוצעת ההצהרה (statement) אם הביטוי (condition)שקר (false) מדלגים על ההצהרה (statement) פקודת If • פקודת Ifנכתבת באופן הבא: If condition Then statement End If

  7. הביטוי נבדק False שקר הלוגיקה של פקודת If true אמת הצהרה מבוצעת (statement)

  8. ההצהרה - statement הביטוי - condition דוגמאות לשימוש בIf ModuleModule1 Sub Main() Dim age AsInteger Console.WriteLine("Please enter your age") age = Console.ReadLine() Ifage > 18 Then Console.WriteLine("You are an adult") EndIf Console.ReadKey() EndSub EndModule

  9. ביטויים בוליאניים • בביטוי שבפקודת if משתמשים הרבה פעמים באופרטורים של השוואה המחזירים ערך בוליאני = equal to (שווה) <> not equal to (לא שווה) < less than (קטן מ) > greater than (גדול מ) <= less than or equal to (קטן או שווה ל) >= greater than or equal to (גדול או שווה ל)

  10. הביטוי - condition דוגמאות 2 לשימוש בIf ModuleModule1 Sub Main() Dim lamp AsBoolean = True If lamp Then Console.WriteLine("The lamp is on") EndIf Console.ReadKey() EndSub EndModule

  11. הזחה - אינדנטציה • הקשר בין ההצרה של פקודת ifלהצהרה יש לבצע אינדנטציה • אינדנטציהמסייעת לקריאות של התוכנית Iflamp Then Console.WriteLine("The lamp is on") Console.WriteLine(“blabla") EndIf • אינדנטציה היא חסרת משמעות לקומפיילר אך קריטית למתכנת If lamp Then Console.WriteLine("The lamp is on") EndIf Console.WriteLine(“blabla")

  12. מפעל השעווה - לוגיקה ומשפטי תנאי • נניח שרוצים להודיע שתינתן הנחה במקרה שההזמנה גדולה. ModuleModule1 Sub Main() DimnumCandles AsInteger = 4400 Console.WriteLine("Please enter the amount of candles") numCandles = Console.ReadLine() IfnumCandles > 100 Then Console.WriteLine("you get a discount") EndIf Console.ReadKey() EndSub EndModule

  13. הביטוי נבדק (condition) הלוגיקה של פקודת If, Else If condition Then statement1 Else statement2 End If true אמת false שקר הצהרה מבוצעת (statement 1) הצהרה מבוצעת (statement 2)

  14. דוגמא לפקודת Else ModuleModule1 SubMain() DimnumCandles AsInteger = 4400 Console.WriteLine("Please enter the amount of candles") numCandles= Console.ReadLine() IfnumCandles > 100 Then Console.WriteLine("You get a discount") Else Console.WriteLine("No discount for you...") EndIf Console.ReadKey() EndSub EndModule

  15. שימוש בתיבת הודעה - InputBox • ראינו שימוש בתיבת MsgBox לפלט • ישנו גם InputBox המשמש לקלט ModuleModule1 Sub Main() DimnumAsInteger num = InputBox("please enter a number") Ifnum > 10 Then MsgBox("number is bigger than 10") Else MsgBox("number is smaller than 10") EndIf EndSub EndModule

  16. אופרטורים לוגיםAnd, Or, Not

  17. מפעל השעווה – "גם" "או" "היפוך" • שילוב מספר תנאים יחד? • הזמנה קטנה של נרות אדומים • קטן וגם אדום • הזמנה שאין בה נרות פשוטים • היפוך של פשוט • הזמנה שיש בה כמות גדולה של נרות או נרות מדגם מיוחד • גדול או מיוחד ....

  18. אופרטורים לוגיים • לכל ביטוי בוליאני יש ערך אמת -TRUE או שקר - FALSE • ביטוי אמת מחזיר את הערך 1 וביטוי שקר מחזיר 0 • נשלב שני ביטויים בוליאניים בעזרת "וגם" And "או" Or "היפוך" Not • למשל אם A ו B הם ביטויים בוליאניים הביטוי A And B גם כן בוליאני. • ערך הביטוי המשולב תלוי בחלקים ובאופרטור השילוב • דוגמא: • A הוא הביטוי age < 18 • B הוא הביטוי age > 6 • משמעות הביטוי A And B היא שהגיל קטן מ 18 וגדול מ 6.

  19. "וגם" - AND True False False False

  20. דוגמא לשימוש ב And ModuleModule1 SubMain() Dim age AsInteger Console.WriteLine("Please enter your age") age = Console.ReadLine() If age > 6 And age < 18 Then Console.WriteLine("Go to school") EndIf Console.ReadKey() EndSub EndModule

  21. "או" - OR True True True False

  22. דוגמא לשימוש ב Or ModuleModule1 SubMain() Dim age AsInteger Console.WriteLine("Please enter your age") age = Console.ReadLine() If age < 6 Or age > 18 Then Console.WriteLine("no school for you!") EndIf Console.ReadKey() EndSub EndModule

  23. "היפוך" - NOT False True

  24. דוגמא לשימוש ב Not ModuleModule1 SubMain() Dim age AsInteger Console.WriteLine("Please enter your age") age = Console.ReadLine() IfNot (age = 100) Then Console.WriteLine("very young...") EndIf Console.ReadKey() EndSub EndModule

  25. וכמובן שאפשר להרכיב.... ModuleModule1 SubMain() Dim numCandles, x AsInteger x = 5 Console.WriteLine("Please enter the amount of candles") numCandles = Console.ReadLine() IfnumCandles > 100 AndnumCandles < 500 And x = 2 Then Console.WriteLine("You get a discount") Else Console.WriteLine("No discount for you...") EndIf Console.ReadKey() EndSub EndModule

  26. אופרטורים לוגיים - סיכום • בביטויים בוליאניים אפשר להשתמש גם באופרטורים לוגיים. And, Or, Not • אופרטור וגם ( AND) מחזיר אמת אם שני הביטויים משני הצדדים של האופרטור אמת • אופרטור או ( OR ) מחזיר אמת אם אחד מהביטויים משני הצדדים של האופרטור אמת • אופרטור שלילה הלוגי ( NOT ) הופך את ערכו הלוגי של ביטוי הלוגי מימינו.

  27. ElseIf

  28. פקודת ElseIf • פיצול ליותר משני מקרים עושים בעזרת ElseIf If condition Then statement1 ElseIfcondition statement2 ElseIfcondition statement3 . . . Else statement-n End If

  29. ניחוש צבעים – מה יקרה פה? מה רוצים שיקרה פה? ModuleModule1 SubMain() Dimcolor AsString = "red" 'Dim color As String = "purple" If color = "red"Then Console.WriteLine("The color is red") EndIf If color = "green"Then Console.WriteLine("The color is green") EndIf Console.WriteLine("I dont know this color") EndSub EndModule

  30. ניחוש צבעים – הצעה לשיפור ModuleModule1 SubMain() Dimcolor AsString = "red" 'Dim color As String = "purple" If color = "red"Then Console.WriteLine("The color is red") ElseIf color = "green"Then Console.WriteLine("The color is green") Else Console.WriteLine("I dont know this color") EndIf EndSub EndModule

  31. ציונים – צריך עזרה? ModuleModule1 SubMain() Dimgrade AsInteger grade = Console.ReadLine() If (grade >= 90) Then Console.WriteLine("you got an A") EndIf If (grade >= 80) Then Console.WriteLine("you got an B") EndIf If (grade >= 70) Then Console.WriteLine("you got an C") EndIf If (grade >= 60) Then Console.WriteLine("you got an D") EndIf If (grade < 60) Then Console.WriteLine("you failed") EndIf EndSub EndModule

  32. ציונים – פתרון אפשרי ModuleModule1 SubMain() Dim grade AsInteger grade = Console.ReadLine() If (grade >= 90 And grade <= 100) Then Console.WriteLine("you got an A") EndIf If (grade >= 80 And grade < 90) Then Console.WriteLine("you got an B") EndIf If (grade >= 70 And grade < 80) Then Console.WriteLine("you got an C") EndIf If (grade >= 60 And grade < 70) Then Console.WriteLine("you got an D") EndIf If (grade < 60) Then Console.WriteLine("you failed") EndIf EndSub EndModule

  33. ציונים – פתרון משופר ModuleModule1 SubMain() Dim grade AsInteger grade = Console.ReadLine() If (grade >= 90) Then Console.WriteLine("you got an A") ElseIf (grade >= 80) Then Console.WriteLine("you got an B") ElseIf (grade >= 70) Then Console.WriteLine("you got an C") ElseIf (grade >= 60) Then Console.WriteLine("you got an D") Else Console.WriteLine("you failed") EndIf EndSub EndModule

  34. Ifמקונן (nested If)

  35. שימוש ב if בתוך if Dimnum1, num2, num3, min AsSingle min = 0 Console.WriteLine("Enter three numbers: ") num1 = Console.ReadLine() num2 = Console.ReadLine() num3 = Console.ReadLine() If num1 < num2 Then If num1 < num3 Then min = num1 Else min = num3 EndIf Else If (num2 < num3) Then min = num2 Else min = num3 EndIf EndIf Console.WriteLine("Minimum value: " & min)

  36. מה קורה כאן? Dimx, y AsString x = Console.ReadLine() If(x = "A") Then Console.WriteLine("Now enter something else") y = Console.ReadLine If (y = "B") Then Console.WriteLine("Good") Else Console.WriteLine("Bad") EndIf Else Console.WriteLine("Not Defined") EndIf

  37. פונקציות מתמטיות

  38. פונקציות מתמטיות • על מנת להשתמש בפונקציות מתמטיות אפשר להשתמש בחבילה בה מוגדרות פונקציות אלו. • יש לכתוב בתחילת התוכנית (לפני Module Module1) Imports System.Math • הפונקציות הקיימות הן: • Abs – ערך מוחלט • Pi – הערך של π • Sqrt – שורש ריבועי • Pow – מעלה בחזקה • Sgn– הסימן של המספר • Cos, Exp, Log, Sin, Tan • אפשר גם לא לעשות Imports System.Math, ואז לקרוא לפונקציות • Math.Pi, Math.Sqrt וכו'

  39. דוגמא - פונקציות מתמטיות 'Imports System.Math ModuleModule1 Sub Main() Dim x AsDecimal = Math.Abs(-10.4) Dim y AsDecimal Console.WriteLine("I will find the square root of a number") y = Console.ReadLine() Console.WriteLine("The absolute value is " & x) Console.WriteLine("The value of PI is " & Math.PI) Console.WriteLine("The square root of " & y & " is " & Math.Sqrt(y)) Console.WriteLine("2 to the 4.5 power is " & Math.Pow(2, 4.5)) Console.ReadKey() EndSub EndModule

  40. Select Case

  41. Select Case – לבחירה בין מספר ערכים Dimletter AsChar Console.WriteLine("Please enter a letter:") letter = Console.ReadLine() SelectCase letter Case"a" Console.WriteLine("you selected the first letter") Case"b" Console.WriteLine("you selected the second letter") Case"c" Console.WriteLine("you selected the third letter") Case"z" Console.WriteLine("you selected the last letter") Case Else Console.WriteLine("you selected a regular letter") EndSelect

  42. Select Case – דוגמא Dim number AsInteger = 8 SelectCase number Case 1 To 5 Console.WriteLine("Between 1 and 5, inclusive") Case6, 7, 8 Console.WriteLine("Between 6 and 8, inclusive") Case9 To 10 Console.WriteLine("Equal to 9 or 10") Case Else Console.WriteLine("Not between 1 and 10, inclusive") EndSelect

More Related