Thursday, 3 November 2016

Basic Functions in GW-BASIC and Examples

Commands Statements Functions

FUNCTION KEYS

ALTERNATE KEYS





Examples 
1:Write a program to 1 to 10 print number using FOR loop. 
Flowchart:- 
 Source code:-
 output
2:- Write a program to print"voice of sindh"Ten Times using FOR loop.
source code:-
 output:-

 3:-Write a program to print Table .
 source code:-
 output:-

4:-Write a program to Print Square and cube of natural numbers . 
Flow Chart:-
 Source code :-
 output:-
5:-write a program to change Age into Days . 
Source code :-

6:-write a program to print the complete result and calculate total marks.
Flow chart :-  
 Source code :-
Output:-

7:-Write a program to change Rupees in to dollar .

Tuesday, 1 November 2016

The GW-BASIC Screen Editor

You can edit GW-BASIC program lines as you enter them, or after they have been saved in a program file.

4.1 Editing Lines in New Files

If an incorrect character is entered as a line is being typed, it can be deleted with the BACKSPACE or DEL keys, or with CTRL-H. After the character is deleted, you can continue to type on the line.

The ESC key lets you delete a line from the screen that is in the process of being typed. In other words, if you have not pressed the RETURN key, and you wish to delete the current line of entry, press the ESC key.

To delete the entire program currently residing in memory, enter the NEW command. NEW is usually used to clear memory prior to entering a new program.

4.2 Editing Lines in Saved Files

After you have entered your GW-BASIC program and saved it, you may discover that you need to make some changes. To make these modifications, use the LIST statement to display the program lines that are affected:

 Reload the program.

 Type the LIST command, or press the F1 key.

 Type the line number, or range of numbers, to be edited.

The lines will appear on your screen.

4.2.1 Editing the Information in a Program Line

You can make changes to the information in a line by positioning the cursor where the change is to be made, and by doing one of the following:

Typing over the characters that are already there.

Deleting characters to the left of the cursor, using the BACKSPACE key.

Deleting characters at the cursor position using the DEL key on the number pad.

Inserting characters at the cursor position by pressing the INS key on the number pad. This moves the characters following the cursor to the right making room for the new information.

 Adding to or truncating characters at the end of the program line.

If you have changed more than one line, be sure to press RETURN on each modified line. The modified lines will be stored in the proper numerical sequence, even if the lines are not updated in numerical order.


Note:- 
A program line will not actually have changes recorded within the GW-BASIC program until the RETURN key is pressed with the cursor positioned somewhere on the edited line.


You do not have to move the cursor to the end of the line before pressing the RETURN key. The GW-BASIC Interpreter remembers where each line ends, and transfers the whole line, even if RETURN is pressed while the cursor is located in the middle or at the beginning of the line.

To truncate, or cut off, a line at the current cursor position, type CTRL-END or CTRL-E, followed by pressing the RETURN key.

If you have originally saved your program to a program file, make sure that you save the edited version of your program. If you do not do this, your modifications will not be recorded.

4.3 Special Keys

The GW-BASIC Interpreter recognizes nine of the numeric keys on the right side of your keyboard. It also recognizes the BACKSPACE key, ESC key, and the CTRL key. The following keys and key sequences have special functions in GW-BASIC:



 BACKSPACE or CTRL-H    


           

Deletes the last character typed, or deletes the character to the left of the cursor. All characters to the right of the cursor are moved left one position. Subsequent characters and lines within the current logical line are moved up as with the DEL key.

 CTRL-BREAK or CTRL-C

           Returns to the direct mode, without saving changes made to the current line. It will also exit auto line-numbering mode.

CTRL-CURSOR LEFT or CTRL-B
         

             Moves the cursor to the beginning of the previous word. The previous word is defined as the next character to the left of the cursor in the set A to Z or in the set 0 to 9.
 CTRL-CURSOR RIGHT or CTRL-F
 Moves the cursor to the beginning of the next word. The next word is defined as the next character to the right of the cursor in the set A to Z or in the set 0 to 9. In other words, the cursor moves to the next number or letter after a blank or other special character.  Moves the cursor to the beginning of the next word. The next word is defined as the next character to the right of the cursor in the set A to Z or in the set 0 to 9. In other words, the cursor moves to the next number or letter after a blank or other special character.
CTRL-HOME or CTRL-L      
 Clears the screen and positions the cursor in the upper left corner of the screen.
 CURSOR LEFT or CTRL-]  




             Moves the cursor one position left. When the cursor is advanced beyond the left edge of the screen, it will wrap to the right side of the screen on the preceding line.
CTRL-K or HOME    
 Moves the cursor to the upper left corner of the screen. The screen contents are unchanged.
 CTRL-G     

Causes a beep to emit from your computer's speaker.

 CTRL-[ or ESC  


 Erases the entire logical line on which the cursor is located.
 CTRL-M or RETURN    

Enters a line into the GW-BASIC program. It also moves the cursor to the next logical line.
CTRL-RETURN or CTRL-J     
Moves the cursor to the beginning of the next screen line. This lets you create logical program lines which are longer than the physical screen width. Logical lines may be up to 255 characters long. This function may also be used as a line feed.
 CTRL-N or END  

Moves the cursor to the end of the logical line. Characters typed from this position are added to the line.
CTRL-END or CTRL-E         
 Erases from the cursor position to the end of the logical line. All physical screen lines are erased until the terminating RETURN is found.
CTRL-BACKSPACE or DEL   
Deletes the character positioned over the cursor. All characters to the right of the one deleted are then moved one position left to fill in where the deletion was made.If a logical line extends beyond one physical line, characters on subsequent lines are moved left one position to fill in the previous space, and the character in the first column of each subsequent line is moved up to the end of the preceding line.DEL (delete) is the opposite of INS (insert). Deleting text reduces logical line length.
 CURSOR DOWN or CTRL--            

Moves the cursor down one line on the screen.
 CURSOR UP or CTRL-6         

Moves the cursor up one line on the screen.
 CURSOR RIGHT or CTRL-\

 Moves the cursor one position right. When the cursor is advanced beyond the right edge of the screen, it will wrap to the left side of the screen on the following line.




Reviewing and Practicing GW-BASIC

The practice sessions  good time to turn on your computer and load the GW-BASIC Interpreter.
3.1 Example for the Direct Mode

You can use your computer in the direct mode to perform fundamental arithmetic operations. GW-BASIC recognizes the following symbols as arithmetic operators:

GW-BASIC Operator

Addition '+'

Subtraction'-'

Multiplication'*'

Division'/'

To enter a problem, respond to the Ok prompt with a question mark (?), followed by the statement of the problem you want to solve, and press the RETURN key. In GW-BASIC, the question mark can be used interchangeably with the keyword PRINT. The answer is then displayed.

Type the following and press the RETURN key:

?2+2

GW-BASIC will display the answer on your screen:

?2+2
 4

To practice other arithmetic operations, replace the + sign with the desired operator.


3.2 Examples for the Indirect Mode

The GW-BASIC language can be used for functions other than simple algebraic calculations. You can create a program that performs a series of operations and then displays the answer. To begin programming, you create lines of instructions called statements. Remember that there can be more than one statement on a line, and that each line is preceded by a number.

For example, to create the command PRINT 2+3 as a statement, type the following:

10 print 2+3

When you press the RETURN key, the cursor shifts to the next line, but nothing else happens. To make the computer perform the calculation, type the following and press the RETURN key:
 
run

Your screen should look like this:

Ok
10 print 2+3
run
 5
Ok

You have just written a program in GW-BASIC.

The computer reserves its calculation until specifically commanded to continue (with the RUN command). This allows you to enter more lines of instruction. When you type the RUN command, the computer does the addition and displays the answer.

The following program has two lines of instructions. Type it in:
 
10 x=3
20 print 2+x

Now use the RUN command to have the computer calculate the answer.

Your screen should look like this:

Ok
10 x=3
20 print 2+x
run
 5
Ok

The two features that distinguish a program from a calculation are
 
The numbered lines

The use of the run command

These features let the computer know that all the statements have been typed and the computation can be carried out from beginning to end. It is the numbering of the lines that first signals the computer that this is a program, not a calculation, and that it must not do the actual computation until the RUN command is entered.
 
In other words, calculations are done under the direct mode. Programs are written under the indirect mode.


 
To display the entire program again, type the LIST command and press the RETURN key:

list
Your screen should look like this:

Ok
10 x=3
20 print 2+x
run
Ok
 5
Ok
list
10 X=3
20 PRINT 2+X
Ok

You'll notice a slight change in the program. The lowercase letters you entered have been converted into uppercase letters. The LIST command makes this change automatically.

3.3 Function Keys


Function keys are keys that have been assigned to frequently-used commands. The ten function keys are located on the left side of your keyboard. A guide to these keys and their assigned commands appears on the bottom of the GW-BASIC screen. To save time and keystrokes, you can press a function key instead of typing a command name.


For example, to list your program again, you needn't type the LIST command; you can use the function key assign to it, instead:

 Press the F1 key.

Press RETURN.


Your program should appear on the screen.

To run the program, simply press the F2 key, which is assigned to the RUN command.

As you learn more commands, you'll learn how to use keys F3 through F10. Chapter 4, "The GW-BASIC Screen Editor," contains more information about keys used in GW-BASIC.

3.4 Editing Lines

There are two basic ways to change lines. You can
 
Delete and replace them
  
Alter them with the EDIT command

To delete a line, simply type the line number and press the RETURN key. For example, if you type 12 and press the RETURN key, line number 12 is deleted from your program.


To use the EDIT command, type the command EDIT, followed by the number of the line you want to change. For example, type the following, and press the RETURN key:

edit 10

You can then use the following keys to perform editing:

Key Function
CURSOR UP, 

      CURSOR DOWN, 

      CURSOR LEFT, 

      CURSOR RIGHT Moves the cursor within the statement
BACKSPACE Deletes the character to the left of the cursor
 DELETE (DEL) Deletes the current character
 INSERT (INS) Lets you insert characters to the left of the cursor.


For example, to modify statement (line) 10 to read x=4, use the CURSOR-RIGHT control key to move the cursor under the 3, and then type a 4. The number 4 replaces the number 3 in the statement.

Now press the RETURN key, and then the F2 key.

Your screen displays the following:

Ok
10 X=4
RUN
 6
Ok

3.5 Saving Your Program File


Creating a program is like creating a data file. The program is a file that contains specific instructions, or statements, for the computer. In order to use the program again, you must save it, just as you would a data file.

To save a file in GW-BASIC, use the following procedure:
 
Press the  F4 key. The command word SAVE" appears on your screen.

 
Type a name for the program, and press the RETURN key. The file is saved under the name you specified.

To recall a saved file, use the following procedure:
  
Press the F3 key. The command load LOAD" appears on your screen.  
 
Type the name of the file.
 
Press RETURN.

The file is loaded into memory, and ready for you to list, edit, or run.

Getting Started with GW-BASIC

It explains the two different types of operation modes, line formats, and the various elements of GW-BASIC.

2.1 Loading GW-BASIC

To use the GW-BASIC language, you must load it into the memory of your computer from your working copy of the MS-DOS diskette. Use the following procedure:

Turn on your computer.

Insert your working copy of the MS-DOS diskette into Drive A of your computer, and press RETURN.

Type the following command after the A> prompt, and press RETURN:

gwbasic


Once you enter GW-BASIC, the GW-BASIC prompt, Ok, will replace the MS-DOS prompt, A>.

On the screen, the line XXXXX Bytes Free indicates how many bytes are available for use in memory while using GW-BASIC.


The function key (F1–F10) assignments appear on the bottom line of the screen. These function keys can be used to eliminate key strokes and save you time. Chapter 4, "The GW-BASIC Screen Editor," contains detailed information on function keys.

2.2 Modes of Operation

Once GW-BASIC is initialized (loaded), it displays the Ok prompt. Ok means GW-BASIC is at command level; that is, it is ready to accept commands. At this point, GW-BASIC may be used in either of two modes: direct mode or indirect mode.

2.2.1 Direct Mode

In the direct mode, GW-BASIC statements and commands are executed as they are entered. Results of arithmetic and logical operations can be displayed immediately and/or stored for later use, but the instructions themselves are lost after execution. This mode is useful for debugging and for using GW-BASIC as a calculator for quick computations that do not require a complete program.

2.2.2 Indirect Mode

The indirect mode is used to enter programs. Program lines are always preceded by line numbers, and are stored in memory. The program stored in memory is executed by entering the RUN command.

2.3 The GW-BASIC Command Line Format

The GW-BASIC command line lets you change the environment or the conditions that apply while using GW-BASIC.


2.4 GW-BASIC Statements, Functions, Commands, and Variables

A GW-BASIC program is made up of several elements: keywords, commands, statements, functions, and variables.

2.4.1 Keywords

GW-BASIC keywords, such as PRINT, GOTO, and RETURN have special significance for the GW-BASIC Interpreter. GW-BASIC interprets keywords as part of statements or commands.

Keywords are also called reserved words. They cannot be used as variable names, or the system will interpret them as commands. However, keywords may be embedded within variable names.

Keywords are stored in the system as tokens (1- or 2-byte characters) for the most efficient use of memory space.

2.4.2 Commands

Commands and statements are both executable instructions. The difference between commands and statements is that commands are generally executed in the direct mode, or command level of the interpreter. They usually perform some type of program maintenance such as editing, loading, or saving programs. When GW-BASIC is invoked and the GW-BASIC prompt, Ok, appears, the system assumes command level.

2.4.3 Statements

A statement, such as ON ERROR...GOTO, is a group of GW-BASIC keywords generally used in GW-BASIC program lines as part of a program. When the program is run, statements are executed when, and as, they appear.

2.4.4 Functions

The GW-BASIC Interpreter performs both numeric and string functions.

2.4.4.1 Numeric Functions

The GW-BASIC Interpreter can perform certain mathematical (arithmetical or algebraic) calculations. For example, it calculates the sine (SIN), cosine (COS), or tangent (TAN) of angle x.

Unless otherwise indicated, only integer and single-precision results are returned by numeric functions.

2.4.4.2 String Functions

String functions operate on strings. For example, TIME$ and DATE$ return the time and date known by the system. If the current time and date are entered during system start up, the correct time and date are given (the internal clock in the computer keeps track).

2.4.4.3 User-Defined Functions

Functions can be user-defined by means of the DEF FN statement. These functions can be either string or numeric.

2.4.5 Variables

Certain groups of alphanumeric characters are assigned values and are called variables. When variables are built into the GW-BASIC program they provide information as they are executed.

For example, ERR defines the latest error which occurred in the program; ERL gives the location of that error. Variables can also be defined and/or redefined by the user or by program content.


All GW-BASIC commands, statements, functions, and variables are individually described in the GW-BASIC User's Reference.

2.5 Line Format

Each of the elements of GW-BASIC can make up sections of a program that are called statements. These statements are very similar to sentences in English. Statements are then put together in a logical manner to create programs. The GW-BASIC User's Reference describes all of the statements available for use in GW-BASIC.


In a GW-BASIC program, lines have the following format:


nnnnn statement[statements]

nnnnn is a line number


statement is a GW-BASIC statement.


A GW-BASIC program line always begins with a line number and must contain at least one character, but no more than 255 characters. Line numbers indicate the order in which the program lines are stored in memory, and are also used as references when branching and editing. The program line ends when you press the RETURN key.


Depending on the logic of your program, there may be more than one statement on a line. If so, each must be separated by a colon (:). Each of the lines in a program should be preceded by a line number. This number may be any whole integer from 0 to 65529. It is customary to use line numbers such as 10, 20, 30, and 40, in order to leave room for any additional lines that you may wish to include later. Since the computer will run the statements in numerical order, additional lines needn't appear in consecutive order on the screen: for example, if you entered line 35 after line 60, the computer would still run line 35 after line 30 and before line 40. This technique may save your reentering an entire program in order to include one line that you have forgotten.


The width of your screen is 80 characters. If your statement exceeds this width, the cursor will wrap to the next screen line automatically. Only when you press the RETURN key will the computer acknowledge the end of the line. Resist the temptation to press RETURN as you approach the edge of the screen (or beyond). The computer will automatically wrap the line for you. You can also press CTRL-RETURN, which causes the cursor to move to the beginning of the next screen line without actually entering the line. When you press RETURN, the entire logical line is passed to GW-BASIC for storage in the program.


In GW-BASIC, any line of text that begins with a numeric character is considered a program line and is processed in one of three ways after the RETURN key is pressed:


A new line is added to the program. This occurs if the line number is legal (within the range of 0 through 65529), and if at least one alpha or special character follows the line number in the line.


An existing line is modified. This occurs if the line number matches the line number of an existing line in the program. The existing line is replaced with the text of the newly-entered line. This process is called editing.

2.6 Returning to MS-DOS


Before you return to MS-DOS, you must save the work you have entered under GW-BASIC, or the work will be lost.

To return to MS-DOS, type the following after the Ok prompt, and press RETURN:

system

The system returns to MS-DOS, and the A> prompt appears on your screen.