1 / 72

INF110 Visual Basic Programming AUBG Spring semester 2011

seth
Download Presentation

INF110 Visual Basic Programming AUBG Spring semester 2011

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. INF110Visual Basic ProgrammingAUBG Spring semester 2011Reference books:Schneider D., An Introduction to Programming Using Visual Basic, Prentice Hall, Pearson Education Inc., 7th Ed. 2008, 6th Ed. 2006 Liberty J., Learning Visual Basic .NET, O’Reilly, 2002 Any Visual Basic book available in AUBG libraryCourse lecturer: Assoc. Prof. Svetla Boytcheva, PhD

  2. INF110 Visual Basic Programming AUBG Spring semester 2011 Lecture 7 Title: Visual Basic (Repetition/Iteration statements 1)

  3. Lecture Contents: • Repetition/Iteration statements: • Pre Test Loops • Post Test Loops • Logically Controlled Loops • Counter Controlled Loops • Nested Loops

  4. Quiz 2onDecision Operators and Expressions

  5. INF110 Visual Basic Programming Control Structures II Repetition (aka Iteration)

  6. An iterative statement is one which executes one or more other statements a certain number of times • i.e. the execution of one or more statements is repeated • The iterative statement comes in a variety of “flavours”. • They are used for a number of purposes, such as: • searching a collection of data items for one with a specific value, or • performing a calculation involving a collection of data items (e.g. adding them all together to get a total) or • performing some action repeatedly until an event occurs which tells it to stop (imagine a robot placing boxes on a conveyor belt ...)

  7. Repetition statements (also iterative statements) may classify in two sub groups: • Do…Loop statements • For…Next statements

  8. Do Loops • A loop is one of the most important structures in programming. • Used to repeat a sequence of statements a number of times. • The Do looprepeats a sequence of statements either: • as long as, a certain condition isTrue. • until, a certain condition becomesTrue. • It has a few variants …

  9. Comments • Be careful to avoid infinite loops – loops that never end. • VB.NET allows for the use of either the While keyword or the Until keyword at the top or the bottom of a loop. We will mostly use While at the top of a loop (pretest loop) and Until at the bottom of a loop (posttest loop). • Semantics: • “Do these actions while condition is true” • or • “Do these actions until a condition becomes true”

  10. Variations of the Do…Loop control structure: • Do … Loop • 1/ Pre test loops • with While and Until • 2/ Post test loops • with While and Until

  11. Do Loops Do While boolean expression statements Loop ------------------------------------------ Do Until boolean expression statements Loop ------------------------------------------ Do statements Loop While boolean expression ------------------------------------------ Do statements Loop Until boolean expression ------------------------------------------ Do statements Loop

  12. Do Loops in VBasic Pretest loops ----------------------------------------- Do While boolean expression statements Loop ----------------------------------------- Do Until boolean expression statements Loop -----------------------------------------

  13. Do Loops in VBasic Posttest loops ----------------------------------------- Do statements Loop While boolean expression ----------------------------------------- Do statements Loop Until boolean expression -----------------------------------------

  14. Do Loops in VBasic Infinite /endless/ loops ----------------------------------------- Do statements Loop -----------------------------------------

  15. Do While <condition> … LoopStatement

  16. Do While … Loop Syntax Condition is tested. If it is True, the loop is run. If it is False, the statements following the Loop statement are executed. Do While condition statement(s) Loop These statements are inside the body of the loop and are run if the condition above is True. condition is a Boolean expression

  17. Pseudocode and Flow Chart for a Do Loop

  18. Dim Counter As Integer = 20 Do While Counter <= 100 Console.Writeline(Counter) Counter = Counter + 10 Loop The above example will keep on adding until Counter’s value becomes > 100

  19. Example Dim PassWord As String PassWord = “” Do While PassWord <> “SHAZAM” Console.Writeline(“Enter password:") PassWord = Console.Readline() Loop PassWord is the loop control variable because the value stored in PassWord is what is tested to determine if the loop should continue or stop.

  20. ‘Accumulate sum of the numbers from 1 to 7 Dim Num As Integer = 1 Dim Sum As Integer = 0 Do While Num <= 7 Sum = Sum + Num Num = Num + 1 'Add 1 to the value of Num Loop

  21. Do … Loop Until <condition>Statement

  22. Do … Loop Until Since the condition is tested at the bottom the loop, the loop will execute at least once. Do statement(s) Loop Until condition Loop is executed once and then the condition is tested. If it is False, the loop is run again. If it is True, the statements following the Loop statement are executed.

  23. Pseudocode and Flowchart for a Do … Loop Until

  24. Dim Counter As Integer = 20 Do Console.Writeline(Counter) Counter = Counter + 10 Loop Until Counter > 100

  25. Example Dim PassWord As String Do Console.Writeline(“Enter password:") PassWord = Console.Readline() Loop Until PassWord = "SHAZAM"

  26. Nested Loops Statements inside a loop can contain another loop. (Just like If statements). Example: What is the output of this code? i = 0 Do While i < 6 j = 0 Do While j < 6 Console.Write("*") j = j + 1 Loop Console.WriteLine() i = i + 1 Loop Nested loops are quite common, especially for processing tables of data. Assume initially that i = 0

  27. Nested Loops Statements inside a loop can contain another loop. (Just like If statements). Example: What is the output of this code? i = 0 Do While i < 6 j = 0 Do While j < i Console.Write("*") j = j + 1 Loop Console.WriteLine() i = i + 1 Loop Nested loops are quite common, especially for processing tables of data. Assume initially that i = 0

  28. When conditionis a variable of Boolean type, the statements like If flagVar = True Then ... can be replaced by If flagVar Then ... and the statements like If flagVar = False Then ... can be replaced by If Not flagVar Then ...

  29. The statements like Do While flagVar = True can be replaced by Do While flagVar and The statements like Do While flagVar = False can be replaced by Do While Not flagVar

  30. The iterative statement Do While condition statement(s) Loop Has an alternativewhilecontrol structure: The While repetition structure

  31. While Repetition Structure • Repetition structure • Allows the programmer to specify that an action should be repeated, depending on the value of a condition. • Example (pseudocode) • While there are more items on my shopping list • Purchase next item • Cross it off my list Just like Do While ... loop

  32. true product = product * 2 product <= 1000 false Dim Product As Integer = 2 While Product <= 1000 Console.Writeline(Product) Product = Product * 2 End While The decision is tested each time the loop iterates

  33. ‘ Modern VB 2005/2008 version Dim Product As Integer = 2 While Product <= 1000 Console.Writeline(Product) Product = Product * 2 End While VB6 >>> VB 2005/2008 The End While statement replaces the Wend statement of VB6. ‘ Obsolete VB 6 version Dim Product As Integer = 2 While Product <= 1000 Console.Writeline(Product) Product = Product * 2 WEnd

  34. Nested Loops using While … End While control structure Statements inside a loop can contain another loop. Example: What is the output of this code? i=0 While i < 6 j = 0 While j < 6 Console.Write("*") j = j + 1 End While Console.WriteLine() i = i + 1 End While Nested loops are quite common, especially for processing tables of data. Assume initially that i = 0

  35. Nested Loops using While … End While control structure Statements inside a loop can contain another loop. Example: What is the output of this code? i=0 While i < 6 j = 0 While j < i Console.Write("*") j = j + 1 End While Console.WriteLine() i = i + 1 End While Nested loops are quite common, especially for processing tables of data. Assume initially that i = 0

  36. Do Loops • All the variations use the same basic model. A pretest loop is executed either • While the condition is True. Uses While reserved word Do While condition statement-block Loop • Until the condition becomes True. Uses Until reserved word Do Until condition statement-block Loop

  37. Examples I = 1 Do While (I <= 10) Console.WriteLine(I) I = I + 1 Loop How many iterations are to be run? (10) Try it

  38. Examples I = 1 Do Until I <= 10 Console.WriteLine(I) I = I + 1 Loop How many iterations are to be run? (0) Try it

  39. Examples to achieve the same effect I = 1 Do While (I <= 10) Console.WriteLine(I) I = I + 1 Loop ======================= same effect I = 1 Do Until Not(I <= 10) Console.WriteLine(I) I = I + 1 Loop How many iterations are to be run? (10) Try it

  40. Examples to achieve the same effect I = 1 Do WhileNot(I <= 10) Console.WriteLine(I) I = I + 1 Loop ======================= same effect I = 1 Do Until (I <= 10) Console.WriteLine(I) I = I + 1 Loop How many iterations are to be run? (0) Try it

  41. Post test loops with While and Until

  42. Do Loops • All the variations use the same basic model. A posttest loop is executed either • While the condition is True. Uses While reserved word Do statement-block Loop While condition • Until the condition becomes True. Uses Until reserved word Do statement-block Loop Until condition

  43. Examples I = 1 Do Console.WriteLine(I) I = I + 1 Loop While (I <= 10) How many iterations are to be run? (10) Try it

  44. Examples I = 1 Do Console.WriteLine(I) I = I + 1 Loop Until (I <= 10) How many iterations are to be run? (1) Try it

  45. Examples to achieve the same effect I = 1 Do Console.WriteLine(I) I = I + 1 Loop While (I <= 10) =================== same effect I = 1 Do Console.WriteLine(I) I = I + 1 Loop UntilNot(I <= 10) How many iterations are to be run? (10) Try it

  46. Examples to achieve the same effect I = 1 Do Console.WriteLine(I) I = I + 1 Loop WhileNot(I <= 10) =================== same effect I = 1 Do Console.WriteLine(I) I = I + 1 Loop Until (I <= 10) How many iterations are to be run? (1) Try it

  47. Endless loops with While … End While andDo … Loop

  48. Endless loops - examples While True statement-block End While

  49. Endless loops - comments The endless loops should be avoided. They must be terminated from within the own loop body with an Exit statement While True statement-block if condition Then Exit While statement-block End While

  50. Endless loops - examples Do statement-block Loop

More Related