Download presentation
Presentation is loading. Please wait.
Published byEsmond Cummings Modified over 9 years ago
2
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference 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 library Course lecturer: Assoc. Prof. Svetla Boytcheva, PhD
3
2 INF110 Visual Basic Programming AUBG Spring semester 2011 Lecture 08 Title: Visual Basic (Repetition/Iteration statements 2)
4
3 Lecture Contents: §Reminder on Decisions/Selections §Reminder on Repetitions/Iterations §Endless Loops §Loops with GOTO §Demo programs §Practical session
5
4 Iterations Demo programs by J.Liberty, Chapter 06, 0609-0617 Plus Practical Session INF110 Visual Basic Programming
6
5 ReminderOnDecisions/Selections
7
6 VBasic selection statements include If condition Then statement sequence End If If condition Then statement sequence1 Else statement sequence2 End If
8
7 VBasic selection statements include If condition1 Then statement sequence1 ElseIf condition2 statement sequence2 ElseIf condition3 statement sequence3 Else statement sequence4 End If
9
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
10
9 ReminderOnIterations/Repetitions
11
10 Visual Basic supports the following loop structures: Do … Loop While … End While For … Next
12
11 For i = 1 To 5... Next And its corresponding Do While equivalent i = 1 Do While i <= 5... i = i + 1 Loop
13
12 (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 Examples
14
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
15
14 Pre test loops with While and Until
16
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
17
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
18
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
19
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
20
19 Examples to achieve the same effect I = 1 Do While Not(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
21
20 Post test loops with While and Until
22
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
23
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
24
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
25
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 Until Not(I <= 10) How many iterations are to be run? (10) Try it
26
25 Examples to achieve the same effect I = 1 Do Console.WriteLine(I) I = I + 1 Loop While Not(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
27
26 Endless loops with While … End While and Do … Loop
28
27 Endless loops - examples While True statement-block End While
29
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
30
29 Endless loops - examples Do statement-block Loop
31
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
32
31 Example 6-9 Creating Loops with Goto –Using Goto
33
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
34
33 Example 6-10 The Do Loop –Using Do While
35
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
36
35 Example 6-11 The Do Loop –Using Do Until
37
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
38
37 Example 6-12 The Do Loop –Do … Loop While
39
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
40
39 Example 6-13 The Do Loop –Breaking out of a Do Loop Using Exit Do
41
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
42
41 Example 6-14 The For Loop –Using a For loop
43
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
44
43 Example 6-15 The For Loop –Loop with a Single counter
45
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
46
45 Example 6-16 The For loop –Adjusting the step counter
47
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
48
47 Example 6-17 The For Loop –Controlling a For Loop using Next Multiple updates with one Next statement
49
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
50
49 Example Run the program. Test the program. Modify the program and rerun it again.
51
50 Practical session 3 Loops, Iterations, Repetitions
52
51 Practical task 1 Write a program that requests a number from 1 to 20, and then displays a row of that many asterisks.
53
52 Practical task 2 Write a program that displays a 10 by 10 table (i.e. 10 rows by 10 columns) of asterisks.
54
53 Practical task 3 You are offered two salary options for ten days of work. Option 1: $100 per day. Option 2: $2 the first day, $4 the second day, $8 the third day, $16 the fourth day and so on, with the amount doubling each day. Write a program to determine which option pays better.
55
54 Practical task 4 Write a program that reads in 10 student test marks (percentages) and then displays the average mark, the lowest mark and the highest mark. Use these marks: 87, 56, 73, 94, 89, 82, 85, 78, 43, 89. The list is terminated by –1.
56
55 Practical task 4 Suppose you deposit money into a savings account and let it accumulate at 6 percent interest rate compounded annually. Write a program that reads your amount deposited and then displays when (in how many years) you will be a millionaire. Test the program with initial amount values like $10 000, $20 000, $100 000.
57
56 Practical task 5 Write a program that reads in an odd number (between 11 and 19) and displays an inverted triangle of asterisks on the screen such that the input number is the number of asterisks in the top row, and with one asterisk in the bottom row.
58
57 Practical task 6 Write a program to display all the numbers between 1 and 100 that are part of the Fibonacci sequence. The Fibonacci sequence begins 1, 1, 2, 3, 5, 8, 13, …, where each new number in the sequence is found by adding the previous two numbers in the sequence.
59
58 Practical task 7 Write a program that requests an exam grade and displays the equivalent percentage mark range. Use the scale: A = 90%-100%; B = 80%-89%; … F = 0%-59%.
60
59 Practical task 8 Write a program that reads a number between 11 and 20 and displays a triangle of asterisks on the screen. If the input number is odd, then this is the number of asterisks in the top row – the triangle is inverted. If the input number is even, then this is the number of asterisks in the bottom row.
61
60 Questions?
62
61 Thank You For Your Attention!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.