Lists, Loops, - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Lists, Loops,

Description:

Input Boxes Provide a Simple Way to Gather Input Without ... Counters are invariably initialized before the loop begins (above: Dim count As Integer = 0) ... – PowerPoint PPT presentation

Number of Views:74
Avg rating:3.0/5.0
Slides: 32
Provided by: cecsC
Category:

less

Transcript and Presenter's Notes

Title: Lists, Loops,


1
  • Lists, Loops,
  • Validation, and More

2
  • This chapter covers the Visual Basic .NET looping
    statements
  • Do While
  • Do Until
  • For Next

3
Input Boxes
  • Input Boxes Provide a Simple Way to Gather Input
    Without Placing a Text Box on a Form

4
Format of the InputBox Function
InputBox(Prompt ,Title ,Default ,Xpos
,Ypos)
  • Prompt - message to the user
  • Title - text for the box's title bar
  • Default - default text for user's input
  • Xpos - X coordinate for the box's position
  • Ypos - Y coordinate for the box's position
  • Title and beyond are optional arguments

5
Sample InputBox Usage
  • userInput InputBox("Enter the distance.",
    "Provide a Value", "150")

6
Xpos, Ypos, and Twips
  • Xpos specifies the distance from the left of the
    screen to the left side of the box
  • Ypos, from the top of the screen to the top of
    the box
  • Both are specified in twips
  • One twip is 1/440 inch

7
The Do While Loop
  • A Loop Is Part of a ProgramThat Repeats

8
Repetition Structure (or Loop)
  • Visual Basic .NET has three structures for
    repeating a statement or group of statements
  • Do While
  • Do Until
  • For Next

9
Do While Flowchart
  • The Do While loop
  • If/While theexpression is true,the
    statement(s)are executed

Expression
statement(s)
True
False
10
Do While Syntax
  • "Do", "While", and "Loop" are new keywords
  • The statement, or statements are known as the
    body of the loop

Do While expression statement(s) Loop
11
Do While Example
Private Sub btnRunDemo_Click(ByVal sender As
System.Object, _ ByVal e As System.EventArgs)
Handles btnRunDemo.Click ' Demonstrate the Do
While loop Dim count As Integer 0 Do While
count lt 10 lstOutput.Items.Add("Hello") count
1 Loop End Sub
12
Infinite Loops
  • Generally, if the expression is true and, hence
    the starts executing
  • Something with the body of the loop must
    eventually make the test expression false
  • Otherwise, the Do While loop will continuously
    loop forever - called an infinite loop

13
Counters
  • Variables called counters are frequently used to
    control Do While loops (see count in the previous
    example
  • Counters are invariably initialized before the
    loop begins (above Dim count As Integer 0)
  • They are also usually modified within the body of
    the loop (above count 1)

14
Pretest vs. Posttest Loops
  • The preceding Do While loops were written in
    their pretest syntax
  • The expression is always tested before the body
    of the loop is executed
  • Do While loops also have a posttest form
  • In these, the body of the loop is always executed
    first, then the expression is evaluated to check
    to see if additional iterations are needed

15
Posttest Do WhileSyntax and Flowchart
Do statement(s) Loop While expression
statement(s)
  • The statement(s) willalways be done
    once,irrespective of theexpression used

Expression
True
False
16
Example Keeping a Running Total
count 1 ' Initialize the counter total
0 ' Initialize total Do input
InputBox("Enter the sales for day "
_ count.ToString, "Sales Amount Needed") If
input ltgt "" Then sales CDec(input) total
sales ' Add sales to total count 1 '
Increment the counter End If Loop While count lt
5
17
The Do Until andFor Next Loops
  • The Do Until Loop Iterates Until Its Test
    Expression Is True
  • The For...Next Loop Is Designed to Use a Counter
    Variable and Iterates a Specific Number of Times

18
Do Until Pretest and Posttest Forms
  • Pretest
  • Posttest

Do Until expression statement(s) Loop
Do statement(s) Loop Until expression
19
Pretest Do Until Example
input InputBox("How many test scores do you
want " _ "to average?", "Enter a
Value") numScores Val(input) total 0 count
1 Do Until count gt numScores input
InputBox("Enter the value for test score "
_ count.ToString, "Test Score Needed") total
total Val(input) count count 1 Loop
20
For Next Loop, I
  • The syntax is
  • Where 'For', 'To', and 'Next' are keywords
  • Other details follow

For CounterVariable StartValue To EndValue
_Step statement Next CounterVariable
21
For Next Loop, II
  • CounterVariable is a numeric counter variable
  • StartValue is the initial value of the counter
  • EndValue gives the number to test for completion
  • Step indicates the increment for count at the end
    of each iteration it is optional and defaults to
    1 if not specified

22
ForNext Flowchart
set counter to StartValue
Counter EndValue?
statement(s)
increment counter
False
True
23
ForNext Example
For count 1 To 10 square count 2 str
"The square of " count.ToString " is "
_ square.ToString lstOutput.Items.Add(str) Next
count
24
More on the StepValue
  • It is optional, if not specified, it defaults to
    1
  • It may be negative, in which case the loop counts
    downwards

For x 0 To 100 Step 10 MessageBox.Show("x is
now " x.ToString) Next x
For x 10 To 1 Step -1 MessageBox.Show("x is
now " x.ToString) Next x
25
Exiting a Loop Prematurely
  • In some situations it is convenient to be able
    to gracefully end a loop early
  • Exit Do (used in Do While or Until loops)
  • Exit For (used in For Next loops)
  • Will accomplish that task
  • Since these override the normal loop termination
    mechanism, they should be used sparingly

26
Exiting a Loop Prematurely, Example
maxNumbers CInt(InputBox("How many numbers do "
_ "you wish to sum?")) total 0 For x 1 to
maxNumbers input InputBox("Enter a
number.") If input "" Then Exit
For Else num CDbl(input) total num End
If Next x MessageBox.Show("The sum of the numbers
is " total.ToString)
27
When to Use the Do While Loop
  • Use the Do While loop when you wish the loop to
    repeat as long as the test expression is true
  • You can write the Do While loop as a pretest or
    posttest loop
  • Pretest Do While loops are ideal when you do not
    want the loop to iterate if the test expression
    is false from the beginning
  • Posttest loops are ideal when you always want the
    loop to iterate at least once

28
When to Use the Do Until Loop
  • Use the Do Until loop when you wish the loop to
    repeat until the test expression is true
  • You can write the Do Until loop as a pretest or
    posttest loop
  • Pretest Do Until loops are ideal when you do not
    want the loop to iterate if the test expression
    is true from the beginning
  • Posttest loops are ideal when you always want the
    loop to iterate at least once

29
When to Use the For Next Loop
  • The For...Next loop is a pretest loop that first
    initializes a counter variable to a starting
    value
  • It automatically increments the counter variable
    at the end of each iteration
  • The loop repeats as long as the counter variable
    is not greater than an end value
  • The For...Next loop is primarily used when the
    number of required iterations is known

30
Nested Loops
  • Nested Loops Are Necessary When a Task Performs a
    Repetitive Operation and That Task Itself Must Be
    Repeated

31
Nested Loop Example, I
For hours 0 To 24 lblHours.Text
hours.ToString For minutes 0 To
59 lblMinutes.Text minutes.ToString For
seconds 0 To 59 lblSeconds.Text
seconds.ToString Next seconds Next
minutes Next hours
Write a Comment
User Comments (0)
About PowerShow.com