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

December 14, 2009

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]
Read the rest of this entry »