Lecture 9

Introduction to
Computing
Dr. Nadeem A Khan
Lecture 9
Select Case Blocks
►
Example:
Sub Command1_Click ( )
Picture1.Cls
Let num = Val(Text1.Text)
Select Case num
Case 1
Picture1.Print “Buckle my shoe.”
Case 2
Picture1.Print “Pick the sticks.”
Case 3,4,5,6
Picture1.Print “Lay them straight.”
Case Else
Picture1.Print “Start all over again.”
End Select
End Sub
Select Case Block
►
General form
Select Case selector
Case valueList1
action1
Case valueList2
action2
.
.
Case Else
action of last resort
End Select
Another example
Sub Command1_Click ( )
Dim x As Integer, y As Integer, num As Integer
Let x=2
Let y=3
Let num = Val(Text1.Text)
Select Case num
Case y-x, x
Picture1.Print “Buckle my shoe.”
Case Is<=4
Picture1.Print “Pick up sticks.”
Case x+y To x*y
Picture1.Print “Lay them straight.”
Case Else
Picture1.Print “Start all over again.”
End Select
End Sub
Sub Command1_Click ( )
Dim x As Integer, y As Integer, num As Integer
Let x=2
Let y=3
Let num = Val(Text1.Text)
Select Case num
Case y-x, x
Picture1.Print “Buckle my shoe.”
Case Is<=4
Picture1.Print “Pick up sticks.”
Case x+y To x*y
Picture1.Print “Lay them straight.”
Case Else
Picture1.Print “Start all over again.”
End Select
End Sub
Read Chapter 5 completely!
Chapter 7:
Array Variables (Arrays)
Array Variables (Arrays)
►
Array:
 A list of values can be assigned
 Collection of variables of the same
type
Array Variables (Arrays)
►
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)
► 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)
► What
is what?
Dim scores(1 To 3) as Single
scores( ):
scores(1):
scores(2):
scores(3):
1, 2, 3:
name of the array
first element
second element
third element
subscripts
Array Variables (Arrays)
► What
are the ranges or dimensions?
Dim scores(1 To 3) as Single
Dim names(1 To 10) as String
Arrays
► Value
assignment:
Dim names(1 To 3) as String
Let names(1)=“Aslam”
Let names(2)=“Khalid”
Let names(3)=“Akbar”
Picture1.Print names(3), names(2),names(1)
Arrays
►
Range of an Array:
Dim arrayName (1 to n) As varType
Dim arrayName (m to n) As varType
e.g:
Dim names(1 To 3) as String
Dim scores(10 To 20) as Single
=> Range of array need not begin with 1
Note:Dim value(5) As Integer =Dim value (0 To 5) As Integer
Arrays
► Example1:
Display the name of the student from a
class of 5 who got the rank specified by
the user
Arrays
Dim student(1 To 5) As String
Sub Form_Load ( )
Rem Fill data in array
Let student(1)= “Samina”
Let student(2)=“Akbar”
Let student(3)=“Rizwan”
Let student(4)=“Aslam”
Let student(5)=“Iram”
End Sub
Arrays
Sub Command1_Click( )
Dim n As Integer
Rem Access the student array
Let n=Val(Text1.Text)
Picture1.Cls
If n<=5 Then
Picture1.Print student(n); “ got rank”; n
Else
Picture1.Print “Rank”; n; “does not exist”
End If
End Sub
Arrays
=>
For repeated usage it is efficient to
declare a form-level array and
assign data once in the Form-Load
event
Arrays
► If
the amount of data is not known in
advance?
Arrays
► If
the amount of data is not known in
advance?
Solution 1: Declare an array that is large enough
and use part of it
Arrays
► If
the amount of data is not known in
advance?
Solution 2: Use
ReDim
(Dynamic Array)
Arrays
► ReDim
Declaration:
ReDim arrayName (1 to n ) As varType
e.g:
ReDim score(1 to numStudents) As Single
=> subscripts: variables and expressions
=> Can be placed only within a procedure
Arrays
► Declaration
as a Local variable:
ReDim arrayName (1 to n ) As varType
within a procedure
=> Created and erased in memory each time the
procedure is called
Arrays
► Declaration
as Form-Level variable:
 Place as General declaration:
Dim arrayName ( ) As varType
 Place in one procedure
ReDim arrayName (1 to n ) As varType
=>only after ReDim has been executed:
- its range is established
- useable only after it
Arrays
► Example
2:
Display the name of the student from a
class of who got the rank specified by the
user
Arrays
► Given
in example 2:
 The name of the students are in a file
(CLASS.TXT) arranged according to their
ranks
 Class strength is given as the first item of
the file
Arrays
Dim student( ) As String
Dim classStrength As Integer
Sub Form_Load ( )
Dim i As Integer
Rem Fill data in array
Open “CLASS.TXT” For Input As #1
Input #1, classStrength
ReDim student (1 To classStrength) As String
For i=1 To classStrength
Input #1, student(i)
Next i
Close #1
End Sub
Arrays
Sub Command1_Click( )
Dim n As Integer
Rem Access the student array
Let n=Val(Text1.Text)
Picture1.Cls
If n<=classStrength Then
Picture1.Print student(n); “ got rank”; n
Else
Picture1.Print “Rank”; n; “does not exist”
End If
End Sub
Arrays
► What
item?
if the class strength is not there as the first