0% found this document useful (0 votes)
2 views

Introductions To Programming and Visual Basic

Documents

Uploaded by

cordm6547
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Introductions To Programming and Visual Basic

Documents

Uploaded by

cordm6547
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

Introduction to Computer

Sciences
FSC 113

2016/2017 Academic Session

FSC 113 NOT FOR SALE PLEASE


Course Details
• Venue: DLI, Auditorium A, B & C
• Time: Tuesdays 8am 10am
• Lecturer: Dr. Oladeji, Dr. Okunoye, Dr. Akinlade,
Dr. Odunayo, Mr. Ajayi & Mr. Edagbami
• Texts: Computer Fundamentals & Visual
Basic Language (Highly recommended),
Other relevant texts

FSC 113 NOT FOR SALE PLEASE


Course Objectives
At the end of this course, students should:

• Understand the Software Development Life Cycle


(SDLC).

• Write basic programs using Microsoft Visual


Basic

FSC 113 NOT FOR SALE PLEASE


Outline
• Introduction to Programming (2 Parts).

• Programming with Visual Basic (2 Parts).

* Term assignment.

FSC 113 NOT FOR SALE PLEASE


Introduction

LECTURE 1

FSC 113 NOT FOR SALE PLEASE


What is a Computer?
• It can be defined as a data processing machine
that is capable of accepting input from users in
form of data, processing or storing these data and
giving output based on user supplied logic.

• A computer is a general purpose device that can


be programmed to carry out a set of arithmetic or
logical operations automatically

FSC 113 NOT FOR SALE PLEASE


Types of Computers

FSC 113 NOT FOR SALE PLEASE


Source: informationq.com
Stored Program Concept
• A concept of storing alterable instructions that
will direct the machine to automatically
perform the necessary processing steps to
carry out a task.

• This is the major feature that distinguishes a


computer from a basic calculator or other
data processing machines

FSC 113 NOT FOR SALE PLEASE


Machine of Machines
• Computers on their own are dumb machines that simply
do nothing. It is the program supplied that actually
makes them useful.

• A computer for example becomes an arithmetic


calculator or a music player once a calculator or media
player program is loaded into it.

FSC 113 NOT FOR SALE PLEASE


Introduction to Programming

LECTURE 1

FSC 113 NOT FOR SALE PLEASE


What is a Computer Program
• A computer program is a sequence of simple
logical instructions into which a given
problem is reduced and which is in a form a
computer can understand.

• A given problem can be broken down/reduced

– Sequence of simple logical instructions


– A form the computer can understand
FSC 113 NOT FOR SALE PLEASE
• A program is simply a list of logical
instructions that tell the computer what to do.
– Simple addition program
• Accept first number = x
• Accept second number = y
• Add x and y = z
• Output z as answer

• The program should incorporate the logical


procedure for solving a given problem, hence
a computer does not produce logic, it simply

codes.
FSC 113 NOT FOR SALE PLEASE
• Computer Programming is the act of writing code
which can be executed by a computing system to
perform a meaningful task or give a desired result.

• Programs are written to solve specific problems of


varying level of complexity requiring different
level of resources and planning.

A sample program might be to add/subtract two

accounts of a large enterprise bank


FSC 113 NOT FOR SALE PLEASE
• All problems to be solved by a computer must be
broken down into four basic operations:

– Calculating or computing
– Comparing and testing
– Shifting operation
– Jumping operations

As long as a problem can be reduced to these basic


operations it can be solved.

FSC 113 NOT FOR SALE PLEASE


The Software Development Life Cycle
Problem
Definition

Problem
Documentation
Analysis

Development
or Design / Model
Implementation

FSC 113 NOT FOR SALE PLEASE


*Software Development Life Cycle (SDLC)
Problem Definition
• A complete, precise and unambiguous statement
of the problem to be solved.

• It is a clear definition of what the program does or


is meant to do.

Problem definition example:

L
FSC 113 NOT FOR SALE PLEASE
Class Programming Example

Write a program to sum first 10 natural


numbers.

• Problem Definition:
– Program to find the sum of the first 10 natural
numbers

FSC 113 NOT FOR SALE PLEASE


Start from 0 or from 1
Problem Analysis
• A thorough examination of the problem
defined in the problem definition stage.

• It involves breaking down the problem into its


constituent parts and determine what is/are
needed to solve the it.

• Here the Input(s), Output(s) and Procedure(s)


must be identified.
FSC 113 NOT FOR SALE PLEASE
Problem Analysis Example

pension

– Input = EmployeeID, [Full Name], Department, Designation,


Employment Date, Salary, Pension percentage

– Output = Pension

– Procedure = Get active service year by subtracting current


date from employment date, then multiple by
the pension percentage to get pension value
FSC 113 NOT FOR SALE PLEASE
* More examples available in the course text – pg.73
Class Programming Example
Problem Analysis:

• The first 10 natural numbers are:


1,2,3,4,5,6,7,8,9,10
• Input = first 10 natural numbers
• Output = the summation
• Procedure=
Start from the first natural number (1).
Add the next number to it
Store summation
Continue till the tenth natural number
FSC 113 NOT FOR SALE PLEASE
Model / Design

• A model is an abstraction of the real problem


and it defines the relationship between objects
of the problem space.

– Mathematical models (illustrated on page 74).


– Logical model: Algorithm, flowchart, Business
Process Chart

FSC 113 NOT FOR SALE PLEASE


Algorithm
• A finite process or set of rules which spells out
a step-by-step sequence of operations to be
followed to solve a specific problem.
Finite It must end

Definite steps Each step must be clear, precise and


unambiguous
Input It should have at least an input

Output It should have at least an output

Effective The steps can be done exactly in a finite and


reasonable length of time

FSC 113 NOT FOR SALE PLEASE


Examples of Algorithm: Simple Addition

Given 1234 + 5678


1. Arrange the two numbers in
standard form.
2. Start with right most column
(current column). c

3. Add the digits in the current 1 2 3 4


column. 5 6 7 8
4. If sum < 10, record sum
under current column
5. If sum > 10, subtract 10
from the sum and record
remainder, add 1 to the
column left of current
column.
6. Current column = immediate
left column
7. If current column is empty
then stop else goto step 3
FSC 113 NOT FOR SALE PLEASE
Algorithm Notations
• Assignment notation:
A B, meaning assign value of B to A
• Arithmetic notations: +, -, *, /
• Logical notations: =, <>, ≠, <, >

FSC 113 NOT FOR SALE PLEASE


Simple Addition Algorithm
1. Set V1, V2 and SUM 0 V1 1 2 3 4
V2 5 6 7 8
2. Set V1 1234 SUM 6 9 1 2
3. Set V2 5678
4. SUM V1 + V2
5. Output SUM as answer

FSC 113 NOT FOR SALE PLEASE


Class Programming Example
• Sum of first 10 numbers, ie Plan / Procedure
1 + 2 + 3 + 4 +5 + 6 + 7 + 8 + • Let n = number of natural
9 + 10 numbers ie 10.
• Let SUM = summation
Written mathematically as • Let i = current number
• Initialize all variables*
S= 10
𝑖=1 𝑖
n = 10
SUM = 0
i=1

• Thus the algorithm is


FSC 113 NOT FOR SALE PLEASE
*Why initialize? Page 83 & 84 of Course Text for details
Algorithm to sum first 10 natural numbers

1. i 1
2. SUM 0
3. SUM SUM + i
4. i i + 1
5. If i > 10 GOTO Step 7
6. Else GOTO step 3
7. STOP

FSC 113 NOT FOR SALE PLEASE


FLOW CHART
• A flowchart is a diagram that presents logical
structures pictorially.

• It serves to help a person easily understand a


program written by someone else.

• To achieve this, present / standard symbols are


used to denote different operations. These
most used symbols are:
FSC 113 NOT FOR SALE PLEASE
Flowchart Symbols
SYMBOL MEANING

START / STOP

PROCESS

DECISION

INPUT / OUTPUT

PREDEFINED PROCESS

CONNECTOR

LOGIC FLOW
FSC 113 NOT FOR SALE PLEASE
Class Programming Example
Flowchart for Summing first 10 natural numbers
START

i = 1, SUM = 0

Is i > YES
STOP
10

NO

SUM = SUM + i
i=i+1

FSC 113 NOT FOR SALE PLEASE


Flowchart Example 2

• The order of a new customer is accepted


provided it is not more than N5,000 otherwise
it is referred to a supervisor. An order from a
regular (old) customer is always accepted.

– Define the problem


– Analyze the problem
– Draw a flowchart

FSC 113 NOT FOR SALE PLEASE


Flow Chart

START

CHECK CUSTOMER
STATUS

Order
NEW
YES >
CUSTOME
R? N5000
?
NO NO YES

ACCEPT ORDER REFER TO


SUPERVISOR

FSC 113 NOT FOR SALE PLEASE


DEVELOPMENT & IMPLEMENTATION
(PROGRAMMING)

FSC 113 NOT FOR SALE PLEASE


DOCUMENTATION
• Programs are meant to be documented, much like
how an equipment manufacturer adds a USER
MANUAL.

• This ensures that others can read the documentation


and use the appliance / program.

• Documentation should start from the Problem


Definition stage and run all the way to the
programming stage and even beyond (such as
pictures of the program in use and possibly
maintenance / troubleshooting steps).
FSC 113 NOT FOR SALE PLEASE
PROGRAMMING IN VISUAL
BASIC
LECTURE 2

FSC 113 NOT FOR SALE PLEASE


VISUAL BASIC
• BASIC means Beginners All-Purpose Symbolic Instruction
Code.

• It is arguably the most used programming language in the


history of computing.

• It is a very simple but powerful programming language


that combines the mathematical capabilities of FORTRAN
and the business orientation of COBOL.

• VISUAL refers to the method used to create Graphic User


Interfaces.

FSC 113 NOT FOR SALE PLEASE


TYPES OF PROGRAMMING
LANGUAGES
• PROCEDURAL PROGRAMMING LANGUAGE: This is
similar to a list of instructions telling the computer
what to do in a sequential or step-by-step manner.
• These type of language are also called IMPERATIVE
or TOP-DOWN language. Examples are FORTRAN,
COBOL and C.

FSC 113 NOT FOR SALE PLEASE


Object Oriented Programming Languages
• Unlike procedural languages, OOP languages are all about objects.
• An object is a self-contained component of a program that knows how to perform certain
actions and how to interact with other elements of the program.

• Objects are the basic units of object-oriented programming.

• A simple example of an object is a car.


– Make, Model, Colour would be considered as the properties of the car.
– would be considered as methods of the car.

• A method in object-oriented programming is like a procedure in procedural


programming. The key difference here is that the method is part of an object. In object-
oriented programming, you organize your code by creating objects, and then you can
give those objects properties and methods.

• A key aspect of object-oriented programming is the use of classes. A class is a template or


blueprint of an object. You can think of a class as a concept, and the object as the
embodiment of that concept.

• Examples of object-oriented languages include Visual Basic, C#, Java, Python.

FSC 113 NOT FOR SALE PLEASE


• Visual Basic (VB) is an event-driven OOPL, and
consists of two major components the Controls
& Commands.

• Controls are graphical items (such as Buttons &


Text Box) that could be placed on the FORM.

• Commands are instructions written by the


programmer instructing VB to carry out a
specified action.

• Event driven means an action has to be triggered


either by the user or a pre-set system timer.
FSC 113 NOT FOR SALE PLEASE
• Event-driven programs have items/controls
on a form, that carry out associated
procedures once a user interacts with them.
Example includes clicking a button

FSC 113 NOT FOR SALE PLEASE


VB Programming Environment
• To Launch VB 6: • To Launch VB:NET
– From Windows Desktop
– Click Start
– Click All Programs
– Click Microsoft Visual
Studio 6
– Click on Microsoft Visual
Basic 6.0

* To be demonstrated in class

FSC 113 NOT FOR SALE PLEASE


Features of VB
• KEYWORD: a pre-defined word that is
already defined and reserved for a specific
purpose. In VB these include: DIM, REM,
LOOP, IF, ELSE, END, FOR, SUB etc

• CONSTANT: a numeric or string value that


does not change in a program. They include
Integer, Long, Single, Double, String

FSC 113 NOT FOR SALE PLEASE


* Assignment: Learn all the keywords in VB
Features of VB
• VARIABLES: A location or area within a
computer memory where data is/are stored.
– Variable name is the name given to a storage
location in a program.
– Variables are assigned values
– The assigned value can change but the variable
name and storage location cannot change until
the program terminates or the variable is
released.

FSC 113 NOT FOR SALE PLEASE


• Must begin with a letter
• May contain letters, digits and underscore all
others characters are not allowed (not even
space). Similar to an email address.
• Cannot exceed 255 character in length
• Cannot be a VB keyword
• Can be in any case ie Capital or Small letters.

Example
amount = 10000
fName
Ikeja
FSC 113 NOT FOR SALE PLEASE
DATA TYPES IN VB
• Data type is the attribute of a variable or field
which determines the kind of data it can hold.

• Common Data types in VB include:


– Integer - String
– long - Boolean
– Array
– User-defined (see page 114 of course material)

FSC 113 NOT FOR SALE PLEASE


DATA DECLARATION IN VB
• The keyword DIM is used to declare variables
in VB.
• It tells VB to reserve a space in memory for
the variable
• Also tells VB what variable type to expect
when the program is executed.
Example
1. Dim name as string
2. Dim phone as long
3. Dim age as integer
FSC 113 NOT FOR SALE PLEASE
OPERATORS IN VB
• There are 3 types of operators in VB
– Arithmetic operators such as addition (+),
subtraction (-), division (/), multiplication (*),
bracket, mod

Example: 2 * 8 + 10

– Relational Operators
These operators result in either True or False.
These can also be classified as Boolean operation
FSC 113 NOT FOR SALE PLEASE
• Relational Operators include
greater than (>), less than (<), not equals to
(<>), less than or equals to (<=) etc.

Example of Relational Operators


a = 10, b = 50

• Logical Operators
– These include AND, NOT, XOR, OR, EQV

FSC 113 NOT FOR SALE PLEASE


OPERATOR ORDER OF PRECEDENCE
OPERATOR OPERATION ORDER OF
PRECEDENCE
() Bracket / Parentheses 1

^ Exponential 2
NOT Negation 3
* and / Multiplication & Division 4

\ Integer Division 5

Mod Modulus 6
+ and - Addition or Subtraction 7

<, <=, >, <> Relational 8


= and <> Equality 9
AND Logical AND 10
OR Logical OR 11
FSC 113 NOT FOR SALE PLEASE
VB CONTROL STRUCTURE
• Instructions are executed in 3 ways
– Sequentially: one after the other.
– Selection: Choosing an instruction to execute out a
number of alternatives.

• Example
1. If logical expression is true then execute this
Else execute something else
End If

FSC 113 NOT FOR SALE PLEASE


Example of Selection Control Structure

If (Score > 70) then GP = 5


Else if (Score > 60 AND Score< 70) then GP = 4
Else if (Score > 50 AND Score< 60) then GP = 3
Else if (Score > 40 AND Score< 50) then GP = 2
Else if (Score > 30 AND Score< 40) then GP = 1
Else GP = 0
End If

FSC 113 NOT FOR SALE PLEASE


VB Control Structure
LOOPS
• Looping: Allows repeating an instruction or set of
instructions until a preset condition is met.
• Looping statements usually have an index or
counter, which is incremented / decremented
during the loop structure.
• After or before each loop the index is tested to
determine if to proceed with the loop or
terminate.
FSC 113 NOT FOR SALE PLEASE

– For index = 1 to 10 step 1
Print index
Next index

• The
loops to be done is known before hand

FSC 113 NOT FOR SALE PLEASE


DO..LOOP
This is used when the number of loops to be done is not known in advance.

Do Until Do While
Example Example
a = 100, b = 10 a = 100, b = 10
Do Until ( a < b) Do While ( a > b)
b= a b b= a b

loop loop

These two loop statement accomplish the same thing and are examples
of PRE-TEST loop statements.

That is the test condition is tested first before executing the body of the loop
FSC 113 NOT FOR SALE PLEASE
Do Until Do While
Example Example
a = 100, b = 10 a = 100, b = 10
Do Do
b= a b b= a b
Loop Until ( a < b) Loop While ( a > b)

These two loop are examples of POST-TEST loop statements,


whereby the body of the loop is executed before test condition is carried out

FSC 113 NOT FOR SALE PLEASE



– While (a > b)
Print a
b=a*b
Wend


use index, hence variables MUST be assigned
values before commencing the loop.

FSC 113 NOT FOR SALE PLEASE


• 1KB = 1,024 BYTE

• 1MB = 1024 * 1024 BYTE

• 1GB = 1024 * 1024 * 1024 BYTE

FSC 113 NOT FOR SALE PLEASE


LAUNCHING VISUAL BASIC 6
INTEGRATED DEVELOPED INTERFACE
• Click START button
• CLICK PROGRAMS
• CLICK on MS VISUAL
STUDIO 6
• On the NEW PROJECT
dialog, click on
STANDARD EXE.
• Click OPEN

*The above is for Windows


XP / Vista. Windows 8
varies slightly
FSC 113 NOT FOR SALE PLEASE
THE VB I.D.E
Title Bar Menu Bar

Tool Bar

FSC 113 NOT FOR SALE PLEASE


• Title Bar
– The top most line of the IDE window. It contains the project name and
icon.
– On the LEFT side it contains 3 icons that minimize, maximize and
close the IDE window

• Menu Bar
– Provides access to most of the commands that control the
programming environment (IDE), such as File, View, Tools etc.

• Tool Bar
– Collection of buttons that serve as shortcuts for executing commands
and controlling the VB IDE. It includes icons such as OPEN File, SAVE,
New Project etc

• Tool Box
– This gives easy access to common VB controls or tools such as
textbox, command button, list box, etc.

FSC 113 NOT FOR SALE PLEASE


• Form Designer Window
– The form designer sits right at the center of the VB IDE. It is the heart and soul
of the Visual Basic GUI development process. This is where you can drag and
drop dialog boxes, pictures, and other graphical elements to complete your
project. Familiarize yourself with the form designer; you will be using it
extensively in your programs.

• Properties Explorer
– The properties window allows you to view and edit properties associated with
any particular form or control element.

• Form Layout Window


– This little window helps position the designed form where you want it in
relation to the whole screen

• Project Window
– The project window shows all the files and folders included within the
project.

• Code Editor
– The code editor window is used to manually write codes / program
instructions for a control. There is a separate code editor window associated
with each form and control element.

FSC 113 NOT FOR SALE PLEASE


COMMON WINDOWS FORM CONTROLS
VB
• Textbox: is used to accept information entered by the
user during execution or display an information to the
user.

• Label: Primarily used to display text that a user cannot


edit directly by the user during execution.

• Button: Trigger event(s) or action(s) when clicked by


the user.

FSC 113 NOT FOR SALE PLEASE


Class Example 1

Program to Calculate Area &


Circumference of a Circle

FSC 113 NOT FOR SALE PLEASE


• Problem Definition
• Problem Analysis NB: Reporting should
– Input & Output be based on the
– Procedure steps of the Software
– Variables & their data types Development Life
– Buttons & functions Cycle (SDLC)
• Algorithm & Flowchart
• Implementation
• Documentation

FSC 113 NOT FOR SALE PLEASE


• Problem Definition: A program in MS Visual Basic
to calculate the area & circumference of a circle.

• Problem Analysis:
– Input = radius of the circle
– Output = area & circumference
– Procedure:
• Area = Pi * radius * radius
• Circumference = 2 * Pi * radius

FSC 113 NOT FOR SALE PLEASE


Modeling & Design
– Variables:
Start
• Dim rad as Integer
• Dim area as Double
Enter • Dim cirm as Double
RADIUS

– Sketch what the


Output
application would look
Area = Pi * RADIUS2
Cirmf = 2* Pi * RADIUS
Area & like
Cirmf

Stop

Flowchart
FSC 113 NOT FOR SALE PLEASE
Implementation & Documentation
• Implementation & programming to be done in
the lab and submitted in your lab workbooks.
IT WILL BE GRADED AS YOUR TERM ASSIGNMENT

• Documentation

FSC 113 NOT FOR SALE PLEASE


Class Example 2

Program to Add first 10 Natural


Numbers in VB

FSC 113 NOT FOR SALE PLEASE


Program to Add first 10 Numbers
Private Sub Button1_Click(sender As Object, e As
EventArgs) Handles Button1.Click

Dim firstNumber = Val(TextBox1.Text)


Dim lastNumber = Val(TextBox2.Text)
Dim index = firstNumber

Dim result = 0

For index = firstNumber To lastNumber Step 1


result = result + index
TextBox4.Text = Str(index)
Next index

TextBox3.Text = result

End Sub

FSC 113 NOT FOR SALE PLEASE


End of Class

THANK YOU FOR ATTENDING

FSC 113 NOT FOR SALE PLEASE

You might also like