Transcript Document

Visual Basic 6
Programming.
Lecture 2 : January 2005
Dr. Andrew Paul Myers
07/07/2015
1
Logical Variables.
To define use :
Dim blnTest as Boolean
Assignments :
blnTest = True
blnTest = False
Numerically 0 is False, 1 is True.
07/07/2015
2
Logical Expressions.
A logical expression is made up of :



Variables.
Constants.
Logical operators.
e.g. some simple test conditions:
intRadius = 24
intRadius < > intOld_Radius
07/07/2015
3
Logical Operators I.
Test conditions :
<
>
<=
>=
=
<>
07/07/2015
less than.
greater then.
less than or equal to.
greater than or equal to.
equal to.
not equal to.
4
Logical Operators II.
Logical Test Operators :
And
Not
Or
e.g.
(intA
07/07/2015
<=
3)
Or
(intB
>=
5)
5
Conditional Statements.
Conditions use logical expressions.
A simple If statement :
If sngX = 1.5 Then sngY = 20.5
If strText < > “Again” Then End
07/07/2015
6
Block If-Then-Else.
If sngZ >= 3.2 Then
TextBox1.Text = “3.2 or more”
intI = intI + 1
blnTest = True
End If
If intX = 1 And intY = 2 Then
intZ=3
Else
intZ=2
End If
07/07/2015
7
General Form.
If <condition(s)> Then
<statement(s)>
ElseIf <condition(s)>
<statement(s)>
Else
<Statement(s)>
End If
07/07/2015
8
Error Trapping.
If sngRadius < 0.0 Then
TextBox2.Text “Error –ve Radius!”
RadiusBox.Text = “”
Else
sngArea = sngPi * sngRadius^2
Label1.Caption = “Area cm^2 “
TextBox1.Text = sngArea
End If
07/07/2015
9
Case Statement.
Select Case intMark
Case Is >= 70
TextBox1.Text = “First!”
intFirsts = intFirsts + 1
Case Is >= 60
TextBox1.Text = “2.1”
intTwoOnes = intTwoOnes + 1
Case Else
TextBox1.Text = “2.2 or less.”
End Select
07/07/2015
10
Case : General Form.
Select Case <variable>
Case Is <condition(s)>
<statement(s)>
Case Is <condition(s)>
<statement(s)>
.
.
.
Case Else
<statement(s)>
End Select
07/07/2015
11
Intrinsic Functions.




Functions return a value of certain data type.
Parameters are passed to functions.
Parameters can be variables, constants or expressions.
Intrinsic means “built in”.
dblY=Tan(1#)
sngY=Abs(-3.5)
sngY=Abs(-3.5*sngX) dblZ=dblX+3.0*Log(dblY)
dblY=Val(strRadius) dblY=Log(dblX)
strAnswer=Str(sngX)
dblY=Sin((dblPi*degrees)/180#)
07/07/2015
12
More examples…
Sin(x)
Tan(x)
Sqr(x)
Cos(x)
Atn(x)
Abs(x)
Is there a Asn(x) Function?
NO!
Arcsin(X) = Atn(X / Sqr(-X * X + 1))
See Derived Maths Functions Sheet!
07/07/2015
13
Non maths functions.
e.g. Convert string to number.
If (IsNumeric(strText_string)) Then
sngValue = Val(strText_String)
End If
e.g. Convert number to string.
strText_String = Str(intData1)
TextBox1.Text = Str(dblVolume)
07/07/2015
14
Aids to Program
Development.



07/07/2015
Pseudo Code : “English” like version of
program.
Flowcharts : Graphical representation of
the programs structure.
Debugger : Inbuilt tool in VB, allowing
users to stop and monitor the program at
specified “break points”.
15
Pseudo Code I.
How to approach writing a program to solve
the quadratic equation of the form :
ax  bx  c  0
2
The general solution is :
 b  b  4ac
x
2a
2
07/07/2015
16
Pseudo Code II.
Start program.
Declare and initialise variables.
Display a program window with titles and instructions.
Do Until program finished.
Enter values for a,b,c.
If root is complex Then.
Calculate complex roots.
Else
Calculate real roots.
End If
Display answers.
Program finished?
Loop.
End program.
07/07/2015
17
Flowcharts I.
Symbols :
Start or end program :
Start

Process for function :
Print
Titles

Decision :

Connector :

07/07/2015
End?
A
18
Start.
Declare
Variables.
Initialise
Variables.
Print Titles.
Read values
of a,b,c.
A
Yes
Real
roots?
Calculate real
roots
No
Calculate
complex root.
Print results.
No
A
End?
Yes
07/07/2015
End.
19
VB Debugger.
Select options from the “Debug” menu.
 Set “Breakpoints” to halt the program.
 Then able to examine variable values,
highlight or position cursor over variable
to get value.
 Use “Watch points” to monitor a list of
variables as the program runs.
 Use step options to proceed line by line or
enter subroutines.
07/07/2015
20