0% found this document useful (0 votes)
2 views18 pages

Programming

The document provides an overview of programming concepts, including the generations of programming languages from low-level to high-level, and the structure of programs including constructs like sequences, selection, and repetition. It also discusses types of programming errors, the differences between low-level and high-level languages, and introduces Visual Basic .NET as an object-oriented programming language. Additionally, it covers variable declaration, initialization, data types, debugging techniques, and control structures such as loops and conditional statements.

Uploaded by

natliec13
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views18 pages

Programming

The document provides an overview of programming concepts, including the generations of programming languages from low-level to high-level, and the structure of programs including constructs like sequences, selection, and repetition. It also discusses types of programming errors, the differences between low-level and high-level languages, and introduces Visual Basic .NET as an object-oriented programming language. Additionally, it covers variable declaration, initialization, data types, debugging techniques, and control structures such as loops and conditional statements.

Uploaded by

natliec13
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

6.

8 PROGRAMMING

The concepts of a program


Program is a sequence of instructions written in the language of a computer to make a
computer to perform certain task.

Generations of programming language


First generation

Low Level languages such as assembly language and machine code are first generation
languages.

They are executed directly without translation.

Machine code is the actual pattern of 0s and 1s used in computer memory.

Programs written in machine code are time consuming, laborious and error prone when
entering into the computer.

Second generation

Assembly code is second generation languages.

They allow programmers to use mnemonics (abbreviations that represent an instruction


in a memorable way) and denary numbers (1-9) instead of 0s and 1s.

E.g. ADD or ADX to add two numbers, SUB or SBX an instruction to subtract.

Programs written in assembly language need to be translated into machine code before
execution.

The program called Assembler is used to do the translation.

Third generation

Imperative high level languages such as Pascal, COBOL, FORTRAN, BASIC and Algol are
third generation languages.

It is a refinement of the second generation language that makes the language more
programmers friendly.
It includes features like data types and expressing concepts in a way that favours the
programmer not the computer.

Fourth generation

They are languages with the all powerful commands like SQL.

They use command such as SORT which are easier than writing your own sort
procedures using third generation languages.

Fifth generation

Are languages like Prolog.

They are often regarded as very high level languages.

Experts systems are often written in Prolog.

They based on solving problems using constraints given to the program rather using an
algorithm written by a programmer.

Are mostly used in artificial intelligence.

Program construct
Program instructions performed by the computer must be carefully controlled.

Programming languages contains features that allow the order of instruction execution
to be controlled.

Program control structures are use to regulate the order in which program statements
are executed.

They are:

Sequences

The execution of the program usually following the order in which the steps are written.

Selection

A decision between two alternative routes through the program that involves at least
one condition.

Conditions are normally in the form of expressions that when executed give Boolean
results (true/false)
Repetition

A step or sequence of steps repeated within the program as an iteration or loop.

Subroutine:

It is the term used to describe a sub-program.

Sub-program may be described as a component part of a program.

Procedures and functions can be subprograms.

They can be started or called by a program instruction and can return control to the
next instruction after the calling instruction is completed.

Sub-program should be self-contained, perform well-defined operations on well-defined


data and have an internal structure that is independent of the program in which it is
contained.

Machine code – it a sequence of binary codes representing instructions for the processor.

A computer program should be in the form of machine code for it to be run on a


processor.

Instruction set- is the language in which machine code is written for the family of a processor.

An instruction set includes data transfer instruction for:

Setting the value held in a register.

Moving data from an internal memory location to a register or vice versa.

Reading data from an input or storage device and writing data to an output storage
device.

An instruction set also includes arithmetic and logic operations for:

Adding, subtraction, multiplication, or dividing the values in registers.

Comparing the values in two registers.

Performing bit-wise logic operations.

An instruction set also includes instructions for controlling the flow of program.

Types of program errors


Syntax errors:

Occurs due to the fact that the syntax of the language is not respected.

These are errors of grammar.

They are easy to find and correct because the development system finds them for you.

Semantic/logical errors:

Occurs due to an improper use of program statements.

Are errors due the fact that the specification is not respected.

These errors are of meaning.

Usually the system will generate wrong results.

They are generally hardest to fix.

Run-time errors:

Are errors that are found when the program executes.

They are detected when the user enters illegal data

Programming languages

They can be divided into two types:

These are:

Low Level Languages: which are closer to machine language

High Level Languages: which are closer to human language.

Low Level Languages:


It is a language of a particular processor.

It is the code for a particular machine (computer) and is given the name machine code.

They are machine oriented because they refer to hardware such as processor’s register,
input and output ports and specific locations in internal memory.

Advantages
Skilful programmer has complete freedom of the choice of instructions.

Programmers have direct control over the processor’s communication with its
peripherals.

The programmers can write very efficient programs that:

Fits into limited storage space

Require limited RAM.

Run rapidly

Disadvantages

They are difficult for a programmer to read since they lack human relationship.

They are machine oriented making them hard to learn for the programmer.

Programs written in low level language are not portable that means they are not
transferable between families of processors.

Machine language

It consists of a string of zeros and ones and because of this is the lowest level of
language.

It is difficult for programmers to read and write.

Programs written in machine language can be converted to machine code without using
a compiler or interpreter.

Machine language instruction consists of an operation code known as an ‘opcode’.

Example of an opcode 110000.

Assembly language.

It is a low level language for a computer or other programmable device.

It is developed for a family of processors.

Assembly language is considerably easier to read or write than machine language.

It consists of mnemonic- a short word or abbreviation that acts as a memory aid.


Example of instruction in the PIC16F627 microcontroller’s assembly language is:
MOVLW 0x5A.

It need to be converted into executed machine code by a program called assembler.

The conversion processing is referred o as assembly, or assembly the code.

High level languages


They are similar to human language or mathematical notations.

They are easy to read and write, test debug and maintain.

They are problem oriented rather than machine oriented.

Different languages are designed to solve different types of problems.

They are independence of the instruction set of any particular family of processor.

Types of high level programming languages

Language Use
COBOL (Common Business Oriented Business application
Language)

FOTRAN (FORmula TRANslation) Scientific applications

General purpose and educational use

BASIC (Beginners All-purpose Symbolic

Instruction Code)

Visual BASIC

Pascal

Delphi

LISP

LOGO

C
C++

Java

A program consists of procedural steps called statements, or of facts and rules.

Procedural languages provides the programmers with statement that include:

Reading an input form the keyboard and writing it to a variable.

Displaying or printing output expressions or graphics

Perform calculations and comparisons on variables with different data types and data
structures

Selection of a route through the program

Repetition of a part of a program

Calling function and procedure subroutines.

High level languages must be translated in to machine code before execution.

There are two types of translation:

Compiler:- a program that translate all the HLL source code into object code by a
process called compilation.

Compilation must be completed before the program is run.

If there are errors it produces an error report then the programmer correct the error
and compiles the whole program again.

It is slow when debugging large program.

Interpreter:- a program that translate the source code into object code statement by
statement.

If there are errors it produces a report and stops the execution.

It slows the execution process but speed up the debugging of large programs.

A comparison of translation programs.

Assembler Compiler Interpreter


Translates processor-specific Translate HLL code into Translate HLL statements
assembly code into machine machine code for specific and executes them on a
code for a specific processor processor and operating specific processor and
system operating system

Translate an entire program Translate an entire program Translate and executes a


but does not run it but does not run it program on the target
processor, usually one
statement at a time, but
does not run it.

Translate one source code Translate one source code Executes one source code
instruction to one machine statement to one or more statement as one of more
code instruction machine code instructions. instructions.

Comparison of features of LLL and HLL

High Level Languages Low Level Language

Program statements are problem oriented Program instructions are machine oriented

Programs are portable between different Programs are not portable between different
types of computer types of computer.

Programs must be translated to run on a Machine language need no translation


processor

Translation to a machine language is Translation from assembly language to


relatively complex machine language is relatively simple

Programs are relatively easy to read and Programs are relatively hard to read and
write write

Programs are relatively easy to test, debug Programs are relatively hard to test, debug
and maintain and maintain.

Visual [Link]
[Link] is a Visual Basic .NET is an object oriented programming language designed by Microsoft
Visual Studio is a suite of products that includes the .NET Framework and the Integrated Development
Environment (IDE)

Consists of several sections that the developer uses while programming. Generally three sections are in
view:

The Toolbox on the left

The Solution Explorer on the right

The Code / Design view in the middle

Toolbox

The Toolbox is a palette of developer objects, or controls, that are placed on forms or web pages, and
then code is added to allow the user to interact with them. E.g. textbox, label, button

Solution Explorer

This is a section that is used to view and modify the contents of the project.

Properties Windows

The properties windows shows all the control properties to be changed at design time. Most of these
properties can be also changed with code at run time

Code / Design view

Code view is where developers write the code that makes everything in the application work.

Design view is where you perform the interface design

CONDITIONAL STATEMENTS

If statement…..

Select case…….

CONDITIONAL SATEMENTS

Conditional statements are used for decision making.

They are used in conjunction with operators, therefore there is need for an understanding of
operators first.

Operators
Types of operators
Arithmetic – exponentiation ^,negate subtract -,multiplication*, division/, addition +,string
concatenation &

Comparison – equal =, <,>, >=,<=

logical -compares results of 2 logical expression and return a true or false – NOT, AND, OR, XOR

EXPRESSIONS

Combination of operator and operands is an expression

Example of a Simple Expression

3+4

3 and 4 are operands and + is the operator

Example of a Complex Expression

Has several operations that are performed in a single expression e.g. engineering formulas

Conditional statements

General there are 2 types:

If..Then (has several variations else, else if, nested)

Select Case Statement

Types of conditional statements


If…Then (1 condition and 1 code blocks)

If…Then…Else (1 condition and 2 code blocks)

If..Then..Elseif…Then (2 condition and 2 code blocks)

Variations can continue depending on number of conditions

End if

VB executes sequentially

If an if .. Statement occupies more than one line you require a matching end if

Practical

Write a program that implements an If… statement that occupies multiple lines to authenticate a user.
Select case

Used when you have multiple conditions

Can accommodate multiple conditions better than the If…. Statement

The more the lines of code the greater the chances of generating errors.

Select Case General Syntax

Select Case

Case 1

VB code

Case 2

VB code

Case n

VB code

Case Else

VB code

End select

VARIABLES
Declaration

Initialization

Variable types

Definition

Variable is a temporary location in memory used to hold data e.g. letter box

Declaration
Use reserved wordDim Variable_Name As Data type
E.g. Dim age As Integer

Rules for naming variables

You must use a letter as the first character.

You can't use a space, if you want to space use an underscore e.g. Int_Age

Do not use reserved words

Do not begin with a punctuation mark or a space

Variables may contain digits but not start with digits

Variables are not case sensitive e.g. age and AGE

Variable type /Data type

Each data type requires a specific amount of memory to store data that is assigned to the variable e.g.
Byte requires the least amount of memory

*it is a good programming practice to begin with data type followed by variable name e.g. intAge

Data types

1. Boolean – takes 2 possible states true or false, 0 or 1

[Link] – holds characters (char on key board)

[Link] – holds positive numbers 0-255

4. date – holds date and time info

[Link] – holds values that have a decimal

6. Double – holds very large and very small values mostly used in scientific and engineering
calculations

7. Integer – holds a whole number

8. Object – holds any kind of value

9. Long – holds whole numbers with a larger range than integer

10. short holds whole numbers from -32 768 to 32 767

11. string – holds more than one characters e.g. names enclosed in “”

Choose an appropriate data type to avoid errors in program


Initializing
When ever you declare a variable initialize it or else you generate an error

You use an = sign as follows

E.g. Dim intAge As Integer

intAge = 0/ textbox1. text

Practical
Write a program the implements the concept of variables to calculate density.

Formula: Density = Mass/Volume

Code

Dim Mass As integer

Dim Vol As integer

Dim Density As integer

Mass = [Link]

Vol= [Link]

Density = Mass/Vol

[Link] = Density

Debug program

Enter value 13 for mass and 5 for Volume

What answer are you getting?

Is it correct?

Bug/ Error

We have generated a Logic error.

Practice question
Write a program that calculates the area of a circle by implementing the concept of variables.

DEBUGGING
DEBUG

Debugging is the act of removing errors within your code

List of errors are displayed in error list window(View- Other windows-Error list)

Types of errors

Programming errors are generally broken down into three types:

Design-time

Runtime

Logicerrors.

Design-time error

Design-time error is also known as a syntax error and are easy to track e.g . [Link] =
“sam” the b in textbox should be in upper case you receive error message [Link] is not
declared

Runtime errors

Runtime errors are a lot harder to track down cause program to crash. Occur when the programme is
running. E.g. infinite do loop Do while x>10, divide by zero Dim k As Integer, Dim y as integer k=4, y =0
[Link] = CInt(k/x)

Logic errors

Logic errors - occur when the programme is running. They happen when your code doesn't
quite behave the way you thought it would. E.g Dim k As Integer, Dim y as integer, Dim z As
Integer k=10.5, y =3 z= k*y [Link] = z Answer is 30 .

Declaring as integer discards the fractional part.

Try… catch statement

Built in class (called exception) that deals with errors. It traps errors in code.

Try..Catch Example

Practical Design Question

Design an ECD application form for Holy Rosary Primary School. Demonstrate knowledge of various
controls

Design form below


Reset the following properties

Combo box – right click on it-click properties-set Items property to Male and Female

Textbox on address set it to multi line to accommodate more information because addresses occupy
more than one line.

Radio button – reset the text property to read ECD A and ECD B

Controls

We have introduced new controls use them in your assignment project.

Loops
For and Do loop

Purpose

when you want to execute same statements for a specific number of times.

E.g. incease salaries of all civil servants by 100%

Types

For… loop

Do…. loop

For … loop type


Single For …loop

Nested For …loop (For loop within a For loop)

For loop general syntax

For Variable Start value To End Value

VB statement

Next

Step Value

Default increment is one

To define your own increment use

:Step value

Step value - specifies the value of each increment and can be negative

Practical

Write a program to calculate multiples of any given number by implementing the concepts of loops.

Code

Dim Mul As Integer

Dim SV As Integer

Dim EV As Integer

Dim Answer As Integer

Dim i As Integer

Mul = [Link]

SV = [Link]

EV = [Link]

For I =SV to EV

Answer = I * Mul

[Link]( I & “*” & Mul & “=” & Answer)


Next i

Name property

Edit the name property of [Link] to txtMultiple and observe changes in your code.

Practice question

Change the title of the form.

Edit the code such that the start value should always be less than the end value.

Edit the code so that fields are cleared.

Do loop

Defines the conditions that must be true for statements inside the loop to execute.

Syntax: Do While Condition

Vb statements

Loop

Simple program

Add a button – rename to Do while – Double click and write the followig code snippet.

Dim intAge as integer = 1

Do while intAge < 10

[Link]( intAge)

IntAge = intAge +1

loop

Do Until Loop

Similar to do while

Add a Button and write code below

Dim bResponse As Boolean = True

Do until bResponse = False


If [Link] (“Do you enjoy [Link] lectures”, “Question”,[Link],
[Link])= [Link] Then bResponse =False

End If

Loop

Observation

Debug

Explain how the program work?

Do Loop While Loop

First executes code before evaluating logical expression

General Syntax

Do (beginning of code block)

VB statements

Loop(end of code block) While condition

Add a button and write the code

Dim intcounter As Integer = 0

Do

[Link]("Inside code block")

Loop While intcounter = 1

[Link]("Outside code block")

Observation

Debug

Explain how the program work?

Practice

Design any application that uses an input box to accept names until they reach 5, by implementing a Do
loop

You might also like