next up previous
Next: 6. Message Boxes Up: VisualBasic Previous: 4. Saving your VisualBasic


5. IF/THEN/ELSE (and End) Statements

Now edit the quadratic solution programme so that you are using the following values: a=1, b=2, c=8. Run this project. Note that you will get an error message. Click on the Debug button, and you will see the line in your code which is taking the square root highlighted in yellow. This is the actual line in the code which caused the error. Why do you think this happened in this case? (Note, you'll have to click on the stop button to leave the run mode, and to get back to the edit mode.)

This error message is very embarrassing, so we'll investigate a method of getting around it. What you want is for the computer to test whether $b^2 - 4ac$ is negative or not, before proceeding to calculate x.

This "test" can be done with an If ... Then statement. Edit your code so that it looks like:

Private Sub Command1_Click()

a = 1 ' Setting the values of the coefficients of the quadratic
b = 2
c = 8

If b^2 - 4 * a * c < 0 Then
   Print "Imaginary Solution!"
Else
   x = ( -b + Sqr( b^2 - 4 * a * c ) ) / ( 2 * a )
   Print "x = "; x
End If

End Sub

(Note that the indentation in this code is for legibility only.)

In the above the line

a = 1 ' Setting the values of the coefficients of the quadratic
contains a comment which is simply everything after the quotation mark. Remember, comments are totally ignored by VisualBasic; they are designed so that the programmer (i.e. you!) can put in information to remind yourself (and others who will read your program) of what the program is designed to do at that point. It is good programming practice to put lots of comments into your programme!

You can no doubt guess what the above code is doing. If b^2 - 4ac<0, then it prints out the warning message Imaginary Solution and stops at the End statement. If not, then it calculates the root. Note that the warning message may flash up on the screen too quickly for you to see...

Note that the ELSE branch of the IF statement is optional, so the following would also work (make sure you understand why!):

Private Sub Command1_Click()
a = 2
b = 4
c = 1

If b^2 - 4 * a * c < 0 Then
   Print "Imaginary solution!"
   End
End If

x = ( -b + Sqr( b^2 - 4 * a * c ) ) / ( 2 * a )
Print "x = "; x

End Sub

Note that the message Imaginary solution may flash up on the screen too quickly for you to see... (i.e. the End statement stops the programme instantly.)

It is important to remember that, in general, each of these If ... Then statement comes with a matching End If.

You can also do more complicated tests like:

If x > 0 and y < 2 Then ...

If a = 1 or b = 2 Then ...

If Var1 >= 1 and xyz <> 2 Then ...

In the above, the Var1 >= 1 means "Var1 greater than or equal to 1", and the xyz <> 2 means "xyz not equal to 2".

Furthermore you can use "Not" as well in these If/Then statements. i.e.

If Not a + b = c Then
Print "a + b is not equal to c"
End If

is identical to:

If a + b <> c Then
Print "a + b is not equal to c"
End If

You can also use an ElseIf statement and even nest If ... Then as follows:

If a <= 9 Then
   If b = c And ( d < e Or f >= g) Then
      Print "Hello"
   ElseIf b > c Then
      Print "Goodbye"
   End If
Else
   If h <> j Then
      a = 3*c
   End If
End If

Finally, you can have a brief version of If ... Then without the End If line, so long as everything is on the one line. To illustrate this consider the following code:

a = 2
b = 4

If a < b Then Print "a is less than b"

If b < a Then Print "b is less than a"

Print "Hi there"

The output from the case above would be:

a is less than b
Hi there

CHECKPOINT: Write a program which reads in two numbers A and B and prints out the greater.

Sometimes the computer will give an error when you ask it to do something it can't - e.g. take the square root of a negative number. You can get the program to branch to different parts of the code using the If ... Then statements.

You can get a program to stop instantly by the End statement.


next up previous
Next: 6. Message Boxes Up: VisualBasic Previous: 4. Saving your VisualBasic
Chris Allton 2006-10-27