PROGRAMMING PART 1
Programming is a simple and pure form of art which cannot be
replicated
This document has little theoretical knowledge of programming
CHIBI TINODAISHE M
[email protected]
m
0781081816
RESERVED WORDS
Also known as keywords are words with pre-defined meaning in a specific
programming language for example VB.NET has Dim , if , End etc
Reserved words must not be used as variables as they have an inbuilt
function already spelt out to them within the languages.
VARIABLE
Is a name given to a memory location that stores a certain value or string.
CHARACTERISTICS OF A VALID VARIABLE
Variables must not be reserved words.
Variables must be unique in the procedure or program they are found.
Variable names as are all identifiers , start with an alphabetic character.
Have 1 to 255 character range.
Must be one word no space.
Must not contain a period (,) exclamation mark (;) or characters @,$,# in
the name.
SCOPE OF VARIABLES
Refers to an extent in which a variable is recognized in relation to other
modules in a program.
GLOBAL VARIABLES
Also known as public variables
LOCAL VARIABLES
Known as private variables
CONSTANTS
Is a variable whose value does not change during program execution.
IDENTIFIERS
A unique label of a data item or element of a program and comprises of 1 or
more character.
PROCEDURES
Self contained module that does not return a value .
Sub program that performs a special task within the main program
In vb procedures start with the keyword Private Sub , Public Sub
Functions
A self contained module that returns a value to the part of a program which
calls it every time it is called or executed.
(more on functions see chiby function pdf )
SYNTAX
Is a the grammatical rules governing sentence construction in that language.
SEMANTICS
The meaning attached to statements and the way they are used in a certain
language.
ARRAYS
Is a static data structure which stores variables of the same data type.
An array position always starts from zero.
Visual Basic Arrays
Arrays are using for store similar data types grouping as a single unit.
It is a fixed collection of same data type that are stored
contiguously and that are accessible by an index
We specify their length and we can initialize arrays with data.
We can access Array elements by its numeric index.
Integer Array
Declaring and Initializing an Integer Array
Dim array As Integer() = New Integer(3) {}
array(0) = 10
array(1) = 20
array(2) = 30
array(3) = 40
In the above code we declare an Integer Array of four elements and assign the value
to array index . That means we assign values to array index 0-4.
We can retrieve these values from array by using a for loop.
Module Module1
Sub Main()
Dim array As Integer() = New Integer(3) {}
array(0) = 10
array(1) = 20
array(2) = 30
array(3) = 40
For i As Integer = 0 To array.Length - 1
Console.WriteLine(array(i))
Next
Console.ReadKey()
End Sub
End Module
We can declare and initialize an array in one line
Dim array As Integer() = New Integer() {10, 20, 30, 40}
HOW TO FIND ARRAY LENGTH
Myarray.length
We can use myarray.length to find the length of the array.
DECLARING AND INTIALIZING A STRING ARRAY
Dim names(4) As String
The above Vb.Net statements means that , an Array named as names
declared as a String type and it can have the capability of five String
type values.
names(0) = "Chiby"
names(1) = "Tatenda"
In the above statement , we initialize the values to the String
Array.names(0) = "Chiby" means , we initialize the first value of Array
as "Sunday" ,
Dim stuName as String = names(1)
We can access the Arrays elements by providing its numerical index,
the above statement we access the second value from the names
Array.
In the following program , we declare an Array "names" capability of 5
String values and assigns the 5 values as names . Next step is to
retrieve the elements of the Array using a For loop. For finding the
end of an Array we used the Length function of Array Object.
Module Module1
Sub Main()
Dim names(4) As String
names(0) = "Chiby"
names(1) = "Tatenda"
names(2) = "Letwin"
names(3) = "Salome"
names(4) = "Tacue"
names(5) = "Marange"
names(6) = "Faith"
For i As Integer = 0 To names.Length - 1
MsgBox(names(i))
Next
Console.ReadKey()
End Sub
End Module
How to Use For Each loop with Arrays ?
Dim array As Integer() = {10, 30, 50}
'array declaration
For Each element As Integer In array
MsgBox(element)
Next
Array Sort
You can sort the arrays in ascending order as well as descending .
We can use Array.Sort method for sorts the elements in a one-
dimensional array.
Also we can use Array.Reverse method for reverses the
sequence of the elements in the entire one-dimensional Array.
Module Module1
Sub Main()
Dim names(4) As String
names(0) = "Chiby"
names(1) = "Tatenda"
names(2) = "Letwin"
names(3) = "Salome"
names(4) = "Tacue"
names(5) = "Marange"
names(6) = "Faith"
Array.Sort(names) ' sorting the array in ascending order
For j = 0 To 4
Console.WriteLine(names(j))
Next
Console.WriteLine(vbNewLine)
Array.Reverse(names) 'reverses the sequence of the entire 1D array
Console.ReadKey()
End Sub
End Module
LOADING AN ARRAY USING A LOOP
Dim names(8) As String
WriteLine("Enter the names of the test dummies")
For t = 0 To 8
names(t) = ReadLine()
Next
OUTPUTTING AN ARRAY
WriteLine("" & vbTab) 'CREATING A LINE
For K = 0 To 8
WriteLine(names(K))
Next
PROGRAMMING IS A SIMPLE AND PURE FORM OF ART
- Chibi Tinodaishe M
CONTACT: 0781081816
EMAIL : [email protected]
ISAIAH 43 VS 2
THANK YOU
===============================================================================