Introduction to Introduction to ComputingComputing
Dr. Nadeem A KhanDr. Nadeem A Khan
Lecture 24Lecture 24
► The general format with step of 1:The general format with step of 1:
For i = m To nFor i = m To nStatementsStatementsNext iNext i
For…Next Loops For…Next Loops
► Convert to For.. Next LoopConvert to For.. Next Loop
Sub Command1_Click ( )Sub Command1_Click ( )Dim num As IntegerDim num As IntegerLet num=1Let num=1Do While num<=10Do While num<=10
Picture1.Print num;Picture1.Print num;Let num=num+1Let num=num+1
LoopLoopEnd Sub End Sub
For Next Loops For Next Loops
► Example with For.. Next LoopExample with For.. Next Loop
Sub Command1_Click ( )Sub Command1_Click ( )Dim num As IntegerDim num As IntegerFor num=1 To 10For num=1 To 10
Picture1.Print num;Picture1.Print num;Next numNext numEnd Sub End Sub
For Next Loops For Next Loops
►The result:The result:
1 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 10
For Next Loops For Next Loops
► The general format with steps:The general format with steps:
For i = m To n Step sFor i = m To n Step sStatementsStatementsNext iNext i
=> Each time increment: i = i + s; => Each time increment: i = i + s; m, n, s could be numeric m, n, s could be numeric
expressions expressions
For…Next Loops For…Next Loops
► Example with For.. Next Loop with Example with For.. Next Loop with stepssteps
Sub Command1_Click ( )Sub Command1_Click ( )Dim num As IntegerDim num As IntegerFor num= 1 To 10 Step 2For num= 1 To 10 Step 2
Picture1.Print num;Picture1.Print num;Next numNext numEnd Sub End Sub
The output?The output?
For Next Loops For Next Loops
►The result:The result:
1 3 5 7 91 3 5 7 9
For Next Loops For Next Loops
► Rewrite the following program using Rewrite the following program using nested For.. Next Loopnested For.. Next Loop
Nested Loops: For…Next Nested Loops: For…Next
Sub Command1_Click ( )Sub Command1_Click ( )Dim num As Integer, counter As IntegerDim num As Integer, counter As IntegerLet counter=1Let counter=1
Do While counter<=4Do While counter<=4 Let num=1Let num=1
Do While num<=10Do While num<=10Picture1.Print num;Picture1.Print num;Let num=num+1Let num=num+1
LoopLoopLet counter=counter+1Let counter=counter+1Picture1.PrintPicture1.PrintLoopLoopEnd Sub End Sub
Nested Loops: For…Next Nested Loops: For…Next
Sub Command1_Click ( )Sub Command1_Click ( )Dim num As Integer, counter As Dim num As Integer, counter As
IntegerIntegerFor counter = 1 To 4 For counter = 1 To 4
For num =1 To 10For num =1 To 10Picture1.Print num;Picture1.Print num;
Next numNext numPicture1.Print Picture1.Print
Next counterNext counterEnd Sub End Sub
Nested Loops: For…Next Nested Loops: For…Next
►The result:The result:
1 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 10
Nested Loops Nested Loops
► Reverses the input stringReverses the input string
Sub Command1_Click ( )Sub Command1_Click ( )Picture1.ClsPicture1.ClsPicture1.Print Reverse$((Text1.Text))Picture1.Print Reverse$((Text1.Text))End Sub End Sub
For Next Loops For Next Loops
Function Reverse$ (info As String)Function Reverse$ (info As String)Dim m As Integer, j As Integer, temp As Dim m As Integer, j As Integer, temp As
StringStringLet temp = “”Let temp = “”For j = Len(info) To 1 Step -1For j = Len(info) To 1 Step -1
Let temp = temp + Mid$(info, j, 1)Let temp = temp + Mid$(info, j, 1)NextNextReverse$ = tempReverse$ = tempEnd FunctionEnd Function
For Next Loops For Next Loops
►Exit For: Works similar as Exit DoExit For: Works similar as Exit Do
Exit For Exit For
Note:Note:
►Read: Schneider Chapter 6, Warner Read: Schneider Chapter 6, Warner Chapter 6 Chapter 6
►Read comments and do practice Read comments and do practice problemsproblems
of these sectionsof these sections►Attempt some questions of ExercisesAttempt some questions of Exercises
Chapter 7:Chapter 7:Array Variables (Arrays) Array Variables (Arrays)
► Array:Array:
A list of values can be assignedA list of values can be assigned
Collection of variables of the Collection of variables of the same typesame type
Array Variables (Arrays) Array Variables (Arrays)
► Array Declaration:Array Declaration:
Dim Dim arrayName arrayName (1 to n) As (1 to n) As varTypevarType
e.g:e.g:
Dim names(1 To 3) as StringsDim names(1 To 3) as Strings
Dim scores(1 To 10) as SingleDim scores(1 To 10) as Single
Array Variables (Arrays) Array Variables (Arrays)
►Value assignment: Value assignment:
Dim names(1 To 3) as StringsDim names(1 To 3) as Strings
Let names(1)=“Aslam”Let names(1)=“Aslam”Let names(2)=“Khalid”Let names(2)=“Khalid”Let names(3)=“Akbar”Let names(3)=“Akbar”Picture1.Print names(3), Picture1.Print names(3),
names(2),names(1)names(2),names(1)
Array Variables (Arrays) Array Variables (Arrays)
►What is what? What is what?
Dim scores(1 To 3) as SingleDim scores(1 To 3) as Single
scores( ): scores( ): name of the arrayname of the arrayscores(1): scores(1): first elementfirst elementscores(2): scores(2): second elementsecond elementscores(3): scores(3): third elementthird element1, 2, 3: 1, 2, 3: subscriptssubscripts
Array Variables (Arrays) Array Variables (Arrays)
►What are the What are the rangesranges or or dimensionsdimensions? ?
Dim scores(1 To 3) as SingleDim scores(1 To 3) as Single
Dim names(1 To 10) as String Dim names(1 To 10) as String
Array Variables (Arrays) Array Variables (Arrays)
End End
Top Related