You are on page 1of 96

Programming Concepts

Chapter 11
Data Type

The fundamental data type in Visual Basic including variant are


integer, long, single, double, string, currency, byte and Boolean.

Visual Basic supports a vast array of data types. Each data types
has limits to the kind of information and the minimum and
maximum values it can hold.
Exercise #1
Kindly specify the
appropriate Data type
for each fieldname in
the Bio-data form at
the right side.
START / END

Putting data into an algorithm


INPUT / OUTPUT Displaying data from an algorithm to the user.
OUTPUT by display the variable that contains
the result like sum=n1 + n2

COMPUTATION, INITIALISE VARIABLES,


ASSIGN A VALUE TO A VARIABLE…
PROCESS
An action is performed, a change is being
made to something like subtract or add

DECISION and SELECTION


CONDITION Usually use comparison and selection of two
sides or variable that contains a value like:
10 > 5 or N1 < N2
Example:

Create a flowchart that will ask the user to enter the score of a
student for each of the 5 Quizzes having a perfect score of 10 each.
Using Totalling get the sum of the scores of the 5 Quizzes.
Example:
Create a flowchart to compute for the score of a student answering
a quiz with 10 questions and for every correct answer the student
gets 2 points. Display the total score of the student for the final
result.
Assignment

Please study for our Quiz# 1 next meeting


IT Coursebook Chapter 11 p. 153-157
IT Programming Book Chapter 5 p.51-58
Seatwork
Determine the shape for each procedure
1. N=0 and count=1
2. If num MOD 2 = 1
3. Display ODD
4. Score = Score + 2
5. Count = Count + 1
6. Average = num1 + num2 + num3/3
7. Num1 ^ Num2
8. Is Num1 => Num2
9. Num1 DIV Num2
10. Output Result
Create a program solution in a flowchart that will ask the user
to enter a number and determine if the number is ODD of
EVEN.
Create a program solution using flowchart that will determine which
number is of higher value between two numbers entered and
determine if both numbers are equal.
Create a program solution using flowchart that will ask the user to
enter 10 numbers and will display all even numbers only among
the numbers entered.
Assignment:

Please study and read ahead p. 154 - 159


● Predefined Functions and Procedures
● Library
● Array
Examples
Predefined Functions
n= q1 + q2 + q3
Average (n)

string1= firstname; string2 = familyname;


con(string1 + string2)
P.154
Pre-defined Functions

https://bettersolutions.com/vba/functions/complete-list.htm

These functions are already defined in the system libraries. Programmer will reuse the already
present code in the system libraries to write error free code. But to use the library functions, user
must be aware of syntax of the function.

A pre-defined function is built into the software and does not need to be created by a programmer. Pre-defined
functions often exist to carry out common tasks, such as:

● finding an average number


● determining the length of a string
Pre-defined Procedure

Is made-up of block of codes that performs a specific task however,


does not return a value. Like print, clear screen, end the program.

• End sub()
• Print
• Clear screen
Libraries: DLL, in full dynamic link library, file containing code for commonly
used program functions on personal computers (PCs) that run the Microsoft Corporation’s
Windows operating system.

Linking is part of the process of creating a computer program in which programmers


combine their new program codes with preexisting code libraries (special functions, such
as printing a document, that are used often). Static linking, the process traditionally used
in many operating systems, puts everything together into the executable program.
Dynamic linking, on the other hand, stores code libraries in DLL files. The functions in
these files are then accessed by different running programs only when needed. Dynamic
linking results in programs that use less memory and disk space and that are easier to
upgrade. Without dynamic linking, making changes to part of a code library—for
example, a dialog box for saving a file—would mean making changes to every statically
linked program that uses it. With dynamic linking, only the DLL needs to be changed.
Programming Concepts Using
Microsoft Visual Basic 2010 Express
Array
Review of the previous lesson covered: Operators
Arithmetic Operators:
Relational Operators

< Less Than

> Greater Than

= Equal

<> Not Equal

<= Less Than Equal

>= Greater Than Equal


Logical Operators
OR

AND

NAND

NOR

XOR

XNOR
Array:
A store of data values that are all related and are of the same data type.

Element:

An individual data location in an Array


Array - is a way of storing data that is all related and of the
same type like: list of the names of the students in a class can
be stored in an array.

Each item stored in an array can be accessed by referring to its


location within the array.

Dim Student_name(5) As String

Student_name[0] = Andy
:
:
Student_name[5] = Joshua
Example 1:
Dim Student_name(5) As String

Student_name[0] = Yesika
Student_name[1] = Nikolas
Student_name[2] = Robert Print Student_name[2]
Student_name[3] = Chelsea
Student_name[4] = Bellezza
Student_name[5] = Rivallen
Example 2:
Dim Highscore(9) As Integer

Highscore ← [83, 75, 77, 82, 95, 99, 84, 70, 91, 80]

Name of the variable Data stored in the array


that stores the array
Creating an Array
1. Assign a name to the variable that will store the array a name.
Example: Highscore, Student_names and Class_section

2. Set the number of elements that array will hold. This is called specifying
the dimension or size of the array. The number of data it may store in
each memory location of an array. It must be enclosed with a
parentheses (n).
3. Define the type of data the array will hold. This is called specifying the
data type.
Dim Highscore(9) As Integer
Dim Student_name_10C2(29) As String
Creating an Array
What? the array is going to be used for, so it can be given a
meaningful name.

How? many items are going to be stored, so the size of the array
can be determined.

What? sort of data is to be stored, so that the array can be the


appropriate data type.
Dim Highscore(9) As Integer

Highscore ← [83, 75, 77, 82, 95, 99, 84, 70, 91, 80]
Highscore ← [83, 90, 77, 82, 95, 99, 84, 70, 91, 80]

INPUT Highscore[1] <- 90


Reading and Writing from and to an Array
Read - Output data from an array so we can see it or
display on the screen.
OUTPUT
PRINT highscore[3] 82
PRINT highscore[8] 91
PRINT highscore[0] 83

Highscore ← [83, 90, 77, 82, 95, 99, 84, 70, 91, 80]
Write - store or input data into an element of an
array to store it.

INPUT highscore[1] ← 90

We are storing the value 90 to element (1) in the


array with variable name highscore.
Each individual item in the array has a location
that can be pointed to. In making a program using
an array the programmer must bear in mind that
the array always starts with 0 and not 1.

Highscore ← [83, 75, 77, 82, 95, 99, 84, 70, 91, 80]

Location 3 of the array above contains the value 82.


Seatwork# 3B
Create an array that will store all student names in a class with 8
students.

Assigned the following names:


1. Andy 4. William 7. Felicia
2. Grace 5. Nikolas 8. Rivallen
3. Clif 6. Robert

What is the element number of Nikolas, Felicia and Andy?


Seatwork # 3A

QuizScore ← [90, 92, 88, 85, 97, 91, 93, 98, 95, 99]

Create the Array above: Array name, dimension or size


and assign the data type.

How to display the following scores?


98
99
88
QuizScore ← [90, 92, 88, 85, 97, 91, 93, 98, 95, 99]

QuizScore[0] = 90
:
:
:

QuizScore[9] = 99
Hands-On Activities
Create a program that will determine the size of an Array.

Create a program that will allow you to enter 5 names in an Array and
display the names in the Listbox.
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim CusName(10) As String


MsgBox(CusName.Length)
End Sub
End Class

Save @ filename: Size_of_Array


Output
Code 2:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim num As Integer
Dim CusName(5) As String

For num = 0 To 5
CusName(num) = InputBox("Enter the customer name", "Enter Name")
ListBox1.Items.Add(CusName(num))
Next
End Sub
End Class

Save @ Filename: Names_Array


Output:
Please refer to the IT Coursebook
P.156 - P. 158
Using a variable as an Index
Dim Highscore(9) As Integer

Highscore ← [83, 75, 77, 82, 95, 99, 84, 70, 91, 80]

FOR count = 0 to 9
Index variable use to move
PRINT HighScore(count) from one element in the array
NEXT to the next
Using a variable as an Index
Dim Highscore(9) As Integer

Highscore ← [83, 75, 77, 82, 95, 99, 84, 70, 91, 80]

FOR count = 0 to 9
PRINT HighScore(count)
NEXT

Index variable may also be used to write and read data


to an element in the array.
Read data to an array using the index variable
Dim Highscore(9) As Integer

Highscore ← [83, 75, 77, 82, 95, 99, 84, 70, 91, 80]

FOR count = 0 to 3
PRINT HighScores(count)
NEXT
Examples: PRINT HighScores(count) -> 83
PRINT HighScores(count) -> 75 OUTPUT
PRINT HighScores(count) -> 77
PRINT HighScores(count) -> 82
Write data to an array using the index variable
Dim Highscore(9) As Integer

Highscore ← [83, 75, 77, 82, 95, 99, 84, 70, 91, 80]

FOR count = 0 to 3
The first four values in the element of the array
INPUT HighScores(count) will be replaced with the new values entered.
NEXT
Highscore ← [75, 77, 90, 99, 95, 99, 84, 70, 91, 80]
Examples: INPUT HighScores(count) <- 75
INPUT HighScores(count) <- 77 INPUT / STORE

INPUT HighScores(count) <- 90


INPUT HighScores(count) <- 99
Write data to an array using the index variable
Dim Highscore(9) As Integer
0 1 2 3 4 5 6 7 8 9
Highscore ← [83, 75, 77, 82, 95, 99, 84, 70, 91, 80]

FOR count = 2 to 7 You may not necessarily start with 0 when using
an index variable because you may point the
PRINT HighScores(count) variable to the desired position in the array.
However, the array still starts with 0 to 9 based on
NEXT the example above.
Highscore ← [75, 77, 90, 99, 95, 99, 84, 70, 91, 80]
Create an Array that will use an index variable.

Firstname Middlename Surname

1. Use index variable to display your initials only.


First letter of your Firstname, Middlename and Surname.
2. Use index variable to store the following numerical values
88, 85, 88, 90, 95 and 99 using FOR - NEXT
Index variable 3 to 8 array size = 9 (10 elements)
Why do we use arrays?
Advantages of using an Array.
● They store many data values under one name. This means we do not
have to use or remember many variable names.
● We can easily find an item of data by specifying its element number.
● We can easily read and write many value by using a
FOR….TO...NEXT loop.
Why do we use arrays?
Disadvantages of using an Array.
● Once defined, the size of the array cannot normally be changed.
This can be wasteful of memory if we don’t use all the array’s
elements to store or hold data.
● Arrays can only hold data of one data type (the type specified when
the array is defined).
Application and Coding
Code 2:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim num As Integer
Dim HighScore(9) As Integer

For num = 0 To 9
HighScore(num) = InputBox("Enter the High Score", "Enter Scores")
ListBox1.Items.Add(HighScore(num))
Next
End Sub
End Class

Save @ Filename: HighScores_Array


Output:
Input the following data on the Array
For count = 5 to 9
Input HighScore(count)
Next

Enter the following data: 99, 98, 95, 92 and 90


Please revise the program code.
Save @ filename: Index_five
Input the following data on the Array
For count = 5 to 9
Input HighScore(count)
Next

Enter the following data: 99, 98, 95, 92 and 90


Please revise the program code.
Save @ filename: Index_five
Display the following values in the array

For count= 7 to 9
Print HighScore(count)
Next

Save @ filename: Index_last3


CODE:
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load
Dim num As Integer
Dim HighScore(9) As Integer

For num = 0 To 9
HighScore(num) = InputBox("Enter the High Score", "Enter Scores:")
ListBox1.Items.Add(HighScore(num))
Next

End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim num As Integer
Dim HighScore(9) As Integer
For num = 0 To 5
HighScore(num) = InputBox("Enter the High Score", "Enter Scores:")
ListBox2.Items.Add(HighScore(num))
Next

For num = 3 To 5
ListBox3.Items.Add(HighScore(num))
Next

End Sub
End Class
OUTPUT save@Filename: rw_index_Array
VB6.0 directly display on the Form 1
Private Sub cmdDisplay_Click()
Dim HighScore(6) As Integer

For num = 0 To 5
HighScore(num) = InputBox("Enter the High Score", "Enter Scores")
Print HighScore(num)
Next

End Sub
VB6.0 Display on the List1
Private Sub cmdDisplay_Click()
Dim HighScore(6) As Integer
Dim num As Integer

For num = 0 To 5
HighScore(num) = InputBox("Enter the High Score", "Enter Scores")
List1.AddItem (HighScore(num))
Next
End Sub
MULTI-DIMENSIONAL ARRAY
Create a multi-dimensional Array that will store the following names of
students. Using row-by-row layout

1. Aubrey 3. Diana 5. Edna 7. Gina 9. Indra


2. Bea 4. Cathleen 6. Fe 8. Hillary 10. June

What is the output of the following code:


1. PRINT Names[1, 1]
2. PRINT Names[2, 2]
3. PRINT Names[3, 1]
Thank you and see you next meeting!

You might also like