Wednesday, June 03, 2009

Learning BASIC

I'm trying to relearn BASIC. Beginning Programming For Dummies and the Liberty Basic CD that comes with it are guiding me through many of the functions and concepts that I learned (and forgot) in the 1980s, and I am happy to say that it's coming back to me without too many hiccups. For instance, the following program worked the first time, without any bugs, despite the fact that it has a bunch of interlocked subprograms although I later had to update it when I found some loop situations that had to be eliminated:

REM Testing a function that calculates the user's age in any given year
REM And also tests to make sure that users use real numbers and future years, not past years
REM I found out that you can call subprograms from within subprograms
REM
NOMAINWIN
NOTICE "Enter the year you were born, and a year in the future."
NOTICE "This program will calculate how old you will turn in that year."
REM
REM The subprogram to ask the year you were born
GOSUB [yearBorn]
REM
REM The subprogram to ask the future year
GOSUB [futureYear]
REM
REM The function that calculates the difference
REM
NOTICE "This is how old you will be in the future: "; CALCULATEAGE(FUTUREYEAR,YEARBORN)
END

[yearBorn]
PROMPT "What year were you born?"; YEARBORN
REM
REM Another subprogram that checks YEARBORN is a real number
REM
GOSUB [checkYearBorn]
RETURN

[checkYearBorn]
WHILE YEARBORN < 1 NOTICE "You need to enter a year from sometime in the last two millenia. Numbers only." PROMPT "What year were you born?"; YEARBORN WEND RETURN [futureYear] PROMPT "Type a year in the future."; FUTUREYEAR REM REM Test to make sure a real number is used, via a subprogram REM GOSUB [checkFutureYear] REM REM Another test, to make sure that the future year is after the birth year GOSUB [checkFutureYearFuture] RETURN [checkFutureYear] WHILE FUTUREYEAR < 1 NOTICE "You need to enter a year from sometime in the last two millenia. Numbers only." PROMPT "Type a year in the future."; FUTUREYEAR WEND RETURN [checkFutureYearFuture] WHILE FUTUREYEAR <= YEARBORN NOTICE "The year you entered is before the year you were born. Enter a year after you were born." PROMPT "Type a year in the future."; FUTUREYEAR WEND RETURN FUNCTION CALCULATEAGE(FUTUREYEAR,YEARBORN) CALCULATEAGE = FUTUREYEAR-YEARBORN END FUNCTION


The program asks a user's age, and then a year in the future, and calculates how old he or she will be in the future year. It also checks for bad data -- letters and inconsistent years.

Maybe I can port it to the Web, but that would require me dusting off my old javascript skills, which I haven't tapped in about five years.

1 comment:

  1. I recommend looking into RealBasic. It allows compiling for Windows, Linux, & Mac using the same source code. No need to develop source for each platform!

    ReplyDelete

All comments will be reviewed before being published. Spam, off-topic or hateful comments will be removed.