If you get an error message, press ESC to clear the message, then retype the line at the cursor. BAS, then write and run some code: 1.
If you typed everything correctly, QuickBASIC displays the program's output on the "output screen," which should look like the screen in Figure 3. Figure 3. You can restore the windows to their previous sizes by pressing this toggle again.
The following steps illustrate this and prepare the windows for the exercise in the next section: 1. This adds lines to the View window and compresses the Immediate window beneath it. This decreases the number of lines in the View window while increasing the size of the Immediate window. Only one window can be active at a time. However, other windows are available. Note that the title bar of the active window is highlighted. Moving between Windows To move the cursor down one window, press the F6 key.
Executing Code in the Immediate Window The Immediate window at the bottom of the screen can be used as a utility window as you program in the View window. For example, if you want to clear the output screen, you can execute the CLS statement from the Immediate window. You can also use the Immediate window for doing calculations, or experimenting with programming ideas and previewing the results.
When you run lines of code in the View window, they are executed sequentially. In the Immediate window, a single line of code is executed whenever you put the cursor anywhere on that line, then press ENTER. You can type in either uppercase or lowercase in the Immediate window.
Try the following procedure to execute lines of code in the Immediate window: 1. Press F6 to place the cursor in the Immediate window. Separating statements with a colon allows you to put more than one statement on a line. The cursor is placed on the next blank line in the Immediate window.
The quotation marks have no space between them, so the string has a length of zero. Press a key to return to the environment. You can write up to 10 lines of statements in the Immediate window. If you type an eleventh line, the first line you typed is lost; if you type a twelfth, the second line you typed is lost, and so on. Section Monitoring Variables with the Watch Window When a program doesn't behave the way you expect, it is often because a variable is acquiring a value you did not anticipate.
The Watch window lets you monitor the values of expressions while your program is running, so you can be sure their values are within the expected range.
Try the following to see how the Watch window works: 1. Press F6 to move the cursor to the Immediate window if it is not already there. The Watch window opens at the top of the screen. The cursor returns to the Immediate window. With the Watch window you can monitor variable values as they change in the running program without having to use PRINT statements to display the value on the output screen. The Help Window Most on-line help appears in the Help window. You can use the Help menu or press F1 while an item on screen or a menu or command contains the cursor or is highlighted.
The following steps illustrate the use of context-sensitive help: 1. Press ESC to close the Help menu if it is open. Hyperlinks At the top of the screen, several bracketed items, called "hyperlinks," indicate related topics you can investigate.
Hyperlinks provide instant connections between related topics. At the bottom of the screen, the other uses of the keyword are also bracketed as hyperlinks.
If you want more information on one of them, use the TAB key to move the cursor to the hyperlink, then press F1, as explained here. Press F6 until the cursor appears into the Help window. Press TAB to move the cursor to the Example hyperlink. Press F1 to view the Example screen see Figure 3. Repeat steps 1 and 2 to view the information available through the other bracketed hyperlinks. You can use several successive hyperlinks without returning to the previous screen, QuickBASIC keeps track of the last 20 hyperlinks.
You can press ESC any time to clear the Help window completely, even if you are several levels down in hyperlink screens. For a reminder on which keys or key combinations to use for moving in or among Help screens, choose Contents from the Help menu, then choose the "Help keys" hyperlink. Even some items that are not bracketed are hyperlinks. A dialog box appears asking if you want to save the unsaved material in the View window. Type program1 in the text box. The focus is on the fundamentals, so you can start programming quickly.
If you have never programmed before, you should read this chapter carefully. You may want to read some intermediate-level books on BASIC before tackling advanced programming techniques in the following chapters. You can enter multi-line examples in the View window.
Study each example to understand why it works the way it does. Be sure you grasp the material in one section before going on to the next. A "program" is a sequence of instructions to the computer, in a language both you and the computer understand.
Each instruction is called a "statement" and usually takes up one line in the program. The goal of any program is to perform some useful job, such as word processing, bookkeeping, or playing a game. Programming is the process of deciding what you want the program to do. Then you select and arrange language statements to accomplish the individual tasks needed to reach the overall goal. This process is part science, part art. The science comes from books; the art is learned by writing your own programs and analyzing programs written by others.
Comments Comments are an important part of programming. They explain the program to others, and remind you why you wrote it the way you did. Comments begin with an apostrophe, and can extend to the end of the line, as follows: ' This is a comment. These and most other programs communicate with the user by displaying words, numbers, and data on the screen. A "string" is any group of characters letters, numbers, punctuation marks enclosed in quotes.
The examples in this chapter are shown the way QuickBASIC formats them, and each program's output is shown exactly as it appears on the screen. Variables Programs take data from the outside world input , then process it to produce useful information output. The input and output data are stored in the computer's memory. Variable names are used to keep track of where the data are stored. Storing Data in Memory Computer memory is divided into storage locations, each of which has a unique "address.
Every piece of data has its own storage location and matching address see Figure 4. BASIC uses a "variable name" to associate a word of your choice with a memory location in the computer. BASIC does the associating automatically. You can then use the variable name without having to know its address. The following example program demonstrates this. Note that Hello, world is not enclosed in quotes in this example.
They aren't strings, because they aren't enclosed in quotes. They are definitely not numbers. Therefore, Hello and world must be variables. A variable name can contain digits, but the name cannot begin with a digit. Any word starting with a digit is interpreted as a number.
Programming languages, therefore, need different types of variables to hold different types of data. BASIC has three categories of variables: integer, floating point, and string. Integer Variables An integer number is a whole number, without a fractional part. For example, 0, , and are integers. Integer numbers are stored in integer variables.
BASIC has two types of integer variables: integer and long integer. Integer variables can have any whole-number value between , and 32, Floating-Point Variables A floating-point number can have a fractional part though it doesn't have to. For example, 0. Floating-point numbers are stored in floating-point variables. BASIC has two types of floating-point variables: single precision and double precision. A variable name ending in a letter, a digit, or!
Single-precision floating-point numbers can have values ranging from about A single-precision floating-point variable is accurate to only about 7 decimal places.
A variable name ending in is a double-precision floating-point variable and can have values from about Single-precision floating-point numbers are the kind most often used in calculations, since "real-world" applications usually need numbers with fractional parts. Prices are a good example. Note, however, that calculations with integer variables take much less computer time than those performed with floating-point variables.
Integer variables should be used when speed is required. The numerical variables in the remaining examples in this chapter are marked with the appropriate type suffix, either! Although most of these are single-precision floating-point variables, which need no type suffix, they are given the!
String Variables A string variable holds a sequence a string of characters. Just as a numeric variable is given an initial value of 0, a newly defined string variable is assigned an initial length of zero.
It contains no data at all, not even blank spaces. Assigning Values to Variables A variable gets its value through an "assignment" statement. The result is assigned to a third string. The output should look like the following: Hello, there! Although assignment is shown by the equal sign, it is not the same thing as algebraic equality.
To BASIC, the assignment statement means: "Perform all the calculations and data manipulations on the right side of the equal sign. Then give the variable on the left side the result. This occurs most often when you want to change the variable's value. Try the following example program: 1. Choose the File menu's New Program command to clear the View window. Choose the Run menu's Start command. The output should look like the following: 6 5 4. You cannot assign numeric values to string variables or vice versa.
Therefore, the following statements are illegal. Either one halts execution and causes the error message Type mismatch when the program runs. Exponentiation raising a number to a power is shown by ' the caret. Integer division retains the integer whole-number part of the division and discards the fractional part. The number 9. Therefore, the result of the second example above is 5, not 4. The remainder operator is the complement of integer division.
The whole-number part of the division is discarded, and the remainder is returned. The following example program illustrates algebraic precedence: 1.
Parentheses can confirm or override the normal precedence. Try this example: 1. The result is the same as in the last example because the precedence of operations dictated by the parentheses is no different from the usual order. However, the next example program produces a different result: 1. The result here is different because the calculations within parentheses are performed first. Within parentheses, the usual precedence still applies, of course.
You might want to use parentheses to control the order of calculations, rather than depending on precedence. Parentheses prevent errors and make your programs easier to understand. Math Functions Along with the addition, subtraction, multiplication, and division operators just presented, BASIC provides a number of common math functions, described briefly below.
To use them, follow the function name with the variable or expression you want the function performed on in parentheses. The expression may be any valid combination of variables, numbers, and math functions. Expressions are explained further in the following section. Type the following example in the View window: adjacent! The output should look like the following: the hypotenuse is: 5 4.
Expressions An "expression" is what most people would call a formula. An expression is any combination of numbers, strings, variables, functions, and operators that can be evaluated. It evaluates to four. Enter the following statements in the View window to assign a variety of simple expressions to variables, as follows: 1. You cannot add a number or numerical variable to a string or string variable.
Expressions may be as complex as you like, with the results of one expression passed to a function, the value returned by a function combined with other expressions, and so on.
Almost any place you can use a number or a string, you can use any expression no matter how complex that evaluates to a number or a string. For example, try this: 1. The PRINT statement is quite flexible; it can display any combination of strings, numbers, variables, and expressions. The items to be displayed are separated by semicolons ; or commas ,. When a semicolon separates two items, the second item is displayed immediately after the first, as shown in the example program below: 1.
Here's another example program to try: 1. The output should look like the following: The sum of 36 and 4 is 40 The square of 36 is 4. If two items in a PRINT statement are separated by a comma, the second item is positioned at the next preset tab location. The tab stops are 14 columns apart. Note the difference between the output of the following PRINT statement and that of the similar one with semicolons, above: 1.
Commas and semicolons can be used in the same PRINT statement to position the items any way you like. Each PRINT statement normally places its output on a new line, as the following example program shows: 1.
Change the last example program as follows: 1. The output should look like the following: How old are you? The value entered in response to the prompt is assigned to the variable specified in the INPUT statement. When you type 42, you are not typing the number 42, but rather the characters 4 and 2.
If the input variable is a string, the character sequence 42 is simply assigned to the string. A numeric variable, however, can only be assigned a number. If you type dog when you should have entered a number, BASIC responds with the message Redo from start and a new prompt line. Arrays of Variables If you wanted to keep track of the high temperature for each day in July, you could use 31 floating-point variables, named july1, july2, july3, and so on through july However, a group of related but individually named variables is clumsy to work with; each variable has to be handled separately.
For example, if you wanted to prompt the user for each temperature in July, you would have to write 31 individual prompt statements. It would be much more convenient if you could just give the computer the date the number 4 and the computer would select the right variable and value.
The solution is to give the entire set of values the same name july in our example and distinguish the individual values by a numerical index called a "subscript. Arrays can be especially powerful when used in conjunction with loops see the section titled "Repeating Program Operations".
A DIM statement must appear before the array variable can be used. The value given in the DIM statement specifies the maximum value for the variable's subscript. In this way the program can connect a specific variable with a number entered by the user.
The following code fragment prompts the user for the date, then prints the high temperature for that day in July assuming that all the temperatures have been assigned to the array july. On the basis of such a decision, an appropriate group of program statements can be executed or repeated.
An income-tax program can make this decision and execute either the part of the program that performs itemized deductions or the part that takes the standard deduction. The user doesn't have to tell the program what to do. Relational Operators Numbers and strings are compared using "relational operators. BASIC knows which usage is intended from the context.
But if apples and oranges were variable names, then the truth or falsity of the statement would depend on the values of these variables. The following relational operations compare strings. Are they true or false? Y and Y are the same character, so the second assertion is true. Y and y are not the same character, so the third assertion is false. The last statement may be confusing at first; how can one string be "larger" or "smaller" than another? It can be, and the decision is made this way.
In this example, the fourth characters are the first nonmatching characters. The decision of "larger" or "smaller" is then based on these differing characters, in the following way. Within the computer, characters are represented by numbers. Letters coming later in the alphabet have numeric values larger than those appearing earlier.
Since t follows s in the alphabet, t is a "larger" letter, and the statement is therefore false; misty is "larger" than miss. This ability to determine the relative value of strings makes it easy to write a program that can alphabetize a list of words or names.
In most of these examples, numbers were compared with numbers and strings with strings. But any combination of numbers and numerical variables can be compared; the same is true for strings and string variables. The only things you cannot compare are numerical values with string values because the comparison is logically meaningless. Boolean Expressions Logical assertions are called "Boolean expressions" for George Boole, who formulated some of the rules of mathematical logic.
Boolean operations are concerned with only one thing: is a Boolean expression true or false? Unlike mathematical expressions, which evaluate to numbers, Boolean expressions evaluate to one of two values: "true" or "false. The output should look like the following: 0 -1 The display shows 0 on the first line, -1 on the second. The AND operator requires both expressions to be true if the compound expression is to be true.
When the OR operator is used, only one of the expressions has to be true for the compound expression to be true. Therefore, NOT 0 means "not false," or true. There is no practical limit to their complexity, except the obvious one of writing an expression that is difficult to read.
When Boolean expressions are evaluated, the precedence of evaluation is taken in the same order as arithmetic expressions, with AND equivalent to multiplication and OR equivalent to addition. Evaluation goes from left to right, with all the simple expressions evaluated first. As with arithmetic expressions, parentheses can be used to control the order of evaluation, rather than relying on the rules of precedence.
The next sections show how Boolean expressions are used in programs. Using Logical Statements to Control Program Flow You are always deciding what to do next by evaluating your priorities and desires, asking yourself which is most important at the moment. Similarly, BASIC has a mechanism that allows your program to select which set of operations that is, which part of the program will be executed next.
This mechanism is the IF ELSE statement. If the expression is false, the statements following ELSE are executed. A Boolean expression must be either true or false, so one or the other group of statements has to execute. Both cannot execute. The process of selecting among two or more possible actions or sections of program code is called "branching.
This is the syntax of the IF ELSE statement to decide whether a number entered is greater than, less than, or equal to 1.
The logical opposite of "greater than" is "less than or equal to" not "less than". Run this example with several values for n, including a value of to verify that the is less than or equal to statement is displayed.
You can repeat them a fixed number of times or until a particular logical condition is met. If you want to execute a block of statements times, you need only type them in once.
BASIC's control structures then control the repetitions. The ability to repeat program statements has many uses. For example, to raise a number N to the power P, N must be multiplied by itself P times.
To enter data for a ten-element array, you must prompt the user ten times. The FOR NEXT loop. Try the following: 1. The output should look like the following: this line is printed 5 times; this is time 1 this line is printed 5 times; this is time 2 this line is printed 5 times; this is time 3 this line is printed 5 times; this is time 4 this line is printed 5 times; this is time 5 4.
In this example, 1 is the start value, and 5 is the end value. Before the statements in the FOR NEXT loop are executed for the first time, the loop counter is given the start value in this case, 1. To repeat a FOR NEXT loop n times, the start and end values would normally be 1 and n.
This is not a necessity, though. They can have any two values that differ by n - 1. For example, using -2 and 2 would also cause five repetitions. The counter can be the subscript variable for an array. Accessing each element in an array is a common use for a FOR The next example prompts the user for the high temperature on each day in July: DIM july! The following example requests start and end values, then prints out the square root of every value in that range: INPUT "starting value?
The STEP statement sets a different increment value, as shown in the following procedure: 1. The output should look like the following: this line is printed 5 times; this is time 5 this line is printed 5 times; this is time 4 this line is printed 5 times; this is time 3 this line is printed 5 times; this is time 2 this line is printed 5 times; this is time 1 4. The step value must be consistent with the start and end values. If the end value is larger than the start value, the step must be positive.
If the end value is smaller than the start value, the step must be negative. If the step has the wrong sign, the FOR NEXT loop doesn't execute even once; it is skipped over. The DO NEXT loop, however, has a serious limitation.
Although you can specify the starting and ending points, the loop itself is fixed; it repeats a specific number of times, no more and no less. This caused no problem in the example where you were prompted for the 31 high temperatures in July, because July always has 31 days. However, there are times you can't know how often to repeat a calculation or an operation. Suppose you were searching through text, one line at a time, for a specific word. You don't know which line the word is on if you did, you wouldn't have to search , so you can't specify the number of times to go through a FOR You need a flexible rather than fixed way to repeat groups of program statements.
LOOP is the more flexible mechanism for repetition needed here. Try the following example: 1. Choose the File menu's New Program command to clear the View window: 2. Type the following example in the View window. Going around in circles The simple DO LOOP shown above is endless.
BASIC has a way to terminate the loop at the right time. All you have to do is specify the logical condition you want to cause termination. The user-defined condition is a Boolean expression. This expression usually compares a variable with a constant. When the expression satisfies the logical condition set by the programmer such as the variable equalling the constant , the loop terminates.
Any comparison is possible, but comparing the loop variable with a number or string is the most common. The loop termination condition is for big! Within the loop, their values are first printed, then big!
The process repeats until both are equal to 16 and the loop terminates. The initial values of big! If they are not so chosen, the loop repeats indefinitely. In practice, the program eventually stops regardless of the values chosen.
The variable little! The program then halts, displaying an Overflow error message. If big! Always be sure the variables in the Boolean expression have the values you really want before entering the loop. The following is a more practical example of a DO It is also possible to place them following the LOOP keyword.
When that is done, the statements in the loop are executed first, then the condition is tested. This assures that the loop will always be executed at least once. To demonstrate your understanding, try writing the simple program described below. The program computes the average of several numbers.
It could be written in the following sequence: 1. Prompt the user for the quantity of numbers to be averaged. If the quantity is zero or negative, print a warning message and do nothing else.
If the quantity is positive, pick a variable name for the running total and set it equal to zero. Prompt for the numbers, one at a time. Tell the user each time which value is being entered number 1, number 2, and so on. When all the numbers have been entered, compute and print the average value. This program uses only BASIC language elements that have been explained in this chapter, and no programming "tricks" are needed.
A solution is given below, but please try to create your own program before peeking. This is an "open-book" project. If you get stuck, simply reread this chapter to find the program statements you need and to see how they are used. There are many ways to write a program. If your program works, it's correct, even if it differs from the program given here.
However, the documentation does not include a complete introduction to BASIC, and does not teach the general principles of programming, or how to use DOS. The following books contain more information on those subjects. They are listed for your convenience only. With the exception of its own publications, Microsoft Corporation neither endorses these books nor recommends them over others on the same subjects. Redmond, Wash.
Dwyer, Thomas A. Reading, Mass. Enders, Bernd, and Bob Petersen. New York, N. Feldman, Phil, and Tom Rugg. Carmel, Ind. Hergert, Douglas. Inman, Don, and Bob Albrecht. Berkeley, Cal. Wolverton, Van. The comments that describe each code block may be all you need to understand the programming, but you don't have to understand the programming concepts used in each section before moving on to the next.
Then you can review the program comments and code and use QuickBASIC's on-line help and debugging features to understand how each block works. In Chapter 6 you'll see how the smart editor helps catch and correct programming errors as you enter code.
Chapters 7 and 8 give you practice in using on-line help the way you will use it while writing your own programs. You'll add some code in the form of a "procedure," a routine you write once and then call from different parts of the program, to perform a specific task. The code-entry sequences that are part of QCARDS appear under special headings, so you know which ones are necessary for finishing the tutorial program.
You can save your work as you finish each numbered sequence. If you make a mistake during a code-entry sequence, just reload the previous saved version and repeat the current sequence. These errors are intentional. You will fix them as you work through the tutorial. DAT to an empty removable disk and use that disk to complete the tutorial. It provides a convenient way to work with many small collections of information called records , which are stored together in a disk file.
Each card that appears on screen represents a record in the database. Each card has several "fields" separate areas on the card that are used to enter new data or change and reorder existing data.
When you first execute the environment, you will be placed in the editor. At the top of the editor is the main menu. The line below this contains the name of the file currently being edited. If the file has no name, the word Untitled will appear. On the far right of this line will be an arrow.
If the arrow only points up, it means the editor does not fill up the entire screen. To expand the editor window, place the mouse cursor on the arrow and [Left Mouse Button]. When the arrow points in both the up and down directions, it means the editing window occupies the entire editing area. Clicking the [Left Mouse Button] on this arrow will cause the window to reduce to its size before the window was expanded.
At the bottom of the screen is a reference bar. Above this reference bar is the immediate window. This window is used to test your programs and perform calculations. You cannot, however, load files into this window. Each window will have scroll bars at the bottom and right of its borders.
The scroll bars can be used to display where you are in the file displayed as well as move to a specific location within the file. Your position in the file is denoted by the position of the black box on the specific scroll bar. If the black box is half-way between the ends of the scroll bar, then you are currently viewing the middle of the document. To move to another area of the file, place the mouse cursor on the desired side of the black box and press the [Left Mouse Button]. If the cursor is below the black box, the window moves down one page in the file.
If you click above the black box, the editor moves the displayed text up one page. The same method can be used to with the horizontal scroll bar to move right and left in the document.
The main menu is located on the top line of the screen. There are several ways of selecting an option from the main menu. The first, and easiest, is to place the mouse cursor on the desired option and click the left mouse button. A submenu will appear from which you can select the desired option. Another approach is to hold down the [ALT] key and press the capitalized letter of the desired menu option. The third method of selecting a command from the main menu is to press the [ALT] key and then use the arrow keys to highlight the desired option.
If you decide you want a main menu command other than the one you have currently selected, use the [Left Arrow] and [Right Arrow] keys to choose the new option. Another method is to place the mouse cursor on the new option and press [Left Mouse Button].
Keyboard shortcuts are displayed next to certain options in submenus. These shortcuts specify the keyboard equivalent for the specific submenu option. To specify the option, press the specified keyboard combination. There are several ways to display the on-line help. The first is to highlight the command for which you are requesting help, and press the [F1] key. You can also select the Help menu option, and the menu will present four options.
You can get help on a specified keyword by placing the cursor on the specified keyword and clicking the [Left Mouse Button]. The Contents option displays a list of categories from which you can select for further help.
The Topic option displays help for the keyword located under the cursor in the edit window. If the cursor is not on a word, this option will be unavailable. The Help on Help option explains how to use the on-line help facility. When a topic is displayed, a menu will appear at the top of the help window. You can select any of the menu commands by placing the cursor on the desired command and pressing the [Left Mouse Button].
At the end of the help information will be a list of related keywords. You can display help on any of these keywords by selecting it with the mouse or using the [TAB] key. To exit help, press the [ESC] key. This will remove the help window and place the cursor back in the editing window. Help is also available for symbols in your own program. For example, if you wanted to see what type a variable was, you could place the text cursor on the variable and press [F1].
To do this, choose the File Print command from the main menu while the Help window is the active window. When you choose this command, a dialog box of options will appear. The Selected Text option will print only that text which is selected. The Active Window option will print the contents of the active window. The Current Module option will print the contents of the window you are currently viewing.
The All Modules option will print the contents of all the programs loaded in memory. Many of the commands listed in the on-line help come with sample programming code. To do this, display the desired sample code in the Help window. You can then use the cut and paste facilities mentioned later in this manual to copy the selected text from the Help window to your program.
Dialog boxes appear whenever you choose a menu command that is followed by three dots. They are used to get information from you so that the specific command can be executed. Dialog boxes are split into several boxes. The first type of box is a text box, you use to get a string you type on the keyboard. The List box displays a list of choices from which you are to select. The command buttons are usually displayed at the bottom of the dialog box. Option buttons will display a small dot if the option is active.
Check boxes will display an "X" if the option is selected. To exit, choose the File and then the Exit menu options File Exit. You will be asked to name the file if this is your first time saving the file.
The first type is the View window. The View window is where most of your programming is done. The Immediate window is the second type, and is used to test a line of code or do calculations.
The Help window appears when on-line help is requested. The fourth window is called the Watch window, and is where the values of specified variables are displayed while you are debugging your program. QuickBASIC only lets you use one window at a given time, even though more than one may be displayed on the screen.
The window in which you are currently working is called the active window. The active window is differentiated by having its title highlighted.
To make another window the active window, press the [F6] key. If you are using a mouse, you can make a window active by placing the mouse cursor within the window boundaries and clicking the [Left Mouse Button]. You can reduce the size of a window by one line by pressing the [ALT] [-] key combination.
These functions require that the Num Lock must not be set. It is also possible to configure the size of the window by using the mouse.
To change the size of the window, place the mouse cursor on the title bar of the window you wish to change. While pressing the [Left Mouse Button] drag the border to the desired location on the screen. When you release the button, the window will be resized. When you load a program from disk or start a new program, the source code will appear in the view window. The view window will have two cursors tracking your position. Although the content of this book aredifficult to be done in the real life, but it is still give good idea.
It makes the readers feel enjoy and still positive thinking. This book really gives you good thought that will very influence for the readers future. How to get thisbook? Getting this book is simple and easy. You can download the soft file of this book in this website. Javier Ceballos Sierra , you can also download other attractive online book in this website.
This website is available with pay and free online books. Then download it. Stall for most moments until the transfer is stain. This supple report is ready to examine in case you desire. Menu Home. PDF ePub.
Schreiber : Fco. Javier Ceballos Sierra Read Online. Newer Post Older Post Home.
0コメント