For...Next Statements - PowerPoint PPT Presentation

About This Presentation
Title:

For...Next Statements

Description:

For num = 1 To 99 Step 2. sum = sum num. Next num. picOutput. ... For Words = 10 To 1 Step -1. For Chars = 0 To 9. MyString = MyString & Chars. Next Chars ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 9
Provided by: Computer9
Learn more at: http://www.cs.uwyo.edu
Category:

less

Transcript and Presenter's Notes

Title: For...Next Statements


1
For...Next Statements
2
For...Next Syntax
  • Repeats a group of statements a specified number
    of times.
  • For counter start To end Step step
  • statements
  • Exit For
  • statements
  • Next counter

3
For...Next Statement
  • When the number of times a loop should be
    executed is known in advance, a ForNext loop
    should be used.
  • For i m To n
  • statement(s)
  • Next i

initial value
terminating value
control variable (counter)
body
4
ForNext Example
Private Sub cmdDisplay_Click()
Dim i As Integer 'Display a row of 10 stars
picOutput.Cls For i 1 To 10 picOutput.
Print "" Next i End Sub
5
ForNext Example
Private Sub cmdDisplay_Click()
Dim sum As Single, num As Integer
sum 0 For num 1 To 99 Step 2 sum s
um num Next num picOutput.Print "The sum i
s" sum End Sub
6
Private Sub cmdReverse_Click()
Dim m As Integer, j As Integer
Dim temp As String, strWord as String
m Len(txtWord.Text) strWord txtWord.Text
temp "" For j m To 1 Step -1 temp
temp Mid(strWord, j, 1) Next j picTranspos
e.Print temp End Sub
7
Nested For...Next Example
  • Private Sub cmdDisplay_Click()
  • Dim Words, Chars As Integer
  • Dim MyString As String
  • MyString
  • For Words 10 To 1 Step -1
  • For Chars 0 To 9
  • MyString MyString Chars
  • Next Chars
  • MyString MyString
  • Next Words
  • picOutput.Print MyString
  • End Sub

8
Private Sub cmdDisplay_Click()
Dim j As Integer, k As Integer
picTable.Cls For j 1 To 4 For k 1 To
4 picTable.Print j "x" k "" j k,
Next k picTable.Print Next j End Sub
Write a Comment
User Comments (0)
About PowerShow.com