Download presentation
Presentation is loading. Please wait.
1
Introduction to Computing Dr. Nadeem A Khan
2
Lecture 24
3
► The general format with step of 1: For i = m To n Statements Next i For…Next Loops
4
► Convert to For.. Next Loop Sub Command1_Click ( ) Dim num As Integer Let num=1 Do While num<=10 Picture1.Print num; Let num=num+1 Loop End Sub For Next Loops
5
► Example with For.. Next Loop Sub Command1_Click ( ) Dim num As Integer For num=1 To 10 Picture1.Print num; Next num End Sub For Next Loops
6
► The result: 1 2 3 4 5 6 7 8 9 10 For Next Loops
7
► The general format with steps: For i = m To n Step s Statements Next i => Each time increment: i = i + s; m, n, s could be numeric expressions m, n, s could be numeric expressions For…Next Loops
8
► Example with For.. Next Loop with steps Sub Command1_Click ( ) Dim num As Integer For num= 1 To 10 Step 2 Picture1.Print num; Next num End Sub The output? For Next Loops
9
► The result: 1 3 5 7 9 For Next Loops
10
► Rewrite the following program using nested For.. Next Loop Nested Loops: For…Next
11
Sub Command1_Click ( ) Dim num As Integer, counter As Integer Let counter=1 Do While counter<=4 Let num=1 Let num=1 Do While num<=10 Picture1.Print num; Let num=num+1 Loop Let counter=counter+1 Picture1.PrintLoop End Sub Nested Loops: For…Next
12
Sub Command1_Click ( ) Dim num As Integer, counter As Integer For counter = 1 To 4 For num =1 To 10 For num =1 To 10 Picture1.Print num; Next num Picture1.Print Next counter End Sub Nested Loops: For…Next
13
► The result: 1 2 3 4 5 6 7 8 9 10 Nested Loops
14
► Reverses the input string Sub Command1_Click ( ) Picture1.Cls Picture1.Print Reverse$((Text1.Text)) End Sub For Next Loops
15
Function Reverse$ (info As String) Dim m As Integer, j As Integer, temp As String Let temp = “” For j = Len(info) To 1 Step -1 Let temp = temp + Mid$(info, j, 1) Next Reverse$ = temp End Function For Next Loops
16
► Exit For: Works similar as Exit Do Exit For
17
Note: ► Read: Schneider Chapter 6, Warner Chapter 6 ► Read comments and do practice problems of these sections of these sections ► Attempt some questions of Exercises
18
Chapter 7: Array Variables (Arrays)
19
► Array: A list of values can be assigned Collection of variables of the same type Array Variables (Arrays)
20
► Array Declaration: Dim arrayName (1 to n) As varType e.g: Dim names(1 To 3) as Strings Dim scores(1 To 10) as Single Array Variables (Arrays)
21
► Value assignment: Dim names(1 To 3) as Strings Let names(1)=“Aslam” Let names(2)=“Khalid” Let names(3)=“Akbar” Picture1.Print names(3), names(2),names(1) Array Variables (Arrays)
22
► What is what? Dim scores(1 To 3) as Single scores( ): name of the array scores(1): first element scores(2): second element scores(3): third element 1, 2, 3: subscripts Array Variables (Arrays)
23
► What are the ranges or dimensions? Dim scores(1 To 3) as Single Dim names(1 To 10) as String Array Variables (Arrays)
24
End
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.