Beginning Microsoft Small Basic: Chapter 2: Small Basic Program Basics
Small Basic > Small Basic Books > Beginning Microsoft Small Basic** **> 2. Small Basic Program Basics
Review and PreviewIn the first class, we spent all of our time just preparing our computer for creating and running Small Basic programs. In this second class, we will look further into some of the tasks we have done. We will reexamine the Welcome program from Class 1. We will learn some of the basic rules for writing Small Basic programs. We will create and save a program using Small Basic. This will give us the skills needed to create our first Small Basic program in Class 3. The Welcome Program (Revisited)Start SmallBasic and open the Welcome program we looked at in Class 1 Here’s the code you will see in the editor:
|
http://msdn.microsoft.com/hh182234.BeginningSB-med(en-us,MSDN.10).png This chapter is adapted from the book BEGINNING Microsoft Small Basic by Philip Conrod and Lou Tylee. To purchase this book in its entirety, please see the Computer Science For Kids web site. |
The first several lines of the program are:
'' Welcome Program' Beginning Small Basic'
These lines are comments. They simply provide some information about what the program is and provides some contact information. The comment begins with a single apostrophe (‘). These lines are also known as a programheader. It’s a good idea to always put a header on your Small Basic programs to give someone an idea of what your program does and who wrote it. When running a program, Small Basic ignores any comments – their only use is provide explanation.
The first non-comment statement is:
TextWindow.Title = "Welcome Program"
Remember the Welcome program back in Class 1? When you ran the program, you saw this window
http://msdn.microsoft.com/hh125897.image1(en-us,MSDN.10).jpg
Notice the words WelcomeProgram in the title bar of the window. The above line of code displays that title. In this line, TextWindow is an object built into Small Basic – it is the window that displays the output of the program. Small Basic has a number of such objects available for our use. We will use the TextWindow object extensively in our first few programs. Objects have both properties and methods. Properties describe objects, while methods do things to objects. In this single line of code, we are setting the Title property of the TextWindow object to the text string “Welcome Program”. The dot (.) and assignment operator (=) are punctuations that must be placed appropriately for the computer to understand your intent. This line of code literally says “set the Title property of the TextWindow object to Welcome Program.”
The other statement in this short program is:
TextWindow.WriteLine("Welcome to Beginning Small Basic!")
Notice in the text window of the running program, there is a message that says Welcome to Beginning Small Basic! . The above line of code printed that message. This line of code uses the TextWindow WriteLine method to perform the task. We say the text “Welcome to Beginning Small Basic!” is passed to the WriteLine method – the input is placed in parentheses – which then results in the input text being written in the text window.
Though this is a very short, very simple program, it illustrates some major components in a Small Basic program. We want a program header and appropriate comments. Using properties and methods, we can display information using the built-in Small Basic object, the TextWindow.
Some Rules of Small Basic Programming
Let’s look at the Welcome code one more time to point out some basic rules of Small Basic programming. Here’s that code:
'' Welcome Program' Beginning Small Basic' TextWindow.Title = "Welcome Program"TextWindow.WriteLine("Welcome to Beginning Small Basic!")
And, here’s the rules:
Small Basic code requires perfection. All keywords must be spelled correctly. If you type WriteLne instead of WriteLine, a human may know what you mean, but a computer won’t.
Small Basic is not case-sensitive, meaning upper and lower case letters are considered to be the same characters. That means writeline and WriteLine are the same. But, even though Small Basic is not case-sensitive, it is good practice to use accepted case conventions in programming.
Small Basic ignores any “whitespace” such as blanks. We will often use white space to make our code more readable to humans.
To set an object property, we use this ‘dot’ convention:
ObjectName.PropertyName = PropertyValue
where ObjectName is the object, PropertyName the property and PropertyValue the value you want to establish.
To invoke an object method, use this convention:
ObjectName.MethodName(MethodInputs)
where ObjectName is the object, MethodName the method and MethodInputs the inputs needed by the method.
We’ll learn a lot more Small Basic programming rules as we progress.
Creating Small Basic Programs
In Class 3, we will begin learning the Small Basic language and start writing our own Small Basic programs. In preparation for this, you’ll need to know how to create a new program with Small Basic. Let’s do that now. What we’ll do is re-create the Welcome program.
If it’s not already running, start Small Basic. Click the NewProgram button in the toolbar:
http://msdn.microsoft.com/hh125897.image2(en-us,MSDN.10).jpg
An empty editor will appear:
http://msdn.microsoft.com/hh125897.editor(en-us,MSDN.10).jpg
Click in this window and start typing in the code for the Welcome program.
Type one line at a time, paying close attention that you type everything as shown (pay attention to the rules seen earlier). After each line, press the <Enter> key. Here, again, is the code.
'' Welcome Program' Beginning Small Basic' TextWindow.Title = "Welcome Program"TextWindow.WriteLine("Welcome to Beginning Small Basic!")
After typing the four comment lines and starting to type the first line of code you will notice this popup appears:
http://msdn.microsoft.com/hh125897.TextWindow(en-us,MSDN.10).jpg
Small Basic has a feature called “intellisense” that helps you type your programs faster. When this list appears, you move through the list using the up/down arrow keys and make a selection by pressing <Enter>. It will appear for object names, properties and methods. Give it a try!
Also notice as soon as you type TextWindow, this appears in the help area of the Small Basic environment:
http://msdn.microsoft.com/hh125897.image5(en-us,MSDN.10).jpg
Small Basic provides “context-sensitive” help. The help area will always display information it deems is important to the user at the appropriate time. In this case, information concerning the properties (marked by painter’s palette icon) and methods (marked by gear icon) for the TextWindow object are displayed. And, once you select a property or method, a help description for that selection appears. For example, once you type Title, you will see this help screen describing the property and how its used:
http://msdn.microsoft.com/hh125897.title(en-us,MSDN.10).jpg
With intellisense and context-sensitive help, you always have on-line information to help you with your programming tasks.
Notice these editing buttons in the toolbar:
http://msdn.microsoft.com/hh125897.image7(en-us,MSDN.10).jpg
If you’ve ever used a word processor, these tasks are familiar to you. When typing code, you can Cut, Copy and Paste. And you can Undo and Redo tasks. These tasks make typing code (especially long programs) much easier in the Small Basic environment. Another thing to notice is that the editor uses different colors for different things in the code. Comments, objects, method names and data used by objects are all colored differently. This coloring sometimes helps you identify mistakes you may have made in typing.
When done typing, you should see:
http://msdn.microsoft.com/hh125897.program(en-us,MSDN.10).jpg
Try running your program. Use the toolbar run program button (or press <F5>). You should once again see the Welcome to Beginning Small Basic! Message:
http://msdn.microsoft.com/hh125897.WelcomPro(en-us,MSDN.10).jpg
You should also see that it’s really kind of easy to get a Small Basic program up and running.
Saving Small Basic Programs
Before leaving Small Basic, we need to discuss how to save programs we create. Each program should be saved in its own folder. You decide where you want to store this folder. There are two buttons on the toolbar used to save programs. To save a new program, click the SaveProgram button:
http://msdn.microsoft.com/hh125897.image10(en-us,MSDN.10).jpg
A dialog box will appear:
http://msdn.microsoft.com/hh125897.image11(en-us,MSDN.10).jpg
Create the folder you want to save the program in. In this example, I have created a folder named Welcome in a MySmallBasic folder.
Select a name for your program. Here I have selected Welcome:
http://msdn.microsoft.com/hh125897.image12(en-us,MSDN.10).jpg
Click Save and the program is saved. From this point on, whenever you reopen this program and make modifications, if you click the Save toolbar button, the program will be automatically saved with the same name in the same folder. We suggest you do this occasionally while modifying a program.
If you wish to assign a different name to or create a different folder for the modified program, use the Save As toolbar button:
http://msdn.microsoft.com/hh125897.image13(en-us,MSDN.10).jpg
Use the resulting dialog boxes to name and locate your program.
If you try to exit Small Basic and have not saved programs, Small Basic will pop up a dialog box to inform you of such and give you an opportunity to save files before exiting. An example dialog is:
http://msdn.microsoft.com/hh125897.image14(en-us,MSDN.10).jpg
Make the appropriate choice. Click “Yes” to save your program if you want to keep the changes you have made to it.
Small Basic Files
When you save a Small Basic program in a particular folder, files other than the file listing your code are saved. These files are needed by the Small Basic environment to keep track of things.
Using My Computer or Windows Explorer in Windows, go to the folder containing the Welcome program you just built and ran. You should see the following files:
http://msdn.microsoft.com/hh125897.image15(en-us,MSDN.10).jpg
The file named Welcome with type Small Basic Program is the source code that appears in the editor of Small Basic. The Welcome file marked Application is a ‘compiled’ version of the code and is the ‘executable’ code. If you double-click this file, the Welcome program will run independent of the Small Basic environment. Try it if you like. Later we will learn how to use this file to let your friends run your programs on their computers or even on the Internet! The Welcome.pdb file is a database file with information needed by your program and lastly, SmallBasicLibrary.dll is called a run-time library. It contains files that help your program run.
We describe these files so you are aware of their presence. Do not modify or delete any of these files outside of the Small Basic environment.
Summary
After all the downloading and installing done in the first class, this second class must have seemed like a breeze. In this class, we looked at several important concepts that will let us start building our own Small Basic programs.
In this class, we studied the structure of a program, knowing it is built using objects, properties and methods. We learned how to use Small Basic to create and run a new program. We looked briefly at some of the rules used in writing Small Basic code and we saw how to save a program. In the next class, we finally get started learning the Small Basic language. And, we’ll write and run our first Small Basic program.
Excerpt © Copyright 2010-2013 By Kidware Software LLC All Rights Reserved. Computer Science For Kids, the Computer Science For Kids logo, and related trade dress are trademarks or registered trademarks of Kidware Software LLC. Philip Conrod & Lou Tylee have co-authored dozens of books and tutorials for beginning Microsoft Basic, Small Basic, Visual Basic, and Visual C# developers of all ages for over 25 years.