QBasic: An example of INPUT, IF-THEN-ELSE, GOTO commands

A program for calculating the area of a triangle using Heron’s formula:

[CODE:]
*********************************************************************
CLS

'Program description, it's always good to have one at the top
PRINT "A program for calculating the area of a triangle using Heron's formula."
10 PRINT "Input the value of the triangle's sides:"
INPUT "a = ";a
INPUT "b = ";b
INPUT "c = ";c

'Check if it's a triangle
IF (a < 0 OR b < 0 OR c < 0 OR a + b < c OR a + c < b OR b + c < a) THEN
PRINT "That is not a triangle. Try again."
BEEP
GOTO 10
ELSE

'Calculation
s = (a + b + c)/2
P = s * (s - a) * (s - b) * (s - c)
P = P ^ (1 / 2)
END IF

'Result print-out
PRINT "P = ";P

'Repeat the program
20 INPUT "Repeat the program [press 1 for YES, and 2 for NO]"; out%
IF (out% 1 AND out% 2) THEN
PRINT "Wrong input!"
GOTO 20

ELSEIF out% = 1 THEN GOTO 10
END IF

END
*********************************************************************
[/CODE]

Program description:
You are familiar with the CLS command, it’s used to clear the program window from anything that might have left out from previous programs. The first “paragraph” of this program is used for querying the user to input the variable that are needed to calculate the triangle area. The first line starts with an apostrophe ( ‘ ) is called a comment.
An article from Wikipedia on the necessity of comments:
——
Technical commentators have documented varying viewpoints on whether and when comments are appropriate in source code. Some commentators assert that source code should be written with few comments, on the basis that the source code should be self-explanatory.Others suggest code should be extensively commented (it is not uncommon for over 50% of the non-whitespace characters in source code to be contained within comments).
——
In between these views is the assertion that comments are neither beneficial nor harmful by themselves, and what matters is that they are correct and kept in sync with the source code, and omitted if they are superfluous, excessive, difficult to maintain or otherwise unhelpful.

In the second line the command PRINT is used which prints out anything that’s between the quotes ( ” ), in this case a basic description of the program.
In the 3rd line you can see the number 10 before the command PRINT, it’s used for the GOTO command, more on that later in the text. On the 4th, 5th and 6th line the INPUT command is used to get the needed variables. The syntax of the command is easy, the text in quotes is displayed and the word after it is the variables name (you can name it as you like but you can’t use the names of commands) and they are devided with a semi colon ( ; ).

In the second paragraph the imputed variables get checked if they form a triangle, if not the user gets redirected back to the variable inputting. The first line of that paragraph is used for the IF command which is quite self-explanatory. The another thing to know about the IF command are the states:

< – lower
= – equals
> – higher
<= – lower or equal
=> – higher or equal
<> – different

and the operators:
AND – when both are true
OR – when only one is true

So if in our case any of those states are true the program will return the one line, a beep sound (unnecessary but makes the program more attractive) and returns the user to the variable input (hence the 10 number). On the other hand, if those states are false than the program will skip the code after the THEN command and execute the one after the ELSE command, which in our case are the needed calculations. The END IF command ends the IF circle.
The next course of action is the result print-out where everything is self-explanatory so I won’t stop there. After that is the “repeat the program” part which is unnecessary, the user can simply exit this program and restart it, but it contributes to the functionality of the program and it’s attractiveness. It’s similar to the IF statement (or command) earlier but here the AND operator is used which means if both of the states are true the program will return the line of text and return the user back to the input ( ’20’ ). The ELSEIF is kinda of a “second IF” and in this case returns the user to the variable input at the top of the program if the value of the ‘out%’ variable equals 1. If the number 2 is in-putted the program will proceed to the next command, which is the END command, because the state out% = 2 is not described.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: