1
Dr. T.P. Clement
CE 3010 class notes
Quotes
•
Genius is 1 percent inspiration and 99 percent
perspiration!
•
There is no secret to success—it is a result of
preparation, hard work, and learning from the
mistakes
‐
Colin Powell
•
If you are going to achieve excellence in big
things, you develop the habit in little things.
Excellence is not an exception, it is a prevailing
attitude
‐
Colin Powell
1
Dr. T.P. Clement
CE 3010 class notes
Learning Objectives
•
Why decisions?
•
IF
‐
THEN
‐
ELSEIF
‐
ELSE conditional operations
•
Combining conditions
•
Loops
•
Examples
2
Dr. T.P. Clement
CE 3010 class notes
Program to Find Real Roots of a Quadratic
Equation
Sub Quad()
'
A program to compute the real roots of a quadratic equation, ax2 + bx
'
+ c.
' These coefficients are prompted as input from the keyboard.
The
'discriminant is ‘calculated but no decision made based on its value.
'
Hence execution will halt and
'
an error will result if the square root of a negative discriminant is
'
attempted.
' Otherwise the two real roots r1 and r2 will be computed and output
' to the screen.
Dim a
as double
, b
as double
, c
as double
, d
as double
, r1
as double
Dim r2 as double
a = Cells(1,2)
b = Cells(2,2)
c = Cells(3,2)
d = b*b – 4*a*c
Cells(5,2)= d
r1 = (b + sqr(d))/(2*a)
r2 = (b – sqr(d))/(2*a)
Cells(6,2)=r1
Cells(7,2)=r2
End Sub
3
What if d is negative?
VBA cannot calculate
square root of
negative number
Dr. T.P. Clement
CE 3010 class notes
Why Decisions?
•
Need to make decisions in programs
•
In the quadratic equation code,
a*x
2
+ b*x + c = 0
has real roots if the discriminant,
d = b
2
‐
4*a*c
is >= 0
Otherwise complex roots
4
Dr. T.P. Clement
CE 3010 class notes
VBA Comparisons
•
VBA comparisons made with
IfThenElse
statements
Syntax:
If (conditions) Then
………
………
Else
………
………
Endif
5
Dr. T.P. Clement
CE 3010 class notes
VBA Comparisons
•
If the condition is true the lines immediately
following
If
are performed
•
If the condition is false control shifts to the lines
following
Else
•
If the operation is only performed if the condition is
true (i.e. no alternative)
Else
and the lines between
it and
Endif
not needed
6
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document2
Dr. T.P. Clement
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '09
 Clement
 Dr. T.P. Clement

Click to edit the document details