Task 4: Your First Program
|-- What really is a computer program? --|
program is a set of instructions for the computer. These instructions
tell the computer precisely what to do, and the computer always follows
these instructions. Just like people, computers can only follow
instructions if specified in a language they can understand. These are
called programming languages. There are very many languages that the
computer can understand and QuickBASIC 64 (QB64) is one.
a conversation happening between you and your friend. You and your
friends would use words, organized as sentences to convey information
back and forth. Similarly, programming languages contain collections of
words that can be organized into sentences that convey information to
the computer. Programs are basically sets of sentences (sometimes just
a few and sometimes many thousands) that together make sense to both
the programmer and the computer alike.
There are many languages
that the computer can understand. Java, C++, Python, VB, etc. are all
powerful modern computer languages that are used to develop simple to
complex software programs.
typical QB64 program consists of a bunch of statements. Every line of
the program represents a statement and every statement is an
instruction for the computer. When we ask the computer to execute a
QB64 program, it takes the program and reads the first statement. It
understands what we’re trying to say and then executes our instruction.
Once it’s done executing our first statement, it comes back to the
program and reads and executes the second line. It continues to do so
until it reaches the end of the program. That is when our program
is a tradition in the programming field that all new to programming
learn the "Hello World" program as the very first program they write in
a given language. Well, in keeping with that tradition let's write our
first program, entitled "Hello World", shall we?
Type the following line of code into the QB64 IDE:
Make sure to press the ENTER key after typing the line of text in. Did you notice something happen when you pressed the ENTER key? The word print
was automatically capitalized because it is a QB64 command. The IDE
will automatically capitalize certain commands for you so there is no
need for you to do so.
Next, click on the Run menu then click on Start to run, or execute, the program. (Note: this action can also be achieved by pressing F5 or by pressing the ALT key, then the R key, then the S key).
Figure 1 - The result of running our first program, "Hello World"
you have written your first QB64 program! Granted, it doesn't do much
but let's go ahead and review what exactly did happen.
By default QB64 runs, or executes, programs in text mode. The black box that appeared, as shown in Figure 1,
is a text window containing an 80x25 area for text characters to
appear, that is, 25 lines of up to 80 characters (letters, numbers,
etc..) on each line. No graphics are allowed in pure text mode (you'll
be shown later how to create graphic windows). The PRINT command is a QB64 statement, or command, and the characters that follow in quotes, "Hello World", is considered the expression the PRINT statement is to work with. This line of code translates to:
Display the characters Hello World to the current screen window at the current cursor position.
Most QB64 commands, or statements, treat any characters contained within quotes as a literal string and this is the reason the quotes did not get displayed on the screen along with the words Hello World. When a QB64 statement encounters a literal string it works only with the characters contained within the quotes. We'll discuss more about statements, expressions and strings as the course progresses.
When a program has termintated execution, or finished running, QB64 will display "Press and key to continue"
at the bottom of the current text or graphics screen. Pressing any key
at this point will return the programmer back the IDE for further code
editing. Go ahead and press any key now to return to the IDE.
you type in your source code the IDE is always watching what you type.
If you type something in it does not understand it will tell you so at
the bottom the screen. Let's go ahead and purposely create an error to
see this behavior in action. Change the previous line of code we typed
in to this:
Make sure you go to the end of the line and press the ENTER key. Figure 2 below shows what your IDE should look like now:
Figure 2 - A bad Line of Code
is not a QB64 command so the IDE has no idea what the line of code is
supposed to do. The offending line of code will get highlighted in red and the IDE will report the error at the bottom of the screen, Syntax error on line 1. A syntax error
is a mispelling of a command or a malformation of a line of code, such
as forgetting to add a space in between a statement and an expression.
In other words, syntax errors are almost always typos that a programmer
mistakenly typed in and in my opinion are the hardest to track down.
Go ahead and position the cursor between the R and the N in PRNT using your arrow keys and type the letter i to fix the line of code. Press the down arrow key once to leave the line and you'll see that once again the IDE is happy with your code and once again capitalized the word PRINT for you because it's now recognized as a QB64 command.
During the duration of this course you will be making LOTS
of typing mistakes in the IDE. This course is purposely designed to
make you type in much of the code examples by hand instead of providing
them to you. The only way to learn how to write source code is to
actually sit down and write it, over and over again, and make lots of
mistakes in the process. It WILL help you become better programmers ... I promise.
Let's use the IDE to save our very first program to your computer's hard drive. Click on the File menu and then click on Save. Note: you can also press the ALT key, then the F key, then the S key to achieve the same result.
A save dialog box will appear in the IDE as shown in Figure 3 below:
Figure 3 - The Save Dialog Box
by default will be saved in the QB64 folder and during this course we
will always be saving our source code here. In the File Name text field remove the default name of untitled.bas and type in HELLO then press the ENTER key. The file extension of .bas will automatically be added to the file name so you first program has been saved as HELLO.bas. Did you notice how the default name of Untitled in the IDE changed to HELLO.bas? Figure 4 below shows this change:
Figure 4 - Your First Program Saved
Create a program that outputs the following screen as shown in Figure 5 below:
Figure 5 - Write a program that outputs this screen
Hint: The PRINT command on a line by itself will print a blank line and move the cursor down to the next line.
Save the program as IRULE.bas to your computer's hard drive when finished.
-- COMMAND REFERENCE --
New commands introduced in this task:
New concepts introduced in this task: