Introduction to computer programming using Visual Basic - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Introduction to computer programming using Visual Basic

Description:

MsgBox('Greetings') The message box function displays the text we write between the quotes. ... Make the greeting program interactive. String (text) Integer ... – PowerPoint PPT presentation

Number of Views:96
Avg rating:3.0/5.0
Slides: 18
Provided by: fishe5
Category:

less

Transcript and Presenter's Notes

Title: Introduction to computer programming using Visual Basic


1
Introduction tocomputer programmingusing Visual
Basic Arni Magnusson
2
Why should we write computer programs?
To automate and speed up tasks that will be
performed more than once. Some computer programs
have been written for us already, such as
Program Task Pine send emails Word write
documents Excel calculate on a spreadsheet
3
How do we write computer programs?
A program is an ordered sequence of
instructions First do this Then that Finally,
show the results The instructions are written in
code which the computer understands. We can
choose from several programming languages. In
this course we will use Basic (Beginners
All-purpose Symbolic Instruction Code). Among
other commonly used languages are Fortran,
Pascal, Java and C.
4
Writing our own program
Define a task, for example greeting the user. 1.
Open Excel Start - Programs - Excel 2. Open
Visual Basic Editor Tools - Macro -
VBEditor 3. Open code window View -
Code Sub TheGreetingProgram()
MsgBox("Greetings") End Sub 4. Run Run - Sub
5
Word for word
Sub Marks the beginning of our program
code. TheGreetingProgram() Name of
program. MsgBox("Greetings") The message box
function displays the text we write between the
quotes. End Sub Marks the end of our program
code.
6
Make the greeting program interactive
The output from an interactive program depends on
the input from the user. Here, we store the
input in a variable called UserName. Sub
TheGreetingProgram() Dim UserName As String
UserName InputBox("What's your name?")
MsgBox("Greetings, " UserName "!") End
Sub The output text is concatenated from three
seperate text strings.
7
Data types used in this lab session
String (text) Integer (whole number) Double (d
ecimal number)
8
Temperature converter FtoC
We store the two temperature values as seperate
variables, both of them decimal. Sub FtoC()
Dim fTemp As Double Dim cTemp As Double
fTemp InputBox("Temp in Fahrenheit") cTemp
(5/9)(fTemp-32) MsgBox(fTemp Chr(176)
"F equals " _ ctemp Chr(176)
"C") End Sub
9
Code anatomy
Sub Dim End are statements FtoC() is the
program name ftemp ctemp are variables As is
a keyword Double is a data type InputBox()
MsgBox() Chr() are functions / - are
operators
10
Visual Basic programmers conventions
Program names should start with an upper case
letter and variable names with a lower case
letter. Declare variables at the top of the
code. Break long lines with _ Indent blocks of
code and broken lines. Insert empty lines to
seperate long code into sections. Write comments
that explain the code.
11
Writing comments
At the beginning of our code we should write a
concise description of what the program
does. Inside the code we can write explanations
of variable names, explanations and state
descriptions. Comments begin with ' and serve as
reminders and documentation. They are only to be
read by computer programmers and have no effect
on the programs execution.
12
Thoroughly commented version of FtoC
Sub FtoC() ' Queries the user for temperature
in ' Fahrenheit and then displays that '
temperature in both Fahrenheit and ' Celsius
units. ' Arni Magnusson, Apr 2001 Dim
fTemp As Double ' temp in Fahrenheit Dim
cTemp As Double ' temp in Celsius fTemp
InputBox("Temp in Fahrenheit") cTemp
(5/9)(fTemp-32) ' F-gtC conversion
MsgBox(fTemp Chr(176) "F equals " _
ctemp Chr(176) "C") End Sub
13
Why comment?
Easier to understand the code, both for other
people and also the author some time later.
Useful for debugging (correcting mistakes in the
code). Enables linking of different programs,
even with only surfacial understanding of how
each program functions. Crystalizes the line of
thought. Skilled programmers write their comments
before writing the program and as they go, not
afterwards. The more serious they are, greater
the emphasis on efficient use of
comments. Comments should not be used to state
the obvious.
14
One-variable rounded version of FtoC
Sub FtoC() ' Queries the user for temperature
in ' Fahrenheit and then displays that '
temperature in both Fahrenheit and ' Celsius
units. ' Arni Magnusson, Apr 2001 Dim
fTemp As Double fTemp CDbl(InputBox("Temp
in Fahrenheit")) MsgBox(fTemp Chr(176) "F
equals " _ CInt((5/9)(fTemp-32))
Chr(176) "C") End Sub
15
Calculations
Arithmetic operators - / Math
functions Abs(), Sqr(), Sin(), Log(),
Exp() User-programmed functions FtoC etc.
16
Save the program code
There are several alternative ways you can save
the Visual Basic programming code you have
written (TheGreetingProgram and FtoC). 1. You
can copy them and paste into an email message.
Send the email to yourself for future access to
the code. 2. (Recommended) Insert - Module and
call it Lesson1. Move both programs to that
module and then close Visual Basic Editor. Now
save the Excel sheet under the name VBlessons.
Attach the file to an email message or keep it in
a safe place.
17
What we covered today
Input and output Data types and
conversions Programmers conventions Comments A
rithmetic operators and math functions File
management
Write a Comment
User Comments (0)
About PowerShow.com