CS 4 Intro to Programming using Visual Basic - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

CS 4 Intro to Programming using Visual Basic

Description:

Multiplication Table. Uthaisombut & Schneider. 18. Multiplication Table. Private Sub btnDisplay_Click(...) Handles btnDisplay.Click ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 25
Provided by: cwy73
Category:

less

Transcript and Presenter's Notes

Title: CS 4 Intro to Programming using Visual Basic


1
CS 4 Intro to Programming using Visual Basic
  • For Loops
  • Patchrawat UthaisombutUniversity of Pittsburgh

based on lecture notes by D. Schneider
2
Chapter 6 Repetition
  • 6.1 Do Loops
  • 6.2 Processing Lists of Data with Do
  • Loops
  • 6.3 For...Next Loops
  • 6.4 A Case Study Analyze a Loan

3
6.3 ForNext Loops
  • Declarations of Control Variables
  • Nested For Next Loops

4
ForNext Loops
  • Used when we know how many times we want the loop
    to execute
  • A counter controlled loop

5
Sample
  • Dim i As Integer
  • For i 1 To 5
  • lstTable.Items.Add(i " " i 2)
  • Next
  • The loop control variable, i, is
  • Initialized to 1
  • Tested against the stop value, 5
  • Incremented by 1 at the Next statement

6
Do While equivalent
  • i 1
  • Do While i lt 5
  • lstTable.Items.Add(i " " i 2)
  • i 1
  • Loop

7
ForNext Loop Syntax
8
Set control variableto initial value
Is control variable gtterminating value?
Yes
No
ExecuteStatementsin loop body
Incrementcontrol variable
Executestatementsfollowing the loop
9
Example 1 Output
10
Example 1 Code
  • Dim yr As Integer
  • Dim pop as Double 300000
  • Dim fmtStr As String _
  • "0,41,12N0"
  • For yr 2006 To 2010 lstPop.Items.Add(String.Form
    at( _
  • fmtStr, yr, pop)
  • pop 0.03 pop
  • Next

11
Example 2
Start value
Control variable
Stop value
Amount to add to index
  • For index 0 To n Step s
  • lstValues.Items.Add(index)
  • Next

12
Example with Negative Step
  • Dim j As Integer
  • For j 10 To 1 Step -1
  • lstBox.Items.Add(j)
  • Next
  • lstBox.Items.Add("Blastoff")

13
Declaration Inside For Statement
  • Dim i As Integer
  • For i 1 To 5
  • lstTable.Items.Add(i " " i 2)
  • Next
  • For i As Integer 1 To 5
  • lstTable.Items.Add(i " " i 2)
  • Next

14
Playing with For Loop
15
Playing with For Loop
  • Private Sub btnDisplay_Click() Handles
    btnDisplay.Click
  • Dim n, s As Double
  • Dim index As Double
  •  
  • n CDbl(txtN.Text)
  • s CDbl(txtStep.Text)
  •  
  • lstDisplay.Items.Clear()
  •  
  • For index 0 To n Step s
  • lstDisplay.Items.Add(index)
  • Next
  • End Sub

16
Nested ForNext Loops
17
Example Seats on an Airplane
  • For i As Integer 65 To 70
  • For j As Integer 1 To 25
  • lstBox.Items.Add(Chr(i) j)
  • Next
  • Next
  • OUTPUT A1
  • A2
  • A3

Outer loop
Inner loop
18
Multiplication Table
19
Multiplication Table
  • Private Sub btnDisplay_Click() Handles
    btnDisplay.Click
  • Dim row, entry As String
  •  
  • lstTable.Items.Clear()
  • For j As Integer 1 To 5
  • row ""
  • For k As Integer 1 To 3
  • entry j " x " k " " (j
    k)
  • row entry " "
  • Next
  • lstTable.Items.Add(row)
  • Next
  • End Sub

20
For and Next Pairs
  • For and Next statements must be paired.
  • If one is missing, the automatic syntax checker
    will complain with a wavy underline and a message
    such as
  • A For must be paired with a Next.

21
Start, Stop, and Step values
  • Consider a loop beginning with
  • For i As Integer m To n Step s.
  • The loop will be executed exactly once if m
    equals n no matter what value s has.
  • The loop will not be executed at all if m is
    greater than n and s is positive,
  • or if m is less than n and s is negative.

22
Altering the Control Variable
  • The value of the control variable should not be
    altered within the body of the loop.
  • Doing so might cause the loop to repeat
    indefinitely or have an unpredictable number of
    repetitions.

23
Non-integer Step Values
  • Can lead to round-off errors with the result that
    the loop is not executed the intended number of
    times.

24
Non-integer Step Example
  • A loop beginning with
  • For i As Double 1 To 2 Step 0.1
  • will be executed only 10 times instead of the
    intended 11 times. It should be replaced with
  • For i As Double _
  • 1 To 2.01 Step 0.1
Write a Comment
User Comments (0)
About PowerShow.com