1 / 24

Iteration in Programming

Learn about different types of loops and how to use them in programming, including for loops, while loops, do-while loops, and loop until.

lemonj
Download Presentation

Iteration in Programming

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. מבני בקרהלולאות

  2. פקודת CASE Module Module1 Sub Main() Dim input As Char input = Console.ReadLine() Select Case (input) Case "A" Console.WriteLine("A is for Apple") Case "B" Console.WriteLine("B is for Boy") Case "C" Console.WriteLine("C is for Cat") Case Else Console.WriteLine("Not defined") End Select Console.ReadKey() End Sub End Module

  3. איטראציה - iterationבצע עד s1 תנאי מבוא לתכנות למנע"ס - שבוע מספר 2 - מאיר קומר - סמסטר ב' - תשס"ו

  4. איטראציה - iterationכל עוד s1 תנאי מבוא לתכנות למנע"ס - שבוע מספר 2 - מאיר קומר - סמסטר ב' - תשס"ו

  5. שני סוגי לולאות • לולאות במספר קבוע של פעמים • לולאות עד אירוע מסוים (Event)

  6. לולאות במספר קבוע של פעמים Module Module1 Sub Main() Dim i As Integer = 0 While (i < 100) Console.WriteLine("I love computers") i = i + 1 End While Console.ReadKey() End Sub End Module

  7. קיצור דרך: FOR Module Module1 Sub Main() Dim i As Integer For i = 1 To 100 Console.WriteLine("I STILL love computers") Next i ' or just Next Console.ReadKey() End Sub End Module

  8. קיצור דרך: FORהמושג STEP Module Module1 Sub Main() Dim i As Integer For i = 100 To 1 Step -5 Console.Write("The square root of " & i) Console.Write(" is " & Math.Sqrt(i)) Console.WriteLine() Next i ' or just Next Console.ReadKey() End Sub End Module

  9. לולאות עד אירוע Module Module1 Sub Main() Dim x As Char Dim counter As Integer = 0 x = Console.ReadLine() If (x <> ".") Then counter = counter + 1 End If While (x <> ".") x = Console.ReadLine() If (x <> ".") Then counter = counter + 1 End If End While Console.WriteLine("The counter is " & counter) Console.ReadKey() End Sub End Module

  10. DO WHILE Module Module1 Sub Main() Dim x As Char Dim counter As Integer = 0 Do x = Console.ReadLine() If (x <> ".") Then counter = counter + 1 End If Loop While (x <> ".") Console.WriteLine("The counter is " & counter) Console.ReadKey() End Sub End Module

  11. LOOP UNTIL Module Module1 Sub Main() Dim counter As Integer = 0 Do Until (counter = 10) Console.WriteLine("What is this " & counter) counter = counter + 1 Loop Console.ReadKey() End Sub End Module

  12. דוגמא נוספת Module Module1 Sub Main() 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) Console.ReadKey() End Sub End Module

  13. ועוד דוגמא נוספת Module Module1 Sub Main() Dim x As String Dim counter As Integer = 0 Do Until (x = "avi") Console.WriteLine("Please enter a word") x = Console.ReadLine() counter = counter + 1 Loop Console.WriteLine("The counter is " & counter) Console.ReadKey() End Sub End Module

  14. מספרים אקראיים Imports System.Random Module Module1 Sub Main() Dim RandomNumber As Integer Dim RandomClass As New Random() Dim i As Integer For i = 1 To 20 RandomNumber = RandomClass.Next(1, 100) Console.WriteLine("the number is " & RandomNumber) Next i Console.ReadKey() End Sub End Module

  15. משחקי קוביות Imports System.Random Module Module1 Sub Main() Dim die1, die2 As Integer Dim RandomClass As New Random() Dim i As Integer For i = i To 20 die1 = RandomClass.Next(1, 6) die2 = RandomClass.Next(1, 6) Console.WriteLine("You rolled " & die1 & " and " & die2) Next i Console.ReadKey() End Sub End Module

  16. משחק ניחוש Imports System.Random Module Module1 Sub Main() Dim num, guess As Integer Dim RandomClass As New Random() num = RandomClass.Next(1, 100) Do Console.WriteLine("Please try to guess the number") guess = Console.ReadLine() If (guess > num) Then Console.WriteLine("You guessed too high") ElseIf (guess < num) Then Console.WriteLine("You guessed too low") Else Console.WriteLine("You got it") End If Loop While (num <> guess) Console.ReadKey() End Sub End Module

  17. לולאות - LOOPS i j s s = 0 For i = 1 to 2 1 1 0 2 1 For j = 1 to 4 2 3 s = s + 1 3 4 4 2 1 Next j 5 2 Next i 6 3 4 7 8 מבוא לתכנות למנע"ס - שבוע מספר 4 - מאיר קומר - סמסטר ב' - תשס"ו

  18. לולאות בתוך לולאות Module Module1 Sub Main() Dim i, j As Integer For i = 1 To 5 For j = 1 To i Console.Write(j) Next j Console.WriteLine() Next i Console.ReadKey() End Sub End Module

  19. מספרים ראשוניים Module Module1 Sub Main() Dim i, factor, num As Integer Dim isprime As Boolean For num = 2 To 30000 isprime = True factor = 0 'Now check if it was divisible For i = 2 To num - 1 If (num Mod i = 0) Then isprime = False factor = i End If Next If (isprime) Then Console.WriteLine("The number " &num & " is prime") Else Console.WriteLine(i & " is divisible by " & factor) End If Next Console.ReadKey() End Sub End Module

  20. לולאות FOR בקבצים Imports System.IO Module Module1 Sub Main() Dim Input As Integer Dim objStreamReader As StreamReader objStreamReader = File.OpenText(“z:\numbers.txt") Dim i As Integer For i = 1 To 10 Input = objStreamReader.ReadLine() Console.WriteLine("I read " & Input) Next Console.ReadKey() End Sub End Module

  21. לולאה בקבצים עד אירוע Imports System.IO Module Module1 Sub Main() Dim Input As Integer Dim sum = 0, count = 0 Dim objStreamReader As StreamReader objStreamReader = File.OpenText(“z:\KBTest.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) Console.ReadKey() End Sub End Module

  22. למה אני צריך את הIF? Imports System.IO Module Module1 Sub Main() Dim Input As Integer = 0 Dim sum = 0, count = 0 Dim objStreamReader As StreamReader objStreamReader = File.OpenText(“z:\KBTest.txt") Do Until Input = -1 sum += Input count += 1 Input = objStreamReader.ReadLine() Loop Console.WriteLine("The Average is " & sum / (count - 1)) Console.ReadKey() End Sub End Module

  23. לולאות EOF Imports System.IO Module Module1 Sub Main() Dim Input As String = "" Dim objStreamReader As StreamReader objStreamReader = File.OpenText(“z:\KBTest.txt") Do Until Input Is Nothing Input = objStreamReader.ReadLine() Console.WriteLine("I read " & Input) Loop Console.ReadKey() End Sub End Module

  24. לולאות EOFעם מספרים Imports System.IO Module Module1 Sub Main() Dim Input As String = "" Dim temp As Integer Dim objStreamReader As StreamReader objStreamReader = File.OpenText("z:\numbers.txt") Do Until Input Is Nothing Input = objStreamReader.ReadLine() temp = Input Console.WriteLine("I read " & Input + 2) Loop Console.ReadKey() End Sub End Module

More Related