Title: The Repetition Process in VB .Net
1The Repetition Process in VB .Net
2The Repetition Process
- The capability to repeat one or more statements
as many times as necessary is what really sets a
computer apart from other devices - All loops have two parts
- the body of the loops (the statements being
repeated) - a termination condition that terminates the loop
- Failure to have a valid termination condition can
lead to an endless loop
3Deteminate Loops Using For-next Loop
- Best way to create a determinate loop is to use a
For-Next Loop - Form of For-Next Loop
- For variable start value to end value Step
change value - statements that compose body of loop
- Next variable
- where variable the counter variable in the
loop - start value the beginning value of the
counter variable - end value the ending value of the counter
variable - change value the amount the counter
variable changes each time through the loop - Next variable the end of the For loop
4Example of For-Next Loop
5Rolling Dice with a For Loop
- ltOption Explicitgt
- ltHTMLgt
- lt Dim DiceNum, DiceOne, DiceTwo, DiceSum
- Randomize Always need to use Rnd function
- For DiceNum 1 to 5 Make five rolls of dice
- DiceOne Int(Rnd6) 1 Compute result of
first die - DiceTwo Int(Rnd6) 1 Compute result of
second die - DiceSum DiceOne DiceTwo Find sum of dice
- gt
- On Roll lt DiceNum gt you rolled a lt DiceSum
gtltpgt Display results - ltNextgt
- lt/HTMLgt
- Save as rolldice.asp and add appropriate link
on default.asp page. - Run this multiple times by clicking Refresh
button to see changing results
6Indeterminate Loops
- Indeterminate loops run for an unknown number of
repetitions until a condition is true or while a
condition is true - The condition that is true or false is known as
the termination condition - Pre-Test loops have termination condition before
loop body - Post-test loops have termination condition after
loop body
7Form of Pre- and Post-Test Loops
- The form of the pre-test loops is
- Do Until (or While) condition
- body of loop
- Loop
- The form of the post-test loops is
- Do
- body of loop
- Loop Until (or While) condition
8Pre and Post-Test Loops
9How many rolls for a 7 or 11?
- ltOption Explicitgt
- ltHTMLgt
- lt Dim DiceNum, DiceOne, DiceTwo, DiceSum
- Randomize
- DiceNum 0
- DiceSum 0
- Do Until DiceSum 11 or dicesum 7
- DiceOne Int(Rnd6) 1
- DiceTwo Int(Rnd6) 1
- DiceSum DiceOne DiceTwo
- DiceNum DiceNum 1
- gt
- On Roll lt DiceNum gt you rolled a lt DiceSum
gtltpgt - ltloopgt
- It look lt DiceNum gt rolls to hit a 7 or 11ltpgt
- lt/HTMLgt
- Save file as diceroll7or11.asp and add an
appropriate link in default.asp to reference it.
Run your program multiple times to see the
result.
10Checking the Rule of 72
ltOption Explicitgt ltHTMLgt ltDim NumYears,
Principal, Amount, IntRate Principal
10000 IntRate 9 NumYears 0 Amount
Principalgt The Principal is ltPrincipalgtltbrgt Th
e interest rate is ltIntrategt.ltbrgt ltDo While
Amount lt 2 Principal NumYears NumYears
1 Amount Principal (1 Intrate/100)NumYears
gt After Year ltNumYearsgt , the compound
amount is ltformatcurrency(Amount)gtltbrgt ltLoop
gt It took ltNumYearsgt years to at least
double the principal.ltbrgt The current amount is
ltformatcurrency(Amount)gtltbrgt lt/HTMLgt
11Nested Loops
- A Nested loop is a loop within a loop. Must
complete the inner loop within the outer loop. - Nested For-Next loops have a For-Next loop within
a For-Next loop in which the inner loop will go
through all its values for each value of the
outer loop. - Three key programming rules to remember about
using nested For-Next loops - Always use different counter variables for the
outer and inner For-Next loops. - Always have the Next statement for the inner
For-Next loop before the Next statement for the
outer For-Next loop. - Always include the counter variable in the Next
statements to distinguish between the loops.
12Debugging Loops
- Debug a loop by watching a variable of interest
- Add the command in the loop to print an important
variable - Run the program and compare the printed values
with those you expect to see - If different, this indicates a bug in program
that needs to be fixed
13Using Arrays in ASP.NET
- Arrays are declared in the same way as in VB6
except that you cannot declare an array to start
anywhere except 0 - This means you cannot declare an array as
- Parts(10 to 20)
- It must be declared as Parts(20) which will have
21 elements
14Using For-Next Loops with Arrays
- In VB6, you had to use a For-Next loop to work
with or search an array. - For example, if you had an array named SECEast
declared to have an upper limit of 5 (this still
gives you 6 elements) in which you stored the 6
teams of the SEC East Division, you would search
this array for UGA with the following code - ltFor Counter 0 to 5
- If SECEast(Counter) Georgia then
- Found True
- Exit For
- End if
- Next
- If Found True thengt
- Georgia is still in the SEC East!
- ltEnd ifgt
- The entire program is shown in the next slide
-
15lt_at_Page languageVB DebugTrue
ExplicitTrue lthtmlgtltbodygt ltDim SECEast(5) as
String,School as Integer Dim Found as
Boolean SECEast(0) Florida SECEast(1)
UGA SECEast(2) Kentucky SECEast(3)
Tennessee SECEast(4) South
Carolina SECEast(5) Vanderbilt Found
False For School 0 to 5 If SECEast(Counter)
Georgia then Found True Exit For End
if Next If Found True thengt Georgia is still
in the SEC East! ltend ifgt lt/bodygtlt/htmlgt
16A New Loop for Arrays
- Instead of using the For-Next loop for arrays,
VB.NET has the For-Each loop - The form of this loop is
- For Each variable In Array
- Processing of array elements
- Next
- For the previous example, this would be used as
shown on next page
17lt_at_Page languageVB DebugTrue
ExplicitTrue lthtmlgtltbodygt ltDim SECEast(5) as
String,School as String Note difference Dim
Found as Boolean SECEast(0) Florida SECEast(1)
UGA SECEast(2) Kentucky SECEast(3)
Tennessee SECEast(4) South
Carolina SECEast(5) Vanderbilt Found
False For Each School in SECEast If School
Georgia then Found True Exit For End
if Next If Found True thengt Georgia is still
in the SEC East! ltend ifgt lt/bodygtlt/htmlgt
18For-Next vs For-Each
- Use For-Next for processing if the declared
number is much greater than the actual number in
array - Use For-Each for searching since you exit loop
once you have a hit - For-each is good for when the number in the array
varies from application to application since you
dont use this number