Site hosted by Angelfire.com: Build your free website today!

EXTRA INFO

Getting Started with VB6

GUI

Removing the form

Getting to the Next Lesson

Code Used

Variables and Data Types

Operators

Message Box Info

Standard Toolbar and Options

Example of Full Planning and Testing Document

Planning Outline

Finished Planning Example

Finished Code with Single If Check


Excellent !!

lesson 7 -

AIM - To Understand The If...Then....End If Statement

 
The Answer is:

DIm telephoneNumber as String

You may have used a different name (TeleNo) but you probably didn't get 'string' and used integer. We use string because a telephone number never behaves like a number i.e. we wont perform mathematical functions on this number - nor would we want to by accident as it would render the number useless. Therefore a telephone number is treated as TEXT.


Open your 'Circle Area' program from last lesson. Click here to download the final planning document.

Review

There are 3 stages to planning we can remember them by the acronym PST. The Structure stage needs to be divided further into the logical sequence of programming i.e. input >>process>>>output.

  • Purpose (The brief or problem to be solved, in your OWN words)
  • Structure
    • Input
    • Process
    • Output
  • Testing - Table (Test data, Result, Further action)

As well as pseudocode we can use a construct diagram to plan a program using shapes.

Construct Shapes

We now need to test the program and correct any errors. Look at the completed test table in the final planning document. The test table lists what data works and does not need review and also what data does not work and will need review. To design a good program you must try to think or every possible thing that the user could enter and 'wreck' your program. Run your program and check you agree with the results in the test table.

The main problem with our program is it stops running if the user enters letters instead of a number for the radius. The second problem is the unit of measurement needs to be checked that it is NOT a number as a number changes our final display and makes the result appear wrong. If the user enters a '3' for the unit of measurement and a 2 for the radius the final message box will read "The area of of a circle with radius 23 is 12.566323^3" obviously this is incorrect.

So we need to check that the user entry for radius is a number and the user entry for unit of measurement is a string (or text). Luckily there is a special inbuilt function that will check for numbers called IsNumeric. This function returns a value "True" if the value is numeric and "False" if the value is not.

We have already seen the If...Then...Else....End If statement in a previous program and understand it allows us to make a choice. Between two options.

If <some condition or check > Then

Do this

Else

Do this

End If

Here we only need ONE option "If the user enters a non numeric value we will display new input box telling the user their last entry was not a number and ask for new input. If they did enter a number the program drops to the else statement which has no argument so nothing happens the If statement ends and the program continues. Change your code to match the code below. Note the REM comments and indenting to clearly show beginnings and ends.

IsNumeri False

Now we need to apply the opposite check to the unit of measurement.

If IsNumeric (unit) = True Then

And then ask for the unit of measurement again.

unit= InputBox("Sorry, that was not a unit of measurement. Please re-enter")

We don't actually need the full If...Then...Else....End If statement because the Else part does nothing anyway so we could just end the statement after this line with End If. Try to code this yourself check the layout below. When you are ready download the final code here and check against yours

Code

This check has it's limitations. If the user types text into the radius request a second the time the program will error again. What is required is a 'loop' something that will keep checking and only allow the program to progress when the required data has been entered. We will move on to loops in level 2.

You should now understand how to plan a program, create a message box, an input box, begin and end a program, declare variables with correct names and data types and be able to use an If statement to make a choice. We will now put all these elements together. Read the brief below and create the plan and then the program.

The Brief

You are to design a program fro your Physics class that converts Centigrade to Fahrenheit. To convert to Fahrenheit, multiply the entered number by 1.8 and add 32. The users data entry should have at least one check for numbers. The program needs to be self explaining and easily understood by the user.

First open up your saved version of the planning document and re-save as 'ConvertTemperaturePlan'. If you don't have this document click on the 'Planning Outline' on the left. Look at the completed planning document (link on left) and plan a program for this brief. Follow each of the steps below. There are links to 'hint' documents if you get stuck, however if you don't complete the work yourself you will never learn to code. Use the hints ONLY if you are absolutely stuck!

  1. Put the brief in your own words -hint 1
  2. What Input is needed?-hint 2
  3. What will the program do with the input?-hint 3
  4. What will the program display at the end? (output)-hint 4
  5. What variables will you need (related to input and output)-hint 5
  6. What will you call the variables?-hint 6
  7. What data types are they?-hint 7
  8. Don't put any checks in - ONCE the program is running THEN use the test table to see where checks are needed.-hint 8
  9. Write out the plan in pseudocode (a bit closer to real code)-hint 9
  10. Use shapes to plan what will happen-hint 10
  11. Code the program - with indents and REMs -hint 11
  12. Run it -hint 12
  13. Correct it where necessary.-hint 13
  14. Complete the test table-hint 14
  15. Add any checks needed-hint 15
  16. Run the program

 

QUIZ 3