p. 1
microsoft small basic an introduction to programming
[close]
p. 2
chapter 1 an introduction small basic and programming computer programming is defined as the process of creating computer software using programming languages just like we speak and understand english or spanish or french computers can understand programs written in certain languages these are called programming languages in the beginning there were just a few programming languages and they were really easy to learn and comprehend but as computers and software became more and more sophisticated programming languages evolved fast gathering more complex concepts along the way as a result most modern programming languages and their concepts are pretty challenging to grasp by a beginner this fact has started discouraging people from learning or attempting computer programming small basic is a programming language that is designed to make programming extremely easy approachable and fun for beginners small basic s intention is to bring down the barrier and serve as a stepping stone to the amazing world of computer programming the small basic environment let us start with a quick introduction to the small basic environment when you first launch smallbasic you will see a window that looks like the following figure.
[close]
p. 3
figure 1 the small basic environment this is the small basic environment where we ll write and run our small basic programs this environment has several distinct elements which are identified by numbers the editor identified by [1 is where we will write our small basic programs when you open a sample program or a previously saved program it will show up on this editor you can then modify it and save if for later use you can also open and work with more than one program at one time each program you are working with will be displayed in a separate editor the editor that contains the program you are currently working with is called the active editor the toolbar identified by [2 is used to issue commands either to the active editor or the environment we ll learn about the various commands in the toolbar as we go the surface identified by [3 is the place where all the editor windows go our first program now that you are familiar with the small basic environment we will go ahead and start programming in it like we just noted above the editor is the place where we write our programs so let s go ahead and type the following line in the editor textwindow.writeline hello world
[close]
p. 4
this is our first small basic program and if you have typed it correctly you should see something similar to the figure below figure 2 first program now that we have typed our new program let s go ahead and run it to see what happens we can run our program either by clicking on the run button on the toolbar or by using the shortcut key f5 on the keyboard if everything goes well our program should run with the result as shown below figure 3 first program output congratulations you have just written and run the first small basic program a very small and simple program but nevertheless a big step towards becoming a real computer programmer now there s just one more detail to cover before we go on to create bigger programs we have to understand what just happened what exactly did we tell the computer and how did the computer know what to do in the next chapter we ll analyze the program we just wrote so we can gain that understanding as you typed your first program you might have noticed that a popup appeared with a list of items figure 4 this is called intellisense and it helps you type your program faster you can traverse that list by pressing the up/down arrow keys and when you find something you want you can hit the enter key to insert the selected item in your program.
[close]
p. 5
figure 4 intellisense saving our program if you want to close small basic and come back later to work on the program you just typed you can save the program it is in fact a good practice to save programs from time to time so that you don t lose information in the event of an accidental shutdown or a power failure you can save the current program by either clicking on the save icon on the toolbar or by using the shortcut ctrl+s press the s key while holding down the ctrl key
[close]
p. 6
chapter 2 understanding our first program what really is a computer program a 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 small basic is one imagine 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 and programs are basically sets of sentences sometimes just a few and sometimes many thousands that together make sense to both the there are many languages that the computer programmer and the computer alike can understand java c python vb etc are all powerful modern computer languages that small basic programs are used to develop simple to complex software a typical small basic program consists of a bunch programs 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 small basic 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 finishes.
[close]
p. 7
back to our first program here is the first program we wrote textwindow.writeline hello world this is a very simple program that consists of one statement that statement tells the computer to write a line of text which is hello world into the text window it literally translates in the computer s mind to write hello world you might have already noticed that the statement can in turn be split into smaller segments much like sentences can be split into words in the first statement we have 3 distinct segments a textwindow b writeline c hello world the dot parentheses and the quotes are all punctuations that have to be placed at appropriate positions in the statement for the computer to understand our intent you might remember the black window that appeared when we ran our first program that black window is called the textwindow or sometimes referred to as the console that is where the result of this program goes textwindow in our program is called an object there are a number of such objects available for us to use in our programs we can perform several different operations on these objects we ve already used thewriteline operation in our program you might also have noticed that the writeline operation is followed by hello world inside quotes this text is passed as input punctuations such as quotes spaces and to the writeline operation which it then prints parenthesis are very important in a computer out to the user this is called an input to the program based on their position and count operation some operations take one or more they can change the meaning of what is being inputs while others don t take any expressed our second program now that you have understood our first program let s go ahead and make it fancier by adding some colors textwindow.foregroundcolor yellow textwindow.writeline hello world
[close]
p. 8
figure 5 adding colors when you run the above program you ll notice that it prints out the same hello world phrase inside textwindow but this time it prints it out in yellow instead of the gray that it did earlier figure 6 hello world in yellow notice the new statement we added to our original program it uses a new word foregroundcolor which we equated to a value of yellow this means we ve assigned yellow to foregroundcolor now the difference between foregroundcolor and the operation writeline is that foregroundcolor did not take any inputs nor did it need any parenthesis instead it was followed by an equals to symbol and a word we define foregroundcolor as a property of textwindow here is a list of values that are valid for the foregroundcolor property try replacing yellow with one of these and see the results don t forget the quotes they are required punctuations black blue cyan gray green magenta red white
[close]
p. 9
yellow darkblue darkcyan darkgray darkgreen darkmagenta darkred darkyellow
[close]
p. 10
chapter 3 introducing variables using variables in our program wouldn t it be nice if our program can actually say hello with the users name instead of saying the generic hello world in order to do that we must first ask the user for his/her name and then store it somewhere and then print out hello with the user s name let s see how we can do that textwindow.write enter your name name textwindow.read textwindow.writeline hello name when you type and execute this program you ll see an output like the following figure 7 ask the user s name and when you type in your name and hit enter you ll see the following output figure 8 a warm hello
[close]
p. 11
now if you run the program again you ll be asked the same question again you can type in a different name and the computer will say hello with that name analysis of the program in the program you just ran the line that might have caught your attention is this name textwindow.read read looks just like writeline but with no inputs it is an operation and basically it tells the computer to wait for the user to type in something and hit the enter key once the user hits the enter key it takes what the user has typed and returns it to the program the interesting point is that whatever the user had typed is now stored in a variable called name a variable is defined as a place where you can store values temporarily and use them later in the line above name was used to store the name of the user the next line is also interesting textwindow.writeline hello name this is the place where we use the value stored in our variable name we take the value in name and append it to hello and write it to the textwindow once a variable is set you can reuse it any number of times for example you can do the following write just like writeline is another operation on consolewindow write allows you to write something to the consolewindow but allows succeeding text to be on the same line as the current text textwindow.write enter your name name textwindow.read textwindow.write hello name textwindow.writeline how are you doing name and you ll see the following output:
[close]
p. 12
figure 9 reusing a variable rules for naming variables variables have names associated with them and that s how you identify them there are certain simple rules and some really good guidelines for naming these variables they are 1 the name should start with a letter and should not collide with any of the keywords like if for then etc 2 a name can contain any combination of letters digits and underscores 3 it is useful to name variables meaningfully since variables can be as long as you want use variable names to describe their intent playing with numbers we ve just seen how you can use variables to store the name of the user in the next few programs we ll see how we can store and manipulate numbers in variables let s start with a really simple program number1 10 number2 20 number3 number1 number2 textwindow.writelinenumber3 when you run this program you ll get the following as output figure 10 adding two numbers in the first line of the program you re assigning the variable number1 with a value of 10 and in the second line you re assigning the variable number2 with a value of 20 in the third line you re adding number1 and number2 and then notice that the numbers don t have quotes around them for numbers quotes are not necessary you need quotes only when you re using text.
[close]
p. 13
assigning the result of that to number3 so in this case number3 will have a value of 30 and that is what we printed out to the textwindow now let s modify that program slightly and see the results number1 10 number2 20 number3 number1 number2 textwindow.writelinenumber3 the program above will multiply number1 with number2 and store the result in number3 and you can see in the result of that program below figure 11 multiplying two numbers similarly you can subtract or divide numbers here is the subtraction number3 number1 number2 and the symbol for division is the progam will look like number3 number1 number2 and the result of this division would be figure 12 dividing two numbers a simple temperature converter for the next program we ll use the formula temperatures to convert fahrenheit temperatures to celsius
[close]
p. 14
first we ll get the temperature in fahrenheit from the user and store it in a variable there s a special operation that lets us read numbers from the user and that is textwindow.readnumber textwindow.write enter temperature in fahrenheit fahr textwindow.readnumber once we have the fahrenheit temperature stored in a variable we can convert it to celsius like this celsius 5 fahr 32 9 the parentheses tell the computer to calculate the fahr 32 part first and then process the rest now all we have to do is print the result out to the user putting it all together we get this program textwindow.write enter temperature in fahrenheit fahr textwindow.readnumber celsius 5 fahr 32 9 textwindow.writeline temperature in celsius is celsius and the result of this program would be figure 13 temperature conversion
[close]
p. 15
chapter 4 conditions and branching going back to our first program wouldn t it be cool that instead of saying the general hello world we could say good morning world or good evening world depending on the time of the day for our next program we ll make the computer say good morning world if the time is earlier than 12pm and good evening if the time is later than 12pm if clock.hour 12 then textwindow.writeline good morning world endif if clock.hour 12 then textwindow.writeline good evening world endif depending on when you run the program you ll see either of the following outputs figure 14 good morning world
[close]