Day 2 Welcome back. Okay, maybe a few of you breezed through that first day, and this day won't be all that hard, either. This session will concentrate on basic programming, the QBASIC IDE (integrated developer's environment), and data manipulation. Basic programming: Yesterday we made the following program: CLS INPUT "Who are you?", name$ PRINT "Hello, " name$ ", now go away you're bothering me" END Which produced the following output: Who are you? Kalvin Hello, Kalvin, now go away you're bothering me." Press any key to continue. . . That program, and every other program, can be boiled down to three concepts. INPUT DATA MANIPULATION OUTPUT Whether you are writing a simple "Hello World" program or a word processor, the basic ideas are the same. You get input from the user, the program itself, a disk or a file, you manipulate that data into something useful, and you publish it in a pleasing format, whether the output is to the screen, to a printer, to a disk, or even onto the net. The same concepts apply. In order to achieve this ideal, there are certain ideas that must be met first. One of these ideals is to plan out your program before you write it, and that includes thinking about what you are doing. The Hello World is not an ideal program to explain this concept; most programmers could do this in their sleep. With other programs, you should think about is input, manipulation, and output. For instance, let's say that we had to carpet our floor. The carpet costs $6 a square foot, and therefore we should know about how much area our floor occupies. So, the thought should be like this: What do we want to know? How much area our floor occupies and how much will the carpet be. What do we know? We know the length and we know the width (you may not, but bear with me). What do we want? A screen message saying how many square feet our floor is and how much the carpet should cost. Breaking this down into the three concepts of programming we know: INPUT: length and width DATA MANIPULATION: multiply length by width to get area. Multiply area by $6 to get the total price. OUTPUT: area and cost later programs will take the same course. We'll get back to this program later on. The QBASIC IDE IDE stands for Integrated Developer's Environment, and it is to the programmer what a word processor is to a writer. While many languages can be written in text editors, an IDE gives the programmer added advantages of syntax checking (whether commands are used properly), slowing down the program and even stopping it at certain points to see what's going on, and compiling the program (or, in our case, translating it). The QBASIC IDE is a text editor mostly, but still has certain functions. QBASIC comes on your computer if you are a user of Win 3.x, DOS, or Windows 95. On Windows 95, you have to usually search for it on your CD-ROM, under the directory old/msdos. I will get a copy of it on this web page later on. To run it in DOS, enter the directory it is located and type QBASIC. In Win 95 or Win 3.x, double click on it or create a shortcut to it on your screen. QBASIC looks like any run-of-the-mill program. Some of the important menu items are: File New Opens a new program. Open Opens an existing program Save Saves the program Save As Saves the current program under an existing name. Print Prints the program source code (the commands that run the program). Exit Exits the program Edit Cut/Copy/Paste commands for copying or cutting a segment of the program and placing it somewhere else New SUB New FUNCTION These commands open up new subroutines or functions in the program. We'll talk about Them more later. Run Run Runs the program Debug Step Executes the program, one command at a time. Useful when you want to see what is going on in the program. Set Breakpoints Breakpoints stop the program in mid-step. Often useful for diagnosting programs. In QuickBasic, there are a few more commands, such as make EXE and so on, but both editors run much the same. The help menus are useful for understanding both the program you are working in and the keywords. Take some time to look at it. REM(" ' "), LET, and data manipulation The next good habit you should get into is commenting your code. Comments are put in to explain what you are doing in your code. To put in a comment you use the following syntax REM REM tells the interpreter to forget everything that is on that line. The line is still there, but it is only meant to be read by the human, everything else is ignored. A shorthand version of this uses the apostraphe mark. ' Comments are ignored by the computer, but not by other programmers Comments should be put in to explain the code. They shouldn't be there to restate the obvious. Here's an example of some commented code ' Program area.bas ' Created by John Meyer ' Tuesday, August 18, 1998 ' Data Directory of variables ' length=Length of room ' wdth= width of room (WIDTH is a reserved keyword in QBASIC) ' area ' price=Price of wallpaper (constant) ' totalcost= price * area '************************************************Input Data****************************** CLS INPUT "How long is the room? ", length INPUT "How wide is the room? ", wdth '***************************************************Calculate data************************ area = length * wdth price = 6 totalcost = price * area '**************************************Output Data******************* PRINT "A room "; length; " long and " wdth; " wide is "; area; " square units and costs $"; totalcost; " to carpet." END Notice a few things I did. The first is to set up a data directory of variables. Unlike other languages (notably C and Pascal), BASIC doesn't require you to declare your variables before you use them. However, doing this is good practice, even if you only declare them in the comments section, where the computer won't see them. Data directories can contain as many as 500 variables and more. Secondly, there are the long lines of asterix before each section of the code, commented out so the interpreter doesn't see them.. This is to break it up for your benefit and show you, again, the three areas of a program. A lot of students put these before subroutines to show them what each one is for. LET In the above program, you saw the following statements area = length * wdth price = 6 totalcost = price * area These statements are assignment statements. What they tell the computer to do is to calculate what's on the right hand and stuff it into the left hand. It's actually a shorthand of the LET statment. These could just as easily be written this way. LET area = length * wdth LET price = 6 LET totalcost = price * area LET is really not necessary, but it shows the syntax of the command LET = Few commands are used more than LET, and with good reason. There are a few rules to LET. The first is what is assigned to what. For instance, this is legal: let name$ = "John Meyer" Even: let name$ = "64" Is legal, although it doesn't exactly do what you think it will. However, this is illegal: let area = "John Meyer" The reason: "John Meyer" is a string constant, and area is a numeric variable. You cannot assign strings to numeric variables, although you can do the opposite.