![]() |
![]() |
![]() |
|
|
||
|
EXTRA INFO |
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.
As well as pseudocode we can use a construct diagram to plan a program using 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.
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
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!
|
||