Lesson 1: Your First Program

A computer program is a set of step-by-step instructions for the computer to follow. A computer program can be imagined in terms of creating cookies from a recipe. To create the perfect cookie you need the correct ingredients and a recipe to follow. The recipe guides the cook through the process of mixing the ingredients in just the right proportion and then baking to perfection.

A computer program is the computer's recipe that guides the computer through the process of executing statements (the ingredients) in just the right order to achieve the desired outcome. Someone making cookies for the very first time may not produce that perfect cookie. It takes time, patience, and practice to produce that perfect cookie. Likewise someone learning to code needs to understand that it will also take time, patience, and practice to become proficient at coding.

Don't give up! If you feel you have come to a point where a concept is eluding you then visit the QB64 forum and ask for assistance. There are MANY great and wonderful people at the forum more than willing to help you on your journey. Keep in mind that every new programmer at one time was sitting in the same position you are right now ... staring at a blank IDE wondering what to do next.

A typical program consists of statements on separate lines. Every line of the program represents a statement that is an instruction for the computer to follow. The first line of code is read and the statement is executed. When the computer has finished with the first line it moves onto the second, then the third, and so on. As the computer encounters each statement it interprets what the statement wishes to have done and executes that command. This continues until the last line of code has been read and executed at which point the program ends.

Your First Program

No matter which programming language course you decide on the first program a person is asked to code is always the obligatory Hello World program. In keeping with tradition we will do the same. With the IDE open and ready to go type in the following line of code:

print "Hello World"

Press the ENTER key after you have typed the line of code in to advance the cursor to the next line.

Figure 1 below shows the line of code in the IDE.

Figure 1: Your first program

Did you notice something after you pressed the ENTER key? The key word print was automatically capitalized because it is a recognized QB64 command. The IDE will automatically capitalize QB64 command key words for you. Also notice that the IDE color coded the line of code. The key word PRINT is blue while "Hello World" is in orange. The IDE will color code command key words in blue and anything contained in quotes ("") in orange. This makes it easy to distinguish different elements of the program quickly.

The word Untitled where the name of our program appears now contains an asterisk (*) next to it. The asterisk is a visual indicator informing the programmer that the code has changed but has not been saved.

It's now time to run, or execute, the first program you have written. Select Run in the menu and then select Start in the drop down menu. (As a shortcut you can simply press the F5 key on your keyboard as well). Figure 2 below shows what the output of your program should look like.

Figure 2: The output of your first program

By default QB64 opens a pure text window (known as SCREEN 0) and runs programs in text mode. Figure 2 above shows the output of your program in an 80 character wide by 25 line high text screen. So what did your line of code actually do?

The PRINT command is a QB64 statement, or command, that displays anything on the line after it, called expressions, to the output screen. "Hello World" is the expression that the PRINT statement was told to work with. Expressions contained in quotation marks ("") are called literal strings. When the PRINT command encounters a literal string it takes the characters inside the quotes and displays them at the current cursor position on the screen. The quotation marks of a literal string will not get printed to the screen because the quotation marks identify the literal string to the PRINT command. We'll discuss more about statements, expressions, and strings as the course progresses. So, our line of code translates in English to this:

Display the characters in the literal string Hello World to the default output screen at the current cursor position.

When a QB64 program has terminated execution, or finished running, a line at the bottom of the screen will instruct the programmer to "Press any key to continue". This action allows the programmer to see the output of the screen before the screen is closed and control is given back to the IDE or operating system.

The IDE to the Rescue!

The IDE is always watching as you type programs in. If you happen to type a line of code in that the IDE does not understand you will be informed of the error. Let's go ahead and see this in action. Go back into your IDE and remove the letter I from the PRINT command and then arrow down to the next line. Figure 3 below shows what you should be seeing in your IDE now.

Figure 3: An error has been detected

Since PRNT is not a recognized QB64 statement, or command, the IDE flags the line as having an error and highlights it in red. Also, an explanation of the error is shown in the status window at the bottom of the IDE offering the line number of the error along with a brief description of the type of error that has occurred.

Go ahead and replace the I you removed earlier to make the PRINT statement correct again. You'll see the IDE is now happy once again.

You are going to make LOTS of typing errors while you enter code into the IDE. This course is purposely designed to make you type in many of the shorter examples by hand so you get used to the "feel" of typing code into an IDE. 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. This WILL make you a better programmer in the long run ... I promise.

False Errors

Every once in while (not common at all) you'll get a "C++ Compilation Error" show up in the error information area when attempting to run a program. 99.9% of the time this is a false error. Simply press the SPACEBAR and then the BACKSPACE key anywhere within your source code and try to run the program again. If you continue to receive this error then it's a legitimate error and not false. I have no idea why this happens from time to time but you need to be aware of this bug. I've noticed this false error usually only shows up for me when programs start getting large, perhaps over 250 lines of code.

Save Your Work!

Let's use the IDE to save your very first program to your computer's hard drive. Enter the File menu and then choose Save from the drop-down menu. Since this is the first time the program is being saved the "Save As" dialog window appears. Figure 4 below shows the Save As window.

Figure 4: Saving the program

Your programs by default will be saved to the QB64 folder on your desktop. In the File Name: text field type in HelloWorld (no space) and then press the ENTER key. The file extension of .BAS  will automatically be added to the end of your program's source code making the full file name HelloWorld.BAS when seen on your hard drive. Also notice that the name of the program is now displayed in your IDE at the top, where Untitled was once listed. Figure 5 below shows how this changed.

Figure 5: The program name now displayed

Your Turn

Create a program that outputs the screen as shown in Figure 6 below.

Figure 6: Write a program that outputs this screen

Hint: The PRINT statement on a line by itself will print a blank line.

Save the program as IRULE.BAS to your computer's hard drive when finished.

Don't cheat! Only view the tutorial solutions if you have succeeded in the goal or are genuinely stuck finding the solution.

Click here to see the solution.

Commands and Concepts Learned

New commands introduced in this lesson:


New concepts introduced in this lesson:

literal string
syntax error