2.1. Hello Word

[<<<] [>>>]

This is the simplest ever example that you can ever find in any programming language. It is quite simple in BASIC. The most frequently used statement is the print command. This simply prints its arguments to the standard output, which is usually the screen.

Example hello1.bas :

' The simplest ever ScriptBasic program
PRINT "HELLO!"

Result executing hello1.bas :

The first line of the program is a comment line. Any line starting with an apostrophe or with the keyword REM is a comment line. In the example we used the ' character but anytime if you feel like an old BASIC fan, and want to use the keyword REM feel free to do so.

As you could realize the sample code contains PRINT while the sentence before uses the same keyword in lower case letters. This is no mistake. ScriptBasic just as most BASIC implementations is quite liberal regarding the case of the identifier. You can write any reserved word in lower or upper or even mixed case. Thus print and PRINT are the same just as Print, print or PrInT. (Although I advise that you avoid using weird and unreadable mixed case.) From now on I will use the upper case version of the keywords in most cases.

As you will learn later, not only the keywords, but also the variables and function names are also case insensitive.

Getting back a bit more to the PRINT statement. You will use this statement many times in your programs to display data on the console or to send HTML code to the browser when programming CGI. Therefore it is worth examining the specialties of this statement.

Old BASIC languages allowed quite complex PRINT statements, that used tabbing, spaces between elements and so on. ScriptBasic treats this statement extremely simple. When you print values to the screen (to the standard output to be precise) ScriptBasic prints out the values that are separated by commas and nothing else. If you need spaces between the elements, or new line at the end of the printed line, you have to specify those.

Example hello2.bas :

A = 1
B = 2
' this just prints "12"
PRINT A,B

Result executing hello2.bas :

On the other hand

Example hello3.bas :

A = 1
B = 2
' this just prints "12"
PRINT A," ",B,"\n"
PRINT "This goes onto a new line"

Result executing hello3.bas :

Now that we discussed in detail that PRINT statement prints only what it is told to we break the rule. There is an exception when PRINT statement prints something, which is not explicitly specified.

When you have a PRINT statement without any argument it would just print nothing according to the rules detailed above. Such a statement would be totally useless and therefore ScriptBasic makes an exception for the case. If you have a single PRINT statement on a line and no argument, ScriptBasic will print new line when executing the statement.

Example hello4.bas :

PRINT "HELLO!"
PRINT
PRINT "HELLO AGAIN!"

Result executing hello4.bas :

To express the new line and make your code more readable you can use the keyword PRINTNL without argument. Note however that this keyword can only be used without arguments!

Both the PRINT and the PRINTNL statements can be used to print values to files as you will see it later.


[<<<] [>>>]