View
237
Download
3
Embed Size (px)
Citation preview
מבני בקרהלולאות
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 SubEnd Module
- 2מבוא לתכנות למנע"ס - שבוע מספר מאיר קומר - סמסטר ב' - תשס"ו
iterationאיטראציה - בצע עדבצע עד
s1 תנאי
- 2מבוא לתכנות למנע"ס - שבוע מספר מאיר קומר - סמסטר ב' - תשס"ו
iterationאיטראציה - כל עודכל עוד
s1
תנאי
שני סוגי לולאות
לולאות במספר קבוע של פעמים•
)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 SubEnd 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 SubEnd 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 SubEnd 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 SubEnd Module
DO WHILEModule 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 SubEnd 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 SubEnd 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 SubEnd 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 SubEnd Module
מספרים אקראייםImports System.RandomModule 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 SubEnd Module
משחקי קוביותImports System.RandomModule 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 SubEnd Module
משחק ניחושImports System.RandomModule 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 SubEnd Module
- 4מבוא לתכנות למנע"ס - שבוע מספר מאיר קומר - סמסטר ב' - תשס"ו
LOOPSלולאות -
For i = 1 to 2
s = s + 1
Next j
s = 0
For j = 1 to 4
Next i
i j s01 1
2 1
3 2
4 3
2 41
52
63
74
8
לולאות בתוך לולאות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 SubEnd Module
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 SubEnd Module
בקבציםFORלולאות Imports System.IOModule 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.IOModule 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 SubEnd Module
?IFלמה אני צריך את הImports System.IOModule 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.IOModule 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.IOModule 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