מבני בקרה לולאות
פקודת 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
מבוא לתכנות למנע " ס - שבוע מספר 2 - מאיר קומר - סמסטר ב ' - תשס " ו בצע עד איטראציה - iteration בצע עד s1s1 תנאי
מבוא לתכנות למנע " ס - שבוע מספר 2 - מאיר קומר - סמסטר ב ' - תשס " ו כל עוד איטראציה - iteration כל עוד s1s1 תנאי
שני סוגי לולאות לולאות במספר קבוע של פעמים לולאות עד אירוע מסוים (Event)
לולאות במספר קבוע של פעמים 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
קיצור דרך : 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
קיצור דרך : 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
לולאות עד אירוע 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
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
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
דוגמא נוספת 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
ועוד דוגמא נוספת 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
מספרים אקראיים 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
משחקי קוביות 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
משחק ניחוש 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
מבוא לתכנות למנע " ס - שבוע מספר 4 - מאיר קומר - סמסטר ב ' - תשס " ו לולאות - LOOPS For i = 1 to 2 s = s + 1 Next j s = 0 For j = 1 to 4 Next i i j s
לולאות בתוך לולאות 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
מספרים ראשוניים Module Module1 Sub Main() Dim i, factor, num As Integer Dim isprime As Boolean For num = 2 To 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
לולאות 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
לולאה בקבצים עד אירוע 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
למה אני צריך את ה 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
לולאות 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
לולאות 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