E N D
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
INF110 Visual Basic Programming AUBG Spring semester 2011 Lecture 08 Title: Visual Basic (Repetition/Iteration statements 2)
Lecture Contents: • Reminder on Decisions/Selections • Reminder on Repetitions/Iterations • Endless Loops • Loops with GOTO • Demo programs • Practical session
INF110 Visual Basic Programming Iterations Demo programs by J.Liberty, Chapter 06, 0609-0617 Plus Practical Session
Reminder On Decisions/Selections
VBasic selection statements include If condition Then statement sequence End If If condition Then statement sequence1 Else statement sequence2 End If
VBasic selection statements include If condition1 Then statement sequence1 ElseIf condition2 statement sequence2 • ElseIf condition3 • statement sequence3 • Else • statement sequence4 End If
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
Reminder On Iterations/Repetitions
Visual Basic supports the following loop structures: • Do … Loop • While … End While • For … Next
For i = 1 To 5 ... Next And its corresponding Do While equivalent i = 1 Do While i <= 5 ... i = i + 1 Loop
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
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
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
Examples I = 1 Do While (I <= 10) Console.WriteLine(I) I = I + 1 Loop How many iterations are to be run? (10) Try it
Examples I = 1 Do Until I <= 10 Console.WriteLine(I) I = I + 1 Loop How many iterations are to be run? (0) Try it
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
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
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
Examples I = 1 Do Console.WriteLine(I) I = I + 1 Loop While (I <= 10) How many iterations are to be run? (10) Try it
Examples I = 1 Do Console.WriteLine(I) I = I + 1 Loop Until (I <= 10) How many iterations are to be run? (1) Try it
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
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
Endless loops - examples While True statement-block End While
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
Endless loops - examples Do statement-block Loop
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
Example 6-9 • Creating Loops with Goto • Using Goto
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
Example 6-10 • The Do Loop • Using Do While
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
Example 6-11 • The Do Loop • Using Do Until
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
Example 6-12 • The Do Loop • Do … Loop While
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
Example 6-13 • The Do Loop • Breaking out of a Do Loop • Using Exit Do
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
Example 6-14 • The For Loop • Using a For loop
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
Example 6-15 • The For Loop • Loop with a Single counter
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
Example 6-16 • The For loop • Adjusting the step counter
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
Example 6-17 • The For Loop • Controlling a For Loop using Next • Multiple updates with one Next statement
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
Example Run the program. Test the program. Modify the program and rerun it again.