The Following program will demonstrate the use of a conditional statement, IF THEN. to make the program interesting it will be used to convert temperatures between C and F and F and C.
___________________________________________________________
REM Simple IF THEN Selection to convert between Celsius and Fahrenheit and Fahrenheit and Celsius
DIM InputNumber AS SINGLE
DIM SelectCalc AS STRING
REM Ask User to select Between Celsius to Fahrenheit or Fahrenheit to Celsius
CLS
INPUT " Please Enter C to convert from Celsius to Fahrenheit or Enter F to convert from Fahrenheit to Celcius "; SelectCalc$
If SelectCalc = "c" THEN
Input "Please enter the Celsius "; InputNumber
Print InputNumber ; "Celsius = " ; (InputNumber +32) * 1.8 "Fahrenheit "
END IF
IF SelectCalc$ = "f" THEN
Input "Please enter the Fahrenheit "; InputNumber
Print InputNumber ; "Fahrenheit = " ; (InputNumber -32) / 1.8 "Fahrenheit "
END IF
_____________________________________________
If all goes well when the program is run the a message will appear asking for F or C. Once that is entered the next message will ask for Celsius or Fahrenheit. once entered it the program displays the conversion.
The program has no error catching and stops after the first run It is unstructured and in the following part of the lesson We will use this program and add some additional code to increase its usability.
So you will have noticed that the program will only except a lower case "c" or a lower case "f". The code can be amended to allow capital C and F to also be accepted.
If SelectCalc = "c" OR SelectCalc = "C" THEN
IF SelectCalc$ = "f" OR SelectCalc = "F" THEN
Amend the code to include the 'OR SelectCalc =' and run the program. You will find now that the program allows Capitals and lower case to be used.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment