Download presentation
Presentation is loading. Please wait.
1
Introduction to Computing Dr. Nadeem A Khan
2
Lecture 9
3
Select Case Blocks
4
► 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
5
Select Case Block ► General form Select Case selector Case valueList1 action1 Case valueList2 action2.. Case Else action of last resort End Select
6
Another example
7
Sub Command1_Click ( ) 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
8
Sub Command1_Click ( ) 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
9
Read Chapter 5 completely!
10
Chapter 7: Array Variables (Arrays)
11
► Array: A list of values can be assigned Collection of variables of the same type Array Variables (Arrays)
12
► 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)
13
► 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)
14
► 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)
15
► What are the ranges or dimensions? Dim scores(1 To 3) as Single Dim names(1 To 10) as String Array Variables (Arrays)
16
► 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
17
► 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
18
► Example1: Display the name of the student from a class of 5 who got the rank specified by the user Arrays
19
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
20
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
21
=> For repeated usage it is efficient to declare a form-level array and assign data once in the Form-Load event Arrays
22
► If the amount of data is not known in advance? Arrays
23
Solution 1: Declare an array that is large enough and use part of it Arrays
24
► If the amount of data is not known in advance? Solution 2: Use ReDim (Dynamic Array) Arrays
25
► 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
26
► 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 procedure is calledArrays
27
► 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
28
► Example 2: Display the name of the student from a class of who got the rank specified by the user Arrays
29
► 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
30
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
31
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
32
► What if the class strength is not there as the first item? Arrays
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.