INF110Lecture09

Report
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
1
INF110 Visual Basic Programming
AUBG Spring semester 2011
Lecture 08
Title:
Visual Basic
(Repetition/Iteration statements 2)
2
Lecture Contents:






Reminder on Decisions/Selections
Reminder on Repetitions/Iterations
Endless Loops
Loops with GOTO
Demo programs
Practical session
3
INF110
Visual Basic Programming
Iterations
Demo programs by J.Liberty,
Chapter 06, 0609-0617
Plus
Practical Session
4
Reminder
On
Decisions/Selections
5
VBasic selection statements include
If condition Then
statement sequence
End If
If condition Then
statement sequence1
Else
statement sequence2
End If
6
VBasic selection statements include
If condition1 Then
statement sequence1
ElseIf condition2
statement sequence2
ElseIf condition3
statement sequence3
Else
statement sequence4
End If
7
VBasic selection statements include
Select Case expression
Case value1
Block of one or
Case value2
Block of one or
Case value3
Block of one or
Case value4
.
.
.
Case Else
Block of one or
End Select
more VB statements
more VB Statements
more VB statements
more VB Statements
8
Reminder
On
Iterations/Repetitions
9
• Visual Basic supports the following loop structures:
Do … Loop
While … End While
For … Next
10
For i = 1 To 5
...
Next
And its corresponding
Do While
equivalent
i = 1
Do While i <= 5
...
i = i + 1
Loop
11
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
12
•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
13
Pre test loops
with While and Until
14
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
15
Examples
I=1
Do While (I <= 10)
Console.WriteLine(I)
I=I+1
Loop
How many iterations are to be run? (10) Try it
16
Examples
I=1
Do Until I <= 10
Console.WriteLine(I)
I=I+1
Loop
How many iterations are to be run? (0) Try it
17
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
18
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
19
Post test loops
with While and Until
20
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
21
Examples
I=1
Do
Console.WriteLine(I)
I=I+1
Loop While (I <= 10)
How many iterations are to be run? (10) Try it
22
Examples
I=1
Do
Console.WriteLine(I)
I=I+1
Loop Until (I <= 10)
How many iterations are to be run? (1) Try it
23
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
24
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
25
Endless loops
with
While … End While
and
Do … Loop
26
Endless loops - examples
While True
statement-block
End While
27
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
28
Endless loops - examples
Do
statement-block
Loop
29
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
30
Example 6-9
• Creating Loops with Goto
– Using Goto
31
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
32
Example 6-10
• The Do Loop
– Using Do While
33
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
34
Example 6-11
• The Do Loop
– Using Do Until
35
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
36
Example 6-12
• The Do Loop
– Do … Loop While
37
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
38
Example 6-13
• The Do Loop
– Breaking out of a Do Loop
• Using Exit Do
39
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
40
Example 6-14
• The For Loop
– Using a For loop
41
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
42
Example 6-15
• The For Loop
– Loop with a Single counter
43
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
44
Example 6-16
• The For loop
– Adjusting the step counter
45
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
46
Example 6-17
• The For Loop
– Controlling a For Loop using Next
• Multiple updates with one Next statement
47
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
48
Example
Run the program.
Test the program.
Modify the program and rerun it again.
49
Practical session 3
Loops,
Iterations, Repetitions
50
Practical task 1
Write a program that requests a number
from 1 to 20, and then displays a row of
that many asterisks.
51
Practical task 2
Write a program that displays a 10 by 10
table (i.e. 10 rows by 10 columns) of
asterisks.
52
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.
53
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.
54
Practical task 4
Suppose you deposit money into a
account and let it accumulate at 6
interest rate compounded annually.
Write a program that reads your
deposited and then displays when
many years) you will be a millionaire.
savings
percent
amount
(in how
Test the program with initial amount values like
$10 000, $20 000, $100 000.
55
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.
56
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.
57
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%.
58
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.
59
Questions?
60
Thank You
For
Your Attention!
61

similar documents