Home
Up
 

BASICS:

Or should I say Visual Basics?

From the time electronic computers were invented people have had to write program code...to make the hardware do what is required.

Some things to remember about hardware:

bulletHardware is limited physically
bulletUnderstanding hardware helps us to design programs in a better way
bulletHardware does not understand natural languages only 0's and 1's
bulletHardware uses the Binary system or variations of it to execute instructions, store data and the like.
bulletThe Binary System:

Before we even start to consider what a program is we need to understand how data is stored and manipulated in a computer: 
bulletData is stored in various parts of the computer hardware
bulletThese hardware parts have physical limits imposed on them by various design constraints
bulletthis imposes a limit on the size of a word or number one can represent and store within these hardware parts
bulletThe size of the data (word or number or picture) is measured in Bits or Bytes  (1 Byte = 8 Bits)
bullet1 Byte is the amount of data required to represent 1 character in a computer
bulletWe also have more types of unit definitions:
Name Exact Measure Approximate Measure
KiloByte or KB  = 1024 Bytes  1000 Bytes
MegaByte or MB  = 1024*1024 Bytes  1,000,000 Bytes
GigaByte or GB  = 1024*1024*1024 Bytes  1,000,000,000 Bytes
TeraByte or TB  = 10244 Bytes  1,000,000,000,000 Bytes
bulletA bit represents one of two binary states we usually represent these as 0 or 1 
bulletit is the smallest unit of storage capacity used
bulletin reality a bit may be a positive or negative voltage, a electrostatic charge, a magnetised element,...
bulletBit size to represent data has increased as technology has improved. 
bulletToday 32 and 64 bit computing is common in PCs, super computers use 1024 bits and more to represent individual pieces of data
bulletThe more bits you can move around per CPU operation the faster your PC can perform calculations

 The use of Bits raises the question of  what decimal number does the binary number stored in a computer 1110011011110110 represent in our number system (decimal)?

Clearly we need to be able to covert a number defined in a particular number base to decimal number base and visa-versa. Typical number bases used in computing are as follows:
bulletbinary - number base 2
bulletoctal - number base 8
bullethex - number base 16

A table of numbers and their representation in different number bases:

Decimal Hex Octal Binary
0 0 0 0
1 1 1 1
2 2 2 10
3 3 3 11
4 4 4 100
5 5 5 101
6 6 6 110
7 7 7 111
8 8 10 1000
9 9 11 1001
10 A 12 1010
11 B 13 1011
12 C 14 1100
13 D 15 1101
14 E 16 1110
15 F 17 1111

Examples of number conversion:
bulletConvert a number AB12n in a base n to decimal = A*n3 + B*n2 +1*n1 +2*n0   
bulletConvert 123410 to an equivalent  number in base 16
bullet1234 / 16 =  77 rem 2
bullet77 / 16 = 4 rem 13
bullet4 / 16 = 0 rem 4

Therefore 123410 = 4D216

PROGRAMMING:
bulletWe assume here that we are using sequential processing rather than parallel processing to solve our problems.
bulletAn application (program) is a set of ordered instructions to the CPU telling it what to do and is a solution to a particular problem or set of problems
bulletPrograms that are "running" are usually stored in RAM
bulletWhen an instruction is sent to the control unit (see CPU diagram) it is “Fetched”, Decoded to CPU language and Executed this is called the F-D-E cycle
bulletInstructions are executed one at a time and in some specific sequence

        TYPICAL STRUCTURE OF A CPU

bulletMore than one program can be stored in RAM and the CPU has to switch between the instructions of each program doing the F-D-E cycle. This switching process is called “Multitasking”.
bulletHowever because the CPU is so fast it seems to us that instructions (programs) are executed in parallel BUT this is NOT parallel processing.
bulletParallel processing is when there is more than one CPU performing different tasks of the same program(s) at the same time.

 

Instructions (code) can be written in different languages
bulletMachine Code most basic code combination of 1 & 0 s
bulletbut difficult for humans to understand
bulletAssembler Code, different types depends on brand of CPU (i.e. Motorola, Intel)
bulleteasier than machine code but still far from the usual way people express problems
bulletHigh Level Languages Visual Basic, C, C++, Pascal, Java,…
bulleteasier than the previous types closer to how people express problems

Programs are not just written the following steps need to take place, This process is called the Program Development Life Cycle (PDLC)
bulletAnalysis of the “problem”
bulletDesign of the program using some method (methodology Top Down, Bottom Up, OOD,…)
bulletCoding of the program
bulletTesting of the program
bulletDocumentation
bulletMaintenance of Program

Structured programming or structured design
bulletIs a methodology to help translate a problem into a program
bulletWe can use flowcharts,  to help us get the solution in a graphical way
bulletWe can use pseudo-code, a  set of instructions written in your own spoken language

Example of a Flowchart:

 

The set of steps of a solution to a problem is called an algorithm:

There exists the idea of a General Algorithm which has 5 main steps

  1. Start (define variables, constants,…)
  2. Input (Data)
  3. Process (Data)
  4. Output (Data, Information = Results)
  5. End (= exit)

We can break down a problem into these 5 steps at different levels of abstraction

Some code structures we use again and again  in many different programming languages the most common are as follows:

 
bulletSequence Control Structure in Visual Basic Language (VBL):

This is how we write lines on code using the correct syntax (i.e. rules of grammar) and in the correct order. These lines of code tell the computer what to do. We can refer to a set of lines of code as a Block of Code or Code Block
bulletLbl_Input1.Caption = "Please Enter an Integer Number"
bulletLbl_Input2.Caption = "Please Enter another Integer Number"
bulletSum = Num1 + Num2
bulletResult = Val(Text1.Text) + Val(Text2.Text)
bulletLbl_Output.Caption = "The sum of the two numbers is = " & Result

 

bulletSelection Control Structures in VBL

Selection control structures are used to make decisions in the program and control  which code statements are executed and which are not.
bulletIF - END IF
bullet If Val(Txt_Num2.Text) = 0 Then
bullet        Print "Input is Valid Division by Zero"
bulletEnd If

 
bulletIF - ELSE - END IF
bulletIf Val(Txt_Num2.Text) = 0 Then
bullet        ' if the test condition above is true we do this
bullet       Result =  Val(Txt_Num1.Text) / Val(Txt_Num2.Text)
bullet        Print    Result
bulletElse
bullet        ' if the test condition above is false we do this
bullet        Print  "Input is NOT Valid Division by Zero"
bulletEnd If

 
bulletIF - ELSEIF-ELSE - END IF
bulletIf Val(Txt_Num2.Text) > 0 Then
bullet        ' if the test condition above is true we do this +ve answer
bullet        Result =  Val(Txt_Num1.Text) / Val(Txt_Num2.Text)
bullet        Print    Result
bulletElseIf  Val(Txt_Num2.Text) < 0 Then
bullet        ' if the test condition above is true we do this -ve answer
bullet        Result =  Val(Txt_Num1.Text) / Val(Txt_Num2.Text)
bullet        Print    Result
bulletElse
bullet        ' if the test condition above is false we do this
bullet        Print  "Input is NOT Valid Division by Zero"
bulletEnd If

 

The Select Case is similar to the IF-ELSEIF-...-ELSE-END IF

bulletSelect Case  test-expression        
bullet        Case expression1
bullet                    'do these code statements
bullet        Case expression2
bullet                    'do these code statements
bullet        Case Else
bullet                    'do these code statements
bulletEnd Select
bulletRepetition (Loop) control structures in VBL

These structures are used to execute any code statements inside the Loop many times.

For  control structures are used when we know exactly the numbers of times we want to execute the code inside the loop. VBL code to add all the integer numbers between 0 and 10 inclusive:
bulletSum = 0.0
bulletFor j = 0 to 10
bullet    Sum = Sum + j
bulletNext

  VBL code to add the numbers 0, 0.5, 1.0, 1.5,...,10:
bulletSum = 0.0
bulletFor Count = 0 to 10 Step 0.5
bullet    Sum = Sum + Count
bulletNext

 

Do - Until control structures are used when we DON'T KNOW exactly the numbers of times we want to execute the code inside the loop

bulletDo Until User_Input = "x" Or "X"
bullet    Print  Enter a number to add to previous TO Exit type x or X
bullet    j = Val(Txt_Num1.Text)
bullet    Sum = Sum + j
bulletLoop
bulletNested control structures: The process of putting a code structure like the If, For & Do inside another If, For or Do is called NESTING. This nesting gives us more power in doing certain types of things.
bulletExample of Nested For and If structures:
bulletFor Count1 = 0 to 10
bullet    For Count2 = 0 to 99
bullet        Sum = Sum + Count1*Count2
bullet        If Sum < 10 Then
bullet                If Sum > 20 Then
bullet                    Label1.Caption = Sum
bullet                Else
bullet                    Label2.Caption = Sum
bullet                End If
bullet        Else
bullet                Label3.Caption = Sum
bullet        End If
bullet    Next
bulletNext

 

bulletArray control structures give us the power to store similar types of data together and be able to use the data quickly.
bulletLbl_Name(0).Caption = "John"
bulletLbl_Name(9).Caption = "Mary"

The thing to remember is that programs are designed to work NOT just written with the hope that they will work!!

 
bulletEXAMPLE of the design steps of a simple program

PROBLEM DEFINITION:

          You are to design a program that performs the following:

The user enters (types) the radius of a circle and the program calculates the area of the circle and displays the result on screen.

Before you start your design you can make a sketch on paper of what you think is required:

 In  your design you can use either Pseudo-Code or a Flow Chart remember.  Note that the General Algorithm has the following steps:

  1. Start

  2. Input

  3. Processing

  4. Output

  5.  End

A SOLUTION to the Calculate Area program USING PSEUDO CODE:

bullet

LEVEL 1 PSEUDO CODE:
bullet

1. Start

bullet

2. Input

bullet

2.1 Display message to user to enter the radius and what units are used

bullet

3. Process

bullet

3.1 Calculate area of circle

bullet

4. Output

bullet

4.1 Display value of the area in suitable units on screen

bullet

5. End

 

bullet

LEVEL 2 PSEUDO CODE: After some more thought and questions!!!
bullet

1. Start
bullet

define PI as a constant

bullet

2. Input
bullet

2.1 Display message to user to enter the radius

bullet

2.2 Display units of Radius

bullet

3. Process
bullet

3.1 Calculate area of circle using Area = PI*Radius2

bullet

4. Output
bullet

4.1 Display value of circle area on screen

bullet

4.2 Display units of circle area

bullet

5. End

 

bullet

LEVEL 3 PSEUDO CODE: Is after even more thought and we start to introduce code statements

bullet

1. Start
bullet

1.1  Use form, labels, textbox, command button objects for interface

bullet

2. Input
bullet

2.1 Display message to user to enter the radius

bullet

2.1.1 Use Label1 object to display message mentioned in 2.1

bullet

2.2 Display units of radius

bullet

2.2.1 Use Label1 object to display message mentioned in 2.2

bullet

2.2.2 Set .Caption in Properties window to -> Enter the radius in cm

bullet

3. Process
bullet

3.1 Calculate area of circle using Area = PI*Radius2

bullet

3.1.1 Use Text1 object to get value of Radius

bullet

3.1.1.1 Use .Text property of object Text1

bullet

3.1.1.2 Set the .Text property in Properties window to blank

bullet

3.1.2 Use formula Area = PI *Val (TxtRadius .Text)^2 inside Command Button named Command1
bullet

a better name for Command1 control is e.g. CmdArea

bullet

4. Output
bullet

4.1 Display value of circle area on screen and units of area

bullet

          4.1.1 Use Label2 to display calculated area and units

bullet

          4.1.2 Set .Caption in Properties Window to blank

bullet

          4.1.3 Use LblArea.Caption = Area inside Command Button

bullet

5 End

  Example of VB6 Form and Code

Const PI = 3.141591  'defined in General

Private Sub CmdArea_Click()

     LblArea.Caption = PI * Val(TxtRadius.Text) ^ 2

End Sub

 

So what would be the next refinement of the design above?