Presentation is loading. Please wait.

Presentation is loading. Please wait.

מבוא למדעי המחשב לתעשייה וניהול הרצאה 6. מפעל השעווה – לולאות  עד עכשיו  טיפלנו בייצור נרות מסוג אחד, במחיר אחיד  למדנו להתמודד עם טיפול במקרים שונים.

Similar presentations


Presentation on theme: "מבוא למדעי המחשב לתעשייה וניהול הרצאה 6. מפעל השעווה – לולאות  עד עכשיו  טיפלנו בייצור נרות מסוג אחד, במחיר אחיד  למדנו להתמודד עם טיפול במקרים שונים."— Presentation transcript:

1 מבוא למדעי המחשב לתעשייה וניהול הרצאה 6

2 מפעל השעווה – לולאות  עד עכשיו  טיפלנו בייצור נרות מסוג אחד, במחיר אחיד  למדנו להתמודד עם טיפול במקרים שונים ( הנחות, מחירים שונים לצבעים שונים וכד ')  למדנו לחשב כמה נרות צריך ( עבור קונה יחיד ) לכל החג.  נרצה לתת אפשרות לבצע מספר הזמנות ברצף  כאשר מספר ההזמנות ידוע מראש  כאשר המשתמש יחליט כמה הזמנות לבצע?

3 פתרון בעזרת for, כאשר מספר ההזמנות ידוע Const WaxPerCandle As Integer = 10 Dim storageWax As Integer = 1000 Dim totalWax, buyWax, numCandles As Integer totalWax = 0 For i As Integer = 1 To 3 Console.WriteLine("Enter number of candles for order " & i) numCandles = Console.ReadLine() totalWax += WaxPerCandle * numCandles Next buyWax = totalWax - storageWax Console.WriteLine("Amount of wax to buy: " & buyWax)

4 While

5 while  אם התנאי condition הוא 'אמת' אז ההצהרה statement מתבצעת  אח " כ התנאי נבדק שוב אם ערכו עדיין ' אמת ' ההצהרה מבוצעת שוב  ההצהרה מבוצעת שוב ושוב עד שהתנאי נהיה ' שקר '  ההצהרה יכולה להכיל מספר פקודות, ותקרא גוף הלולאה While condition statements End While

6 הלוגיקה של פקודת While condition evaluated הביטוי נבדק statement הצהרה מבוצעת true false

7 פתרון בעזרת while, כאשר מספר ההזמנות אינו ידוע Const WaxPerCandle As Integer = 10 Dim storageWax As Integer = 1000 Dim totalWax, buyWax, numCandles As Integer totalWax = 0 Console.WriteLine("Enter number of candles for order ") numCandles = Console.ReadLine() While numCandles <> 0 totalWax += WaxPerCandle * numCandles Console.WriteLine("Enter number of candles for order ") numCandles = Console.ReadLine() End While buyWax = totalWax - storageWax Console.WriteLine("Amount of wax to buy: " & buyWax)

8 דוגמא לשימוש בלולאה – חישוב ממוצע  הבעיה :  נקלוט מספרים ממשתמש, נחשב את ממוצע המספרים.  לא נקבע מראש כמה מספרים לקלוט  פתרון :  נקלוט מספר חדש  נוסיף אותו לסכום הביניים + נעדכן את המונה  כשנחליט לעצור  נחלק את סכום הביניים במונה  איך נדע לעצור ?  נבחר ערך שמייצג רצון לעצור

9 קוד לחישוב ממוצע – חלק 1 Dim sum As Integer = 0, count As Integer = 0 Dim value, average As Single Console.WriteLine("Enter an integer (0 to quit): ") value = Console.ReadLine While value <> 0 count += 1 sum += value Console.WriteLine("Enter an integer (0 to quit): ") value = Console.ReadLine End While המשך בשקף הבא

10 קוד לחישוב ממוצע – חלק 2 המשך משקף קודם... If count = 0 Then Console.WriteLine("No values were entered.") Else average = sum / count End If Console.WriteLine("The average is " & average)

11 דוגמא לשימוש בלולאה – בדיקת קלט  הבעיה :  רוצים לקלוט מספרים מהמשתמש  דרוש לקלוט מספרים מסוג מסוים  פתרון :  נשתמש בלולאה  כל עוד לא קיבלנו קלט מתאים  נבקש ונקלוט קלט נוסף

12 קוד - בדיקת קלט Dim value As Single Console.WriteLine("Enter a number between 1 to 10") value = Console.ReadLine While value 10 Console.WriteLine("Bad input. Enter number between 1 to 10") value = Console.ReadLine End While Console.WriteLine("You entered {0}. Thank you!", value)

13 יציאה מ while  יציאה מתבצעת כאשר התנאי כבר לא מתקיים  ניתן להתערב בהתנהלות של הלולאה ( כמו ב for) בצורות הבאות :  פקודת Continue גורמת ללולאה לדלג לסיבוב הבא  פקודת Exit גורמת ללולאה להסתיים לחלוטין Dim x As Integer = 0 While x < 10 If x = 3 Then Exit While End If Console.WriteLine("the number is: " & x) x += 1 End While

14 מה קורה? Dim x As Integer = 0 While x < 10 Console.WriteLine("the number is: " & x) If x = 5 Then Continue While End If x += 1 End While Dim x As Integer = 0 While x > 10 Console.WriteLine("the number is: " & x) x += 1 End While

15 מה עושה הקוד? Dim x As Integer x = Console.Read() While x <> AscW(vbCr) If x >= AscW(0) And x =< AscW(9) Then Console.Write(ChrW(x)) End If x = Console.Read() End While

16 Do … loop

17 Do Until … Loop Dim counter As Integer = 0 Do Until (counter = 10) Console.WriteLine("What is this " & counter) counter = counter + 1 Loop Console.ReadKey()

18 Do … loop Do While condition statements Loop Do Until condition statements Loop Do statements Loop While condition Do statements Loop Until condition Until – עד ש While – כל עוד

19 השוואה בין גרסאות do loop condition evaluated הביטוי נבדק statement הצהרה מבוצעת true false condition evaluated הביטוי נבדק statement הצהרה מבוצעת true false Do …. Loop While\Until Do While\Until …. Loop

20 השוואה בין do ל while Dim low As Integer = 0 Dim high As Integer = 10 Do Console.WriteLine(low) low += 1 Loop While (low < high) Dim low As Integer = 0 Dim high As Integer = 10 Do While (low < high) Console.WriteLine(low) low += 1 Loop Dim low As Integer = 0 Dim high As Integer = 10 Do Console.WriteLine(low) low += 1 Loop While (high < low) Dim low As Integer = 0 Dim high As Integer = 10 Do While (high < low) Console.WriteLine(low) low += 1 Loop

21 שימוש ב while – לולאה עד אירוע Dim x As Char Dim counter As Integer = 0 x = ChrW(Console.Read()) If (x <> ".") Then counter = counter + 1 End If While (x <> ".") x = ChrW(Console.Read()) If (x <> ".") Then counter = counter + 1 End If End While Console.WriteLine("The counter is " & counter)

22 שיפור ע"י Do while Dim x As Char Dim counter As Integer = 0 Do x = ChrW(Console.Read()) If (x <> ".") Then counter = counter + 1 End If Loop While (x <> ".") Console.WriteLine("The counter is " & counter)

23 מה השתנה? Dim x As Char Dim counter As Integer = 0 x = ChrW(Console.Read()) Do Until (x = ".") x = ChrW(Console.Read()) counter = counter + 1 Loop Console.WriteLine("The counter is " & counter)

24 ומה עכשיו ? Dim x As Char Dim counter As Integer = 0 x = Console.ReadLine() Do Until (x = ".") x = Console.ReadLine() counter = counter + 1 Loop Console.WriteLine("The counter is " & counter)

25 ועוד גיוון... Dim x As String Dim counter As Integer = 0 Do Until (x = "stop") Console.WriteLine("Please enter a word") x = Console.ReadLine() counter = counter + 1 Loop Console.WriteLine("The counter is " & counter)

26 עבודה עם קבצים ולולאות

27 קריאה מתוך קובץ - הרחבה  ראינו שמאתחלים את שם הקובץ כך : writer =My.Computer.FileSystem.OpenTextFileWriter(" שם קובץ ")  ניתן גם להשתמש ב :  writer =File.OpenText(" שם קובץ ")

28 לולאה בקבצים עד אירוע Dim Input As Integer Dim sum = 0, count = 0 Dim objStreamReader As StreamReader objStreamReader = File.OpenText("z:\NumTest.txt") Do Until Input = -1 Input = objStreamReader.ReadLine() If (Input <> -1) Then sum += Input count += 1 End If Loop Console.WriteLine("The Average is " & sum / count)

29 אפשר לוותר על ה if, ע"י השינוי הבא: Dim Input As Integer = 0 Dim sum = 0, count = 0 Dim objStreamReader As StreamReader objStreamReader = File.OpenText("z:\NumTest.txt") Do Until Input = -1 sum += Input count += 1 Input = objStreamReader.ReadLine() Loop Console.WriteLine("The Average is " & sum / (count - 1))

30 לולאה עד סוף הקובץ Dim Input As String = "" Dim objStreamReader As StreamReader objStreamReader = File.OpenText("z:\NumTest.txt") Do Input = objStreamReader.ReadLine() Console.WriteLine("I read " & Input) Loop Until Input Is Nothing


Download ppt "מבוא למדעי המחשב לתעשייה וניהול הרצאה 6. מפעל השעווה – לולאות  עד עכשיו  טיפלנו בייצור נרות מסוג אחד, במחיר אחיד  למדנו להתמודד עם טיפול במקרים שונים."

Similar presentations


Ads by Google