210 likes | 254 Views
Visual Basic 6 Programming. Lecture 2 : January 2005 Dr. Andrew Paul Myers. Logical Variables. To define use : Dim blnTest as Boolean Assignments : blnTest = True blnTest = False Numerically 0 is False, 1 is True. Logical Expressions. A logical expression is made up of : Variables.
E N D
Visual Basic 6 Programming. Lecture 2 : January 2005 Dr. Andrew Paul Myers
Logical Variables. To define use : Dim blnTest as Boolean Assignments : blnTest = True blnTest = False Numerically 0 is False, 1 is True.
Logical Expressions. A logical expression is made up of : • Variables. • Constants. • Logical operators. e.g. some simple test conditions: intRadius = 24 intRadius < > intOld_Radius
Logical Operators I. Test conditions : < less than. > greater then. < = less than or equal to. > = greater than or equal to. = equal to. < > not equal to.
Logical Operators II. Logical Test Operators : And Not Or e.g. (intA <= 3) Or (intB >= 5)
Conditional Statements. Conditions use logical expressions. A simple If statement : If sngX = 1.5 Then sngY = 20.5 If strText < > “Again” Then End
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
General Form. If <condition(s)> Then <statement(s)> ElseIf <condition(s)> <statement(s)> Else <Statement(s)> End If
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
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
Case : General Form. Select Case <variable> Case Is <condition(s)> <statement(s)> Case Is <condition(s)> <statement(s)> . . . Case Else <statement(s)> End Select
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#)
More examples… Sin(x) Cos(x) Tan(x) Atn(x) Sqr(x) Abs(x) Is there a Asn(x) Function? NO! Arcsin(X) = Atn(X / Sqr(-X * X + 1)) See Derived Maths Functions Sheet!
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)
Aids to Program Development. • 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”.
Pseudo Code I. How to approach writing a program to solve the quadratic equation of the form : The general solution is :
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.
Flowcharts I. Symbols : • Start or end program : • Process for function : • Decision : • Connector : Start Print Titles End? A
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.