מבני בקרה לולאות. פקודת CASE Module Module1 Sub Main() Dim input As Char input = Console.ReadLine() Select Case (input) Case "A" Console.WriteLine("A.

Slides:



Advertisements
Similar presentations
Standard Algorithms Find the highest number. ! Your name and today’s date ! Find the maximum Dim numbers(20) As Integer.
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
מחרוזות של תווים. מבוא לתכנות למנע"ס - שבוע מספר 5 - מאיר קומר - סמסטר ב' - תשס"ו מחרוזות - Strings Dim s As String s = “hello” s = s & “kita” או s =
BACS 287 Programming Logic 3. BACS 287 Iteration Constructs Iteration constructs are used when you want to execute a segment of code several times. In.
סוגי מידע / משתנים ד " ר אבי רוזנפלד. סוגאורךטווח Boolean1 Bit0,1 Byte1 byte0-255 Char 2 bytes תו Decimal16 bytes0 through +/-79,228,162,514,264,337,593,543,950,335.
תכנות ב C#. דוגמא לפלט using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { static void.
תרגילים. גדול, קטן, וממוצע מקובץ Imports System.IO Module Module1 Sub Main() Dim readnum As StreamReader readnum = _ My.Computer.FileSystem.OpenTextFileReader.
אלגוריתמים, WINDOWS והחיים. מבוא למדעי המחשב - מאיר קומר - סמסטר א '- תשס " ט - שיעור מספר 7 מיוןsort.
מבני בקרה מבוא לתכנות למנע " ס - שבוע מספר 3 - מאיר קומר - סמסטר ב ' - תשס " ו הסתעפות “ אם השמאל ואימנה ואם הימין ואשמאילה ”
פתרון תרגיל 10. שאלה #1 Module Module1 Sub Main() Dim maarach(9), zoogy(4), ezoogy(5) As Integer Dim i, counter, j As Integer For i = 0 To 9 Console.WriteLine("please.
Switch Statement switch (month) { case 9: case 4: case 6: case 11: days = 30; break; case 2: days = 28; if (year % 4 == 0) days = 29; break; default: days.
פתרון בוחן הכיתה. שאלה #1 Module Module1 Sub Main() Dim x, z As Integer x = Console.ReadLine() z = Console.ReadLine() If (x = 0) Then Console.WriteLine("Error")
פתרון תרגיל 11. שאלה #1 Module Module1 Sub Main() Dim x(10, 10) As Integer Dim i, j As Integer For i = 1 To 10 For j = 1 To 10 x(i, j) = i * j Console.Write("
מערכים מבוא לתכנות למנע"ס - שבוע מספר 8 - מאיר קומר - סמסטר ב' - תשס"ו והנה בעיה כתוב תוכנית אשר תקלוט 36 מספרים ותדפיס כמה מתוכם גדולים יותר מהממוצע.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
אלגוריתמי חיפוש. Brute Force Module Module1 Function BruteForce(ByRef x() As Integer, ByRef item As Integer) As Integer Dim i As Integer For i = 0 To.
פתרון תרגילים #9. דוגמה פשוטה #1 Module Module1 Sub Main() Dim x As String x = Console.ReadLine For i = 1 To x.Length() - 1 Step 2 Console.WriteLine(x(i))
לולאות 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 1 Department of Computer Science-BGU.
Chapter 6 - Visual Basic Schneider
What is the out put #include using namespace std; void main() { int i; for(i=1;i
מערכים דו ממדי ו STRUCTS פונקציות בוליאנית Module Module1 Function Flip(ByVal word1 As String) As Boolean Dim i As Integer For i = 0 To word1.Length()
פונקציות. פונקציות מוכרות Imports System.Math Module Module1 Sub Main() Dim x As Decimal = Math.Abs(-10.4) Dim y As Decimal Console.WriteLine("I will.
1 Chapter 6 Repetition. 2 Outline & Objectives Loop Structure Loop Structure Elements of a Loop Structure Elements of a Loop Structure Processing Lists.
الحلقات for For i =1 To x step -2 …………. Next DO while (شرط) …………. Loop DO …………. Loop while (شرط) الحلقة Do while.
Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה משתנים יחד דוגמאות : עובד : שם, טלפון,
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
BIM313 – Advanced Programming Techniques Flow Control 1.
Programming with C# Iteration LECTURE 3. Summary of last lecture SequenceSelectionif and switch statementsCastingRandom.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
VB Core II Conditional statements Exception handling Loops Arrays Debugging.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4: Control Structures: Part 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures.
26/10/ Selection Nested If structures & Complex Multiple Conditions.
CSE 201 – Elementary Computer Programming 1 Extra Exercises Source: Suggested but not selected midterm questions.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6 - Visual Basic Schneider 1 Chapter 6 Repetition.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
04/11/ Arrays 1D Arrays Defining, Declaring & Processing.
Even more problems.. Mean (average) I need a program that calculates the average of student test scores. I need a program that calculates the average.
1 Chapter 10 Additional Control Structures and Exceptions.
Count and add list of numbers From user input and from file.
מבוא למדעי המחשב לתעשייה וניהול הרצאה 7. סברוטינות subroutines.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
מחסנית ותור Stacks and Queues. מחסנית Stack מחסנית - Stack ADT סוג של מערך מוגבל מהיר מאוד ותופס מעט זיכרון שימוש ב LIFO – LIFO (Last In, First Out)
הרצאה 3 מבוא למדעי המחשב לתעשייה וניהול הודעות : הודעות : או דרך moodle – ושם לפנות ל : אתר מכון טל החומר.
Repetition Structures
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
CIS 3301 C# Lesson 3 Control Statements - Selection.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Susie’s lecture notes are in the presenter’s notes, below the slides Disclaimer: Susie may have made errors in transcription or understanding. If there.
מבוא למדעי המחשב לתעשייה וניהול הרצאה 12. ספריות.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Computer Science I: Understand how to evaluate expressions with DIV and MOD Random Numbers Reading random code Writing random code Odds/evens/…
מבוא למדעי המחשב לתעשייה וניהול הרצאה 6. מפעל השעווה – לולאות  עד עכשיו  טיפלנו בייצור נרות מסוג אחד, במחיר אחיד  למדנו להתמודד עם טיפול במקרים שונים.
מבנה נתונים ואלגוריתמים ) לשעבר - עיבוד מידע( ד"ר אבי רוזנפלד ד"ר אריאלה ריכרדסון.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
אלגוריתמי חיפוש. Brute Force Module Module1 Function BruteForce(ByRef x() As Integer, ByRef item As Integer) As Integer Dim i As Integer For i = 0 To.
Identify the Appropriate Method for Handling Repetition
מבוא למדעי המחשב לתעשייה וניהול
Repetition (While-Loop) version]
Compiler Design First Lecture.
Repetition-Sentinel,Flag Loop/Do_While
الاسباب الأضرار وسائل الحمايه
Variables, Loops, Decision Statements, etc
ניתוח מערכות מידע תכנות ב C#
VB.Net Programming Console Application
Controlling Program Flow
Console.WriteLine(“Good luck!”);
Presentation transcript:

מבני בקרה לולאות

פקודת 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