Download presentation
Presentation is loading. Please wait.
Published byJayson Walters Modified over 9 years ago
1
הרצאה 3 מבוא למדעי המחשב לתעשייה וניהול הודעות : הודעות : http://www.jct.ac.il/~richards/intro-program.htm או דרך moodle – ושם לפנות ל : אתר מכון טל החומר המועלה ל moodle ישירות נועד למכון לב. יש להגיש תרגילים רק למייל הרשום בתרגיל ולא דרך המודל מועד ההגשה של כל תרגיל הוא יום שני לאחר קבלת הרגיל עד חצות ( אלא אם כן נאמר אחרת ). מועדי הגשה המפורטים ב moodle אינם רלונטים למכון טל
2
מפעל השעווה - לוגיקה ומשפטי תנאי עד עכשיו טיפלנו בייצור נרות מסוג אחד, במחיר אחיד המערכת הוגדרה באופן סדרתי ( יש מסלול בודד ) המחיר חושב באופן אחיד לכל הזמנה : מספר נרות * מחיר לנר נניח שרוצים לתת הנחה במקרה שההזמנה גדולה?
3
זרימת הבקרה בהעדר הוראה אחרת התוכנית זורמת לפי סדר, פקודה אחר פקודה יש פקודות שמאפשרות לנו להחליט אם לבצע פקודה או לא לבצע פקודות שוב ושוב ההחלטה מתבצעת על סמך ביטויים או תנאים בוליאניים שערכם אמת (true) או שקר (false) סדר ביצוע הפקודות נקרא זרימת הבקרה (flow of control)
4
זרימת הבקרה ללא פיצולים הצהרה (statement) הצהרה (statement) הצהרה (statement) הצהרה (statement) התחלת התוכנית סוף התוכנית
5
If, Else
6
פקודת If פקודת If נכתבת באופן הבא : If condition Then statement End If If, Then, End If מילים שמורות הביטוי (condition) חייב להיות ביטוי בוליאני – כלומר ערכו אמת - true או שקר - false אם הביטוי (condition) אמת (true) מבוצעת ההצהרה (statement) אם הביטוי (condition) שקר (false) מדלגים על ההצהרה (statement)
7
הלוגיקה של פקודת If הביטוי נבדק הצהרה מבוצעת (statement) true אמת False שקר
8
דוגמאות לשימוש בIf Module Module1 Sub Main() Dim age As Integer Console.WriteLine("Please enter your age") age = Console.ReadLine() If age > 18 Then Console.WriteLine("You are an adult") End If Console.ReadKey() End Sub End Module ההצהרה - statement הביטוי - condition
9
ביטויים בוליאניים בביטוי שבפקודת if משתמשים הרבה פעמים באופרטורים של השוואה המחזירים ערך בוליאני = equal to ( שווה ) <> not equal to ( לא שווה ) < less than ( קטן מ ) > greater than ( גדול מ ) <= less than or equal to ( קטן או שווה ל ) >= greater than or equal to ( גדול או שווה ל )
10
דוגמאות 2 לשימוש בIf Module Module1 Sub Main() Dim lamp As Boolean = True If lamp Then Console.WriteLine("The lamp is on") End If Console.ReadKey() End Sub End Module הביטוי - condition
11
הזחה - אינדנטציה הקשר בין ההצרה של פקודת if להצהרה יש לבצע אינדנטציה אינדנטציה מסייעת לקריאות של התוכנית If lamp Then Console.WriteLine("The lamp is on") Console.WriteLine(“bla bla") End If אינדנטציה היא חסרת משמעות לקומפיילר אך קריטית למתכנת If lamp Then Console.WriteLine("The lamp is on") End If Console.WriteLine(“bla bla")
12
מפעל השעווה - לוגיקה ומשפטי תנאי נניח שרוצים להודיע שתינתן הנחה במקרה שההזמנה גדולה. Module Module1 Sub Main() Dim numCandles As Integer = 4400 Console.WriteLine("Please enter the amount of candles") numCandles = Console.ReadLine() If numCandles > 100 Then Console.WriteLine("you get a discount") End If Console.ReadKey() End Sub End Module
13
הלוגיקה של פקודת If, Else הביטוי נבדק (condition) הצהרה מבוצעת (statement 1) true אמת הצהרה מבוצעת (statement 2) false שקר If condition Then statement1 Else statement2 End If
14
דוגמא לפקודת Else Module Module1 Sub Main() Dim numCandles As Integer = 4400 Console.WriteLine("Please enter the amount of candles") numCandles = Console.ReadLine() If numCandles > 100 Then Console.WriteLine("You get a discount") Else Console.WriteLine("No discount for you...") End If Console.ReadKey() End Sub End Module
15
שימוש בתיבת הודעה - InputBox ראינו שימוש בתיבת MsgBox לפלט ישנו גם InputBox המשמש לקלט Module Module1 Sub Main() Dim num As Integer num = InputBox("please enter a number") If num > 10 Then MsgBox("number is bigger than 10") Else MsgBox("number is smaller than 10") End If End Sub End Module 15
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 A And BBA True FalseTrue False True False
20
דוגמא לשימוש ב And Module Module1 Sub Main() Dim age As Integer Console.WriteLine("Please enter your age") age = Console.ReadLine() If age > 6 And age < 18 Then Console.WriteLine("Go to school") End If Console.ReadKey() End Sub End Module
21
"או" - OR A Or BBA True FalseTrue False True False
22
דוגמא לשימוש ב Or Module Module1 Sub Main() Dim age As Integer Console.WriteLine("Please enter your age") age = Console.ReadLine() If age 18 Then Console.WriteLine("no school for you!") End If Console.ReadKey() End Sub End Module
23
"היפוך" - NOT A NotA True False True
24
דוגמא לשימוש ב Not Module Module1 Sub Main() Dim age As Integer Console.WriteLine("Please enter your age") age = Console.ReadLine() If Not (age = 100) Then Console.WriteLine("very young...") End If Console.ReadKey() End Sub End Module
25
וכמובן שאפשר להרכיב.... Module Module1 Sub Main() Dim numCandles, x As Integer x = 5 Console.WriteLine("Please enter the amount of candles") numCandles = Console.ReadLine() If numCandles > 100 And numCandles < 500 And x = 2 Then Console.WriteLine("You get a discount") Else Console.WriteLine("No discount for you...") End If Console.ReadKey() End Sub End Module
26
אופרטורים לוגיים - סיכום בביטויים בוליאניים אפשר להשתמש גם באופרטורים לוגיים. And, Or, Not אופרטור וגם ( AND) מחזיר אמת אם שני הביטויים משני הצדדים של האופרטור אמת אופרטור או ( OR ) מחזיר אמת אם אחד מהביטויים משני הצדדים של האופרטור אמת אופרטור שלילה הלוגי ( NOT ) הופך את ערכו הלוגי של ביטוי הלוגי מימינו.
27
ElseIf
28
פקודת ElseIf פיצול ליותר משני מקרים עושים בעזרת ElseIf If condition Then statement1 ElseIf condition statement2 ElseIf condition statement3. Else statement-n End If
29
ניחוש צבעים – מה יקרה פה? מה רוצים שיקרה פה? Module Module1 Sub Main() Dim color As String = "red" 'Dim color As String = "purple" If color = "red" Then Console.WriteLine("The color is red") End If If color = "green" Then Console.WriteLine("The color is green") End If Console.WriteLine("I dont know this color") End Sub End Module
30
ניחוש צבעים – הצעה לשיפור Module Module1 Sub Main() Dim color As String = "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") End If End Sub End Module
31
Module Module1 Sub Main() Dim grade As Integer grade = Console.ReadLine() If (grade >= 90) Then Console.WriteLine("you got an A") End If If (grade >= 80) Then Console.WriteLine("you got an B") End If If (grade >= 70) Then Console.WriteLine("you got an C") End If If (grade >= 60) Then Console.WriteLine("you got an D") End If If (grade < 60) Then Console.WriteLine("you failed") End If End Sub End Module ציונים – צריך עזרה?
32
Module Module1 Sub Main() Dim grade As Integer grade = Console.ReadLine() If (grade >= 90 And grade <= 100) Then Console.WriteLine("you got an A") End If If (grade >= 80 And grade < 90) Then Console.WriteLine("you got an B") End If If (grade >= 70 And grade < 80) Then Console.WriteLine("you got an C") End If If (grade >= 60 And grade < 70) Then Console.WriteLine("you got an D") End If If (grade < 60) Then Console.WriteLine("you failed") End If End Sub End Module ציונים – פתרון אפשרי
33
Module Module1 Sub Main() Dim grade As Integer 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") End If End Sub End Module ציונים – פתרון משופר
34
If מקונן (nested If)
35
Dim num1, num2, num3, min As Single 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 End If Else If (num2 < num3) Then min = num2 Else min = num3 End If Console.WriteLine("Minimum value: " & min) שימוש ב if בתוך if
36
מה קורה כאן? Dim x, y As String 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") End If Else Console.WriteLine("Not Defined") End If
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 Module Module1 Sub Main() Dim x As Decimal = Math.Abs(-10.4) Dim y As Decimal 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() End Sub End Module דוגמא - פונקציות מתמטיות
40
Select Case
41
Select Case – לבחירה בין מספר ערכים Dim letter As Char Console.WriteLine("Please enter a letter:") letter = Console.ReadLine() Select Case 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") End Select
42
Select Case – דוגמא Dim number As Integer = 8 Select Case number Case 1 To 5 Console.WriteLine("Between 1 and 5, inclusive") Case 6, 7, 8 Console.WriteLine("Between 6 and 8, inclusive") Case 9 To 10 Console.WriteLine("Equal to 9 or 10") Case Else Console.WriteLine("Not between 1 and 10, inclusive") End Select
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.