1 / 61

INF110 Visual Basic Programming AUBG Spring semester 2011

perrin
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 08 Title: Visual Basic (Repetition/Iteration statements 2)

  3. Lecture Contents: • Reminder on Decisions/Selections • Reminder on Repetitions/Iterations • Endless Loops • Loops with GOTO • Demo programs • Practical session

  4. INF110 Visual Basic Programming Iterations Demo programs by J.Liberty, Chapter 06, 0609-0617 Plus Practical Session

  5. Reminder On Decisions/Selections

  6. VBasic selection statements include If condition Then statement sequence End If If condition Then statement sequence1 Else statement sequence2 End If

  7. VBasic selection statements include If condition1 Then statement sequence1 ElseIf condition2 statement sequence2 • ElseIf condition3 • statement sequence3 • Else • statement sequence4 End If

  8. VBasic selection statements include Select Case expression Case value1 Block of one or more VB statements Case value2 Block of one or more VB Statements Case value3 Block of one or more VB statements Case value4 . . . Case Else Block of one or more VB Statements End Select

  9. Reminder On Iterations/Repetitions

  10. Visual Basic supports the following loop structures: • Do … Loop • While … End While • For … Next

  11. For i = 1 To 5 ... Next And its corresponding Do While equivalent i = 1 Do While i <= 5 ... i = i + 1 Loop

  12. Examples (a) For counter=1 to 10 Console.Writeline(counter) Next (b) For counter=1 to 100 Step 10 Console.Writeline(counter) Next (c) For counter=100 to 5 Step -5 Console.Writeline(counter) Next

  13. 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

  14. Pre test loops with While and Until

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

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

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

  18. 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

  19. 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

  20. Post test loops with While and Until

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

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

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

  24. 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

  25. 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

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

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

  28. 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

  29. Endless loops - examples Do statement-block Loop

  30. Endless loops - comments The endless loops should be avoided. They must be terminated from within the own loop body with an Exit statement Do statement-block If condition Then Exit Do statement-block Loop

  31. Example 6-9 • Creating Loops with Goto • Using Goto

  32. Example 6-9 Option Strict On Imports System Module Module1 Sub Main( ) Dim counterVariable As Integer = 0 repeat: ' the label Console.WriteLine("counterVariable: {0}", counterVariable) ' increment the counter counterVariable += 1 If counterVariable < 10 Then GoTo repeat ' loop implemented: control transferred back to label repeat End If End Sub 'Main End Module

  33. Example 6-10 • The Do Loop • Using Do While

  34. Example 6-10 Option Strict On Imports System Module Module1 Sub Main( ) Dim counterVariable As Integer = 0 Do While counterVariable < 10 Console.WriteLine("counterVariable: {0}", counterVariable) counterVariable = counterVariable + 1 Loop ' While counterVariable < 10 End Sub 'Main End Module

  35. Example 6-11 • The Do Loop • Using Do Until

  36. Example 6-11 Option Strict On Imports System Module Module1 Sub Main( ) Dim counterVariable As Integer = 0 Do Until counterVariable = 10 Console.WriteLine("counterVariable: {0}", counterVariable) counterVariable = counterVariable + 1 Loop ' Until counterVariable = 10 End Sub 'Main End Module

  37. Example 6-12 • The Do Loop • Do … Loop While

  38. Example 6-12 Option Strict On Imports System Module Module1 Sub Main( ) Dim counterVariable As Integer = 100 Do Console.WriteLine("counterVariable: {0}", counterVariable) counterVariable = counterVariable + 1 Loop While counterVariable < 10 End Sub 'Main End Module

  39. Example 6-13 • The Do Loop • Breaking out of a Do Loop • Using Exit Do

  40. Example 6-13 Option Strict On Imports System Module Module1 Sub Main( ) Dim counterVariable As Integer = 0 Do Console.WriteLine("counterVariable: {0}", counterVariable) counterVariable = counterVariable + 1 ' test whether we've counted to 9, if so, exit the loop If counterVariable > 9 Then Exit Do End If Loop End Sub 'Main End Module

  41. Example 6-14 • The For Loop • Using a For loop

  42. Example 6-14 Option Strict On Imports System Module Module1 Sub Main( ) Dim loopCounter As Integer For loopCounter = 0 To 9 Console.WriteLine("loopCounter: {0}", loopCounter) Next End Sub 'Main End Module

  43. Example 6-15 • The For Loop • Loop with a Single counter

  44. Example 6-15 Option Strict On Imports System Module Module1 Sub Main( ) Dim loopCounter As Single For loopCounter = 0.5 To 9 Console.WriteLine("loopCounter: {0}", loopCounter) Next End Sub 'Main End Module

  45. Example 6-16 • The For loop • Adjusting the step counter

  46. Example 6-16 Option Strict On Imports System Module Module1 Sub Main( ) Dim loopCounter As Single For loopCounter = 0.5 To 9 Step 0.5 Console.WriteLine("loopCounter: {0}", loopCounter) Next End Sub 'Main End Module

  47. Example 6-17 • The For Loop • Controlling a For Loop using Next • Multiple updates with one Next statement

  48. Example 6-17 Option Strict On Imports System Module Module1 Sub Main( ) Dim outer As Integer Dim inner As Integer For outer = 3 To 6 For inner = 10 To 12 Console.WriteLine("{0} * {1} = {2}", outer, inner, outer * inner) Next inner, outer End Sub 'Main End Module

  49. Example Run the program. Test the program. Modify the program and rerun it again.

  50. Practical session 3Loops,Iterations, Repetitions

More Related