gw basic

26
GWBASIC 1.Introduction to GWBASIC program BASIC is an acronym for Beginners All-purpose Symbolic Instruction Code. It was developed by dr. John J. Kemeny and dr. Thomas E Kurtz at Dartmouth college. GWBASIC is a programming language with which we can write application programs. It's an interpreted language. That means each instruction in a given program will be executed line by line. In contrast are the compiled languages like C, where you have to write a whole program and compile it first, before running it. To run GWBASIC program, (double) click on GWBASIC.EXE file with mouse or type GWBASIC on DOS prompt. Make a note of the directory in which GWBASIC.EXE file resides. The initial screen of GWBASIC looks like this: GW-BASIC 3.23 (C) Copyright Microsoft 1983,1984,1985,1986,1987,1988 60300 Bytes free Ok _ 1LIST 2RUN 3LOAD" 4SAVE" 5CONT 6"LPT1 7TRON 8TROFF 9KEY 10SCREEN There is a cursor blinking below the word Ok. This is where the typed characters go. At the bottom of the screen you see short cuts attached to function keys F1 to F10. To enter GWBASIC program, you should know how to get out of it. Type in SYSTEM to exit or quit GWBASIC. SYSTEM command takes you to DOS prompt. You must also know that For temporary out to DOS you should type shell command. This will take you to DOS. Then you will return to your program by typing EXIT keyword on DOS. CONTROL key and BREAK key together can take you out of a program that is being executed. Knowing this now is important in case you write any endless looping in a program

Upload: hammadshaikh0011

Post on 02-Aug-2015

21 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Gw Basic

GWBASIC

1.Introduction to GWBASIC programBASIC is an acronym for Beginners All-purpose Symbolic Instruction Code. It was developed by dr. John J. Kemeny and dr. Thomas E Kurtz at Dartmouth college.GWBASIC is a programming language with which we can write application programs. It's an interpreted language. That means each instruction in a given program will be executed line by line. In contrast are the compiled languages like C, where you have to write a whole program and compile it first, before running it.

To run GWBASIC program, (double) click on GWBASIC.EXE file with mouse or type GWBASIC on DOS prompt. Make a note of the directory in which GWBASIC.EXE file resides.The initial screen of GWBASIC looks like this:

 GW-BASIC 3.23(C) Copyright Microsoft 1983,1984,1985,1986,1987,198860300 Bytes freeOk_       1LIST     2RUN     3LOAD"  4SAVE"   5CONT  6"LPT1  7TRON  8TROFF    9KEY   10SCREEN

There is a cursor blinking below the word Ok. This is where the typed characters go. At the bottom of the screen you see short cuts attached to function keys F1 to F10. To enter GWBASIC program, you should know how to get out of it. Type in SYSTEM to exit or quit GWBASIC. SYSTEM command takes you to DOS prompt. You must also know that  For temporary out to DOS you should type shell command. This will take you to DOS. Then you will return to your program by typing EXIT keyword on DOS.CONTROL key and BREAK key together can take you out of a program that is being executed. Knowing this now is important in case you write any endless looping in a program by mistake.

Rules to write a program in basic language.

1-  Statement no or line no is must for every statement appearing in the program.

Page 2: Gw Basic

2-  Statement no must be integer, ranging from 0 to 65535, but this range can be differ from one version to another.

3-  A basic line must begin with a line no and ends with a carriage return.

4-  Two statements can not have same line no.5-   more than one statement may be placed on a line, but each

can be separated by a colon (:).6-  One basic statement in must contain upto 255 characters.7-  Each successive statement or line no must be greater than

the previous one.8-  Blank spaces placed in the program where desired.9-  Each statement or line no must be followed by a basic

reserve word.10-                    Basic performs execution from lower line no to

greater line no.11-                    Usually line no’s 10, 20, …. Are used instead of

1,2,3,….

GW-BASIC has two modes1) Direct mode used as command mode &

2) Indirect mode used as programming mode.

1.1. Immediate or Direct mode: In this mode, you can enter a command or program statement and press enter key. That line will beexecuted immediately. That means you can learn most of the commands or statements easily, without writing big programs. In this mode instructions can be commands or statements. They do not have line numbers. But these lines can not be stored. This mode is useful for learning commands. Examples: EDIT 20, PRINT "Hello" etc. Try the following in GWBASIC. Note that we have not given any line numbers and that the statement is executed as soon as you pressed enter key after typing in the command.         PRINT  "Hello"

GW-BASIC 3.23(C) Copyright Microsoft 1983,1984,1985,1986,1987,198860300 Bytes freeOkPRINT "Hello"Hello- 

Page 3: Gw Basic

       1LIST     2RUN     3LOAD"  4SAVE"   5CONT  6"LPT1  7TRON  8TROFF    9KEY   10SCREEN

2.1     Programming mode:   You provide line numbers and write a program of number of lines and then run it. Programs can be saved and retrieved for later use.Type in NEW     This will tell GWBASIC that we want to start a new program. It is used before starting a new program afresh. This erases the program statements stored hitherto in the RAM or Memory. Should I type in capitals or small letters? Try them. Either way GWBASIC responds by displaying Ok.

GWBASIC is not case sensitive. It is a good practice to use capital letters for all commands or statements you use.Let us learn about GWBASIC program by entering the following program. This is programming mode.         10 REM Program1 to learn basic intro1.bas        20 PRINT "Hello World"        30 PRINT        40 PRINT "Happy Birth Day"        50 PRINT        60 END

The screen in GWBASIC looks like this: GW-BASIC 3.23(C) Copyright Microsoft 1983,1984,1985,1986,1987,198860300 Bytes freeOkNEWOk10 REM Program1 to learn basic intro1.bas20 PRINT "Hello World"30 PRINT40 PRINT "Happy Birth Day"50 PRINT60 END_ 

From now on, I am not going to show the bottom line that displays the function key shortcuts.

Type in RUN to run or execute the program. Now the GWBASIC program executes the small program we wrote. The result or output of executing this program is then displayed.The screen looks like this:

 GW-BASIC 3.23(C) Copyright Microsoft 1983,1984,1985,1986,1987,198860300 Bytes freeOk10 REM Program1 to learn basic intro1.bas20 PRINT "Hello Rohit"

Page 4: Gw Basic

30 PRINT40 PRINT "Happy Birth Day"50 PRINT60 ENDRUNHello World Happy Birth DayOk_ 

You may encounter some Syntax error messages. Please check and edit the program you entered and try again.What is this program doing? I am listing the above program again:        10 REM Program1 to learn basic intro1.bas        20 PRINT "Hello Rohit"        30 PRINT        40 PRINT "Happy Birth Day to you"        50 PRINT        60 ENDHere 10, 20, 30 etc are called line numbers. They are must for each statement of the program. They can start from 1. It's good practice to start from 10. The difference between each line number can be 1 or more. Again normally a difference of 10 is used.

After the line number you keyed in the actual program statement. REM is a statement that tells GWBASIC that what comes next is only a comment and not a program statement. This line will be ignored by GWBASIC. We use it simply to put our comments. 

It's a good practice to include comments in our program at suitable places. That makes it easier for you to understand the logic of a program , say if you were to see it after a months time! Instead of REM you can use ' (apostrophe) to give comments.

PRINT is a statement to display something on the screen. That something in this case is given as a string. Literal strings are always given in quotes. Instead of PRINT you can use ? (Question mark). 

Simple PRINT command displays an empty line. END statement tells GWBASIC that this is the end of our program.

Variable :Variables are the place that store values during program execution. LikeN$ stores a string value.Let P=3; p is variable that stores the value of 3.

Constant:Fixed values that does not change during program execution. LikeP$= “Pakistan” Pakistan is constant everywhere in programLet k=5; 5 is constant for the value of k.

Q- What are basic keywords and reserve words?

Page 5: Gw Basic

Ans- The basic keywords or reserve words are special words that can be used to give instruction to computer. These words are not used as a variable. These words are of three types:

1- Statement (print, input, let, etc.)2- Command (list, run, load, save, etc)3- Function (tab, str, string$, cos, sin, etc)

Now, start GWBASIC program again.When you enter GWBASIC, it's said to be in command mode or immediate mode. If you enter a number( for a line number) instead of a command or statement then GWBASIC knows that you want programming mode.

Commands help you list, edit, save or retrieve programs in GWBASIC. Examples of commands are LIST, EDIT, SYSTEM, SAVE etc. These are used in immediate mode only.

Statements are used in immediate mode or programming mode. Statements do tasks for us. Examples of statements are PRINT, FOR, IF etc.

In addtion to commands and statements GWBASIC provides built in functions like ABS() , system variables like DATE$ and operators like * for ease of programming.

Instructions can be typed in two ways: 2.1. Typing in and editing of programs:   one must be familiar with this to save lot of time wasted in editing of programs. I am listing the above program again :        10 REM Program1 to learn basic intro1.bas        20 PRINT "Hello Rohit"        30 PRINT        40 PRINT "Happy Birth Day to you"        50 PRINT        60 END Press ENTER key at the end of each statement. If you are one of those smart guys who use arrow keys to go to the next line, be warned that it will not do. An ENTER at the end of statement tells GWBASIC to write the statement into memory and hence is a must.

The arrow keys are used to move cursor to edit any typing mistakes. By default the GWBASIC is in what they call as OVERTYPE mode. Enter the following in GWBASIC.        30 PRNT You want to correct it by inserting an I in between R and N. So you take the cursor to the bottom of N by the use of arrow keys and type in I. See what happens! N is replaced by I. This is what is meant byover type mode.Again type in the same.        30 PRNT This time, to insert I in between R and N, you take the cursor to the bottom of N using arrow keys and press the INSERT key once. The cursor changes shape to a block cursor. If you type I now, the correction will be proper. This is called INSERT mode. Observe that Insert mode changes to Overtype mode again if you use the arrow keys.

In addition to the arrow keys and Insert key, You can use BACK SPACE, Delete, Home, END and Space Bar keys also while editing. Experiment with the edit keys.Try to type in the above given program, exactly as suggested. If you had to correct some mistakes, then it is quite possible that the screen looks cluttered with lot of Oks, which may

Page 6: Gw Basic

not be Ok with you. You can type in CLS to clear the screen. Everything on screen is cleared and you see:Ok_ Hey, where is my program? To see it type in LISTNow experiment with the LIST command by typing in following in immediate mode and observe the output.         LIST               lists all lines         LIST  20               lists line 20 only         LIST 20-40               lists line 20 to 40         LIST 20-               lists line 20 to the end         LIST -30               lists from start to line 30         LLIST               prints to printer. Do you have a printer? There is also an EDIT command to help you edit a given line. Try the following. Line 30 is presented for editing.        EDIT 30Want to show the output of the program on a fresh and clean screen?. Of course you can use CLS statement in your program to do just that. In the process of doing that, let us learn about adding or inserting a line or statement to our small program. 

The best place to insert CLS is in the beginning of our program. So let us insert a line before the first line. The line number of first line is 10. So let us write a fresh line with line number 5. You can use 1 to 9. Type in        5 CLS        LISTYou can now observe that our program has one extra line and listing shows the recently added line.Execute it by typing RUN The result is presented more cleanly.Our program starts with line number 5, then line number 10 to 50 with difference of 10 each. Let us make the line numbering uniform. Type in         RENUM        LISTYou can observe that GWBASIC helps you by arranging the line numbers.Use of RENUM is not critical but it helps organise the line numbers.

We could have asked GWBASIC to automatically add line numbers before writing a fresh program, by using AUTO command. Type in        CLS        AUTO

The screen looks like this: Ok

Page 7: Gw Basic

_auto10* 

GWBASIC types in 10 as first line number for you. You can start typing your program statement directly. You have to press CONTROL and C keys together to get out of this AUTO mode after keying in your program. Do remember this if you want to use AUTO numbering. I feel that this command can be avoided.To delete a program line or statement type in DELETE 50. This will delete line 50.You can do the same thing by typing 50 enter.Note that the command DELETE without a line number will delete the whole program from memory. This is like using NEW. Avoid using it for the present2.2. Saving and retrieving programs :   The small program you wrote is stored in the RAM or memory. As you know, this is a volatile memory. The program statements are lost if you leave the GWBASIC program or if the computer is switched off. You may need to save it to a permanent storage device to reuse it later. The SAVE command helps us save the program presently in the memory to hard disk or floppy. 

Please note that to be able to appreciate this saving and retrieving of files, you need to have some idea about directories and filename conventions. Do read the section on DOS. You must know in which directory your GWBASIC.EXE file resides. By default GWBASIC saves files into this directory and looks for files when you try to load files. 

SAVE command saves the present program in memory to the disk. LOAD command is used to reteive a file from disk in to the present memory. FILES command simply displays list of files in present direcory like the DIR command in DOS. Try the following        FILES        FILES *.bas         LIST        SAVE "PGM1.BAS"        SAVE "pgm2"                LOAD "pgm1.bas"        LIST        LOAD "PGM1"        LISTYou can specify the path name of a file in SAVE or LOAD commands , like in SAVE "c:\windows\mypgm1.bas". There is a KILL "filename" command to delete files from floppy or hard disk. Do not use it. 3.1. Writing programsWe write programs to make the computer do some tasks for us. Given a problem, we have to break it into simple tasks and write a step a by step program.1.Define the problem.How to solve it? What kinds of inputs are required? How to process the data? What kind of output is required? What kind of user interface is required?     2.Chalk out algorithm. Clear cut sequence of operations.3.Flow chart is a pictorial representation of logical steps involved.3.2.1. Integers:whole numbers like 2, -256, 10298. Integer variables may be clearly represented by using % sign suffix for the variable name. 3.2.2. Real Numbersnumbers with a decimal point like 10.5764, -23.5 , 2.5 E 103 =2.5x1000=2500. Single

Page 8: Gw Basic

precision real number variables can be clearly indicated by using ! sign suffix for the varible name. 

There is another Double precision real number. Double precision real number varibles can be clearly indicated using # sign suffix for the varible name.3.2.3. Strings: alphanumerics like a name or street address. A string variable name must have $ sign suffix. Use of dollar sign is a must to indicate string variables. Strings are always defined inside quotation marks.3.2.4.Variables:Variable is a name given by the programmer to a number or a string.As the name suggests the value of a variable can change in contrast to a constant. We may create a variable called P and give a value of 3.142 and never change it's value during the course of time of the program. We may create a variable called n$ and it's value may change as the user of the program enters different names.

There are 4 types of operators used in BASIC.1-                           Arithmetic operator(+,-,*,^,%,/,etc)2-                           Logical operator(AND, OR NOT)3-                           Relational operator(< , > , <= , >= ,< > ,= )4-                           Functional operator.(user defined, built in)

3.2.7. Relational operators are used for assignment or comparison purpose.Do not try these now.        =              equal to        >              greater than        <              less than        >=             greater than or equal to        <=             less than or equal to        <>             not equal to3.2.8. logical operators   are used to combine logical expressions. Do not try these now. NOT , AND , OR 3.2.9.Built in or library Functions :Each programming language provides some built in functions that can be used for routine work. Normally functions use () parentheses to supply parameters or the variables required.Try the following. You may not type in the comments.        10 LET X= -5                   'intro2.bas        20 LET Y= 8        30 PRINT ABS(X)                 'function ABS returns absolute value.         40 PRINT ABS(Y)                 'ABS(3)=3 and ABS(-3) = 3        50 PRINT SQR(Y)                'SQR returns square root of a number        60 A$="How are you?"        70 PRINT a$; " Length of this string=";LEN(a$)        72 REM LEN() returns length of a string        80 PRINT  "HOW";SPACE$(10); "COME"            82 REM SPACES$() returns no.of spaces        90 PRINT LEFT$(a$,2)    'retuns 2 chars on left side of string        100 PRINT RIGHT$(a$,6)  'returns 6 chars on right side         110 PRINT MID$(a$,6,3)  'returns 3 chars starting from the 6 th.        120 END4. Statements for input: Programs have to handle data. It can be numbers or strings. Also we may have some data

Page 9: Gw Basic

at the time of writing the program and we may have to get some data from the user of the program, after it was run. 4.1.Variable declaration and assignment using LET statement        LET  A=0       To define and assign variables. LET is optional.                                You can simply say A=0.         LET a=x*5        LET a$ = "Hello"Variables are a nothing but a convenient name we use for a number or a string, which we plan to use repeatedly in a program. See the following program:        10 LET a = 2        20 PRINT a*a        30 ENDHere we declared a variable and gave it the name of a. Also we assigned a value of 2 to it. When we declare a variable actually a space is reserved for it in the memory. And that space will contain whatever value we assign to the variable from time to time. Since our value is a digit, our variable is generally known as numerical type variable. All variables must be assigned with let statement.

The convenience of above program is that if we want to find square of any number we can do so by changing the value of a. We need not use print 3*3, print 4*4 etc many times.Examples of LET:        LET a = 12         LET a = -3.50         LET a$ = "Prakash"                       String variable name is with $ so that GWBASIC                        knows clearly the type of variable we want to                        use is a string or character variable and not                       a numerical    variable.          LET a$ = "2"The name you give to the variable must start with an alphabet. It must not start with a number. It can be of single character or up to 2 characters.LET is optional. We can say LET a=5 or simply a=5. Try the following:               A=5+3               PRINT aVariable names are not case sensitive. That means the names a or A and ykp or YKP are same.        PRINT a        will print the value of a if it is already defined         PRINT A         PRINT  a$   will print value of string variable a$4.2. Asking for input from user:INPUT Athis statement waits for input from user. Terminates if you enter a comma or if you press enter key. The program displays a ?(question mark) and waits for user input. See what happens if you give character input instead of a number. GWBASIC will ask you to redo from start and pose the ? (question mark) again, waiting for you to enter a number. Enter a number or press enter to get out.INPUT "Enter a number:";aINPUT "Enter a number:", a 

Page 10: Gw Basic

we can include a user prompt like in above statement. comma or semicolon can be used. Can you tell the difference?INPUT A,B,Cused to get values for 3 variables in one go. Try this .INPUT a$To ask for string input. See what happens if you give 2 as response. No problem 2 is taken as a character.LINE INPUT "Enter your name"; n$allows comma etc. in the input.4.3. Reading in known data: Try this program.You may not type in the comments:10 REM learn DATA and READ statements20 READ x,y            30 PRINT x,y        'read 1st two values of data into variables x & y.40 REM The internal pointer now points to next item in the data list 50 READ x,y60 PRINT x,y        'observe values of x and y change70 RESTORE          'moves internal pointer to beginning of DATA list 80 READ x            'observe output90 PRINT x95 DATA 1,2,3,4,5    'better to give data statement at the end100 END 5.Commands for output: You already know PRINT will output data to the screen. You can try the commands given below in direct mode.        LIST                   'lists program on screen                                              LLIST                  'lists program to printer         PRINT "Hello" 'this prints the string hello on screen        PRINT                  'prints a blank line         PRINT 2                'prints the number 2. No need for Quotation marks        PRINT 2+3              'prints result of 2+3        PRINT 5-3              'for subtraction        PRINT 2*3              'for multiplication        PRINT 16/2             'for division        PRINT 2^3              '2 raised to the power of 3 is 2x2x2=8By using a ,(comma) or ; (semi colon) you can control the way the data is displayed by print statement. This is called formatting of data.        PRINT 1;2;3;4                  if semi colon is used, prints 1234 one after another. Numbers                are allotted one extra space before them to put + or - sign                if required.+ Sign is not shown. Normally - sign if required                will be shown. For printing a string such a space is not                allocatted.          PRINT "X";"Y";"Z"              'compare this with the above.         PRINT 1,2,3,4               If comma is used each data item is printed after a

Page 11: Gw Basic

               gap of uniform distance. This can be  used to print                tabular data. To check the no of spaces a tab uses,                type the following two commands and measure.          PRINT "1234567890"          PRINT "X","Y"It's like this. The screen can show 80 characters across it. When comma is used in PRINT , the screen is divided into five parts. That means 80/5=13. So, each part is 13 characters wide.        PRINT "X","Y","Z"              'compare this string spacing with above.Try different ways a print statement is normally used:        LET X = 5.23        LET Y = 20.657        LET N$= "Ramu"         PRINT x ,y ,n$        PRINT x; y; n$         PRINT "You entered: x="; x        PRINT x; TAB(6)         'Tab() and spc() functions can be used                                'to add spaces between displayed data.         PRINT x; SPC(6), y         PRINT USING "###.##" ; x ;y                     after USING , give a format string. 3 digits before                and 2 digits after decimal are to be shown.         PRINT USING "##.###"; x ;y          PRINT STRING$(15,ASC("*"))                    used to draw lines. STRING$ function prints 15 times                the character you give. That char must be given as                ASCII code. Given below is another form of the same.         PRINT STRING$(12,42)         LPRINT "hello"         'prints to printer. Do you have one?6.0. Statements for program flow control:6.1. GOTO and ON GOTO Do not try the following.

GOTO 110       unconditional jump to line 110. Say this is given at line 50, then the next line to be executed will be 110. Better to avoid using it 

ON N GOTO 100,200,300         for taking action depending on value of N. N varies from 1 to any. Noramlly used to collect an option from user and then execute program as per the selected choice Try the following:         10 REM learn on goto        20 CLS        30 INPUT "First Number:" , x        40 INPUT "Second Number:" , y

Page 12: Gw Basic

        50 INPUT "Enter 1 to add or 2 to subtract:" , c        60 ON c GOTO 100,200        100 PRINT "SUM       =" ; x+y        200 PRINT "Difference=" ; x-y        210 END6.2. IF THEN ELSE:Following are some typical statements using IF. Program control can be transferred to a different line or calculations can be changed depending on a condition.        IF a > 0 GOTO 210        IF a< 0 GOTO 320        IF a=0 then PRINT "Equal"        IF a>= then PRINT "+ve" ELSE PRINT "-ve"        IF a <>0 then x=26       

Try the following program         10 CLS        20 INPUT " Enter a number: " , x        30 IF x < 0 THEN PRINT "-VE Number" ELSE PRINT "+VE Number"        40 END6.3. FOR - NEXT   loopsFor-Next loop is used to do repeat work or iterations. For and Next are used in conjunction. You use it when you know the number of iterations to do or when you want to repeat a task till a given varible reaches a specific value. Try the following:        5 CLS        10 FOR I=1 to 10        20 PRINT "Hello"       'can you tell how many hello are printed        30 NEXT I              'do not forget NEXT        40 END          5 CLS        10 FOR I=1 to 10 STEP 2      'how many hello are printed        20 PRINT  "Hello"        30 NEXT I        40 END         10 CLS        20 INPUT " Enter a number:" ; x        30 FOR 1to x           'count how many hello are printed        40 PRINT "Hello"        50 NEXT I        60 END6.4. WHILE WEND   LoopsWhile-Wend loop is used if you do not no how many loops or iterations to make. Here you make many loops till a condition is satisfied. If that condition is not satisfied then looping stops. Try the following:        10 CLS        20 LET x = 1        30 WHILE x >0          ' if x is greater than zero repeat        40 INPUT "Enter a number(ZERO to exit ):", x        50 PRINT "Square root of x = "; SQR(x) 'square root function        60 WEND        70 END6.5 Other commands:

Page 13: Gw Basic

STOP statement is like END. Program execution stops but unlike in the case of END, the opened files are not closed. The variables are not erased. When STOP is encountered, break in line n is printed by program. But stop is basically used for debugging.

' (apostrephe) is used for giving comments on a statement line. 

:(colon) is used to separate program statements on a single line.

BEEP rings a bell 

KEY OFF The function key shortcut prompts disappear.KEY ON oppsite of aboveON KEY(4) GOSUB 120 on pressing function key 4 or F4 goto 120 line. Do not try this. 

TRON turns on program tracing- step by step echoing for debuggingTROFF 

DATE$ returns date TIME$ returns time7. Graphics commands   :These commands can be used to create a neat user interface in color.        SCREEN 0                              text screen 80 X 25.                Screen has 25 rows and 80 columns.         COLOR forecolor,Backcolor,border               fore color 0 to 31;backcolor 0 to 7;                border 0 to 15          LOCATE row,col,cursor on or off                row= 1 to 25; col= 1 to 80;                cursor on=1 or cursor off=0 Try this program:         10 CLS        20 SCREEN 0        30 COLOR 4,1        40 LOCATE 5,25        'row 5 and column 25        50 PRINT "This is my Program"        60 BEEP        70 ENDActually the above can hardly be seen as graphics. I touched the locate command because it its very useful.8. User defined functions:In addtion to built in functions you can define your own functions for calculations you need to do repeatedly. Try this program:        10 CLS        20 DEF FNa(R) = 3.14*R*R              ' define FNa function         30 REM the FN in the name is a must. it takes radius R as param        40 INPUT "Enter radius of circle:", x        50 PRINT "Area of circle="; FNa(x)    ' use the function10.Programming ExamplesExample1: 

Page 14: Gw Basic

Write a program to print name ten times by using a) FOR … NEXT Statementb) IF … THEN Statement.

GW-BASIC 3.23(C) Copyright Microsoft 1983,1984,1985,1986,1987,198860300 Bytes freeOk10 ‘ PRACTICAL 1(A)(FOR ... NEXT STATEMENT)20 CLS30 INPUT “ENTER YOUR NAME” ; N$40 PRINT TAB(15); “PROGRAM TO PRINT NAME 10 TIMES BY FOR ... NEXT”50 FOR A = 1 TO 1060 PRINT TAB(19);A, N$70 NEXT A80 END

GW-BASIC 3.23(C) Copyright Microsoft 1983,1984,1985,1986,1987,198860300 Bytes freeOk 10 REM PRACTICAL 1(B)(IF ...THEN STATEMENT)20 INPUT “ENTER YOUR NAME”;N$30 PRINT TAB(15); “PROGRAM TO PRINT NAME 10 TIMES”40 LET A=050 LET A=A+160 PRINT TAB(19);A, N$70 IF A= 10 THEN END ELSE GOTO 50 

Example2:WRITE A PROGRAM TO CALCULATE SQUARE AND CUBE OF FIRST 10 NATURAL NUMBERS.

GW-BASIC 3.23(C) Copyright Microsoft 1983,1984,1985,1986,1987,198860300 Bytes freeOk10 REM PRACTICAL 2 (NUMBER, SQUARE & CUBE)20 CLS30 PRINT TAB(13) “NUMBERS”, “SQUARE”, “CUBE”40 FOR N=1 TO 1050 PRINT TAB(13);N,N^2,N^360 NEXT N70 END

Example3:

Page 15: Gw Basic

A) WRITE A PROGRAM TO GENERATE TABLE OF ANY GIVEN NUMBER.B) WRITE A PROGRAM TO FIND OUT AVERAGE OF ANY FOUR NUMBERS.

GW-BASIC 3.23(C) Copyright Microsoft 1983,1984,1985,1986,1987,198860300 Bytes freeOk10 REM PRACTICAL 3(A) (TABLE OF GIVEN NUMBER)20 CLS30 INPUT “ENTER ANY TABLE NUMBER”;T40 PRINT TAB(15) “PROGAM TO GENETATE TABLE OF ANY NUMBER”50 FOR N=1 TO 1060 PRINT TAB(22)T;”X”;N;”=”T*N70 NEXT N80 END 

GW-BASIC 3.23(C) Copyright Microsoft 1983,1984,1985,1986,1987,1988Ok10 REM PRACTICAL 3(B) (FINDING AVERAGE OF 4 NO.s)20 CLS30 INPUT “ENTER 1ST NUMBER”;N140 INPUT “ENTER 2ND NUMBER”;N250 INPUT “ENTER 3RD NUMBER”;N360 INPUT “ENTER 4TH NUMBER”;N470 LET   TOT=N1+N2+N3+N480 LET AVG=TOT/490 PRINT TAB(12); “PROGRAM TO FIND AVERAGE OF GIVEN NUMBERS”100 PRINT TAB(14); “THE FIRST NUMBER = = = >”;N1110 PRINT TAB(14); “THE SECOND NUMBER= = >”;N2120 PRINT TAB(14); “THE THIRD NUMBER = = = >”;N3130 PRINT TAB(14); “THE FOURTH NUMBER = = >”;N4140 PRINT TAB(14); “THE TOTAL NUMBER = = = >”; TOT150 PRINT TAB(14); “THE AVERAGE NUMBER= = >”; AVG160 END

Example 4:WRITE A PROGRAM TO ARRANGE THE FOLLOWING NUMBERS3,13,23,8,18,28INA) ASSENDING ORDERB) DESCENDING ORDER

GW-BASIC 3.23(C) Copyright Microsoft 1983,1984,1985,1986,1987,198860300 Bytes freeOk10 REM PRACTICAL 4(A) (ASSENDING ORDER)20 CLS30 PRINT TAB(15); “ASSENDING ORDER”40 FOR A=3 TO 28 STEP 5

Page 16: Gw Basic

50 PRINT TAB(22);A60 NEXT A70 END

GW-BASIC 3.23(C) Copyright Microsoft 1983,1984,1985,1986,1987,198860300 Bytes freeOk10 REM PRACTICAL 4(B)(DESCENDING ORDER)20 CLS30 PRINT TAB(15); “DESCENDING ORDER”40 FOR A=28 TO 3 STEP -550 PRINT TAB(22);A60 NEXT A70 END

 Example 5: 

A) WRITE A PROGRAM TO PREPARE MARK SHEET OF FIVE PAPERS

GW-BASIC 3.23(C) Copyright Microsoft 1983,1984,1985,1986,1987,1988Ok10 REM PRACTICAL 5 (MARK SHEET)CLS20 INPUT “ENTER MARKS OF ENGLISH”;EN30 INPUT “ENTER MARKS OF SINDHI”;SI40 INPUT “ENTER MARKS OF PAK-STUDIES”;PK50 INPUT “ENTER MARKS OF COMPUTER”;CO60 INPUT “ENTER MARKS OF CHEMISTRY”;CH70 LET   TOT=EN+SI+PK+CO+CH80 LET PER=TOT * 100 / 42590 PRINT TAB(28); “MARKS SHEET”100 PRINT TAB(28); “**************”110 PRINT TAB(14); “MARKS OF ENGLISH = = = = = >”;EN120 PRINT TAB(14); “MARKS OF SINDHI= = = = = = >”;SI130 PRINT TAB(14); “MARKS OF PAK STUDIES= =  >”;PK140 PRINT TAB(14); “MARKS OF COMPUTER= = = >”;CO150 PRINT TAB(14); “MARKS OF CHEMISTRY= = = >”; CH160 PRINT TAB(14); “TOTAL MARKS ARE = = = = = >”; TOT170 PRINT TAB(14); “PERCENTAGE CLACULATED= >”; PER180 END

Example 6: A) WRITE A PROGRAM TO CONVERT DOLLAR INTO RUPEES AND RUPEES INTO DOLLARS.

GW-BASIC 3.23(C) Copyright Microsoft 1983,1984,1985,1986,1987,198860300 Bytes freeOk10 REM PRACTICAL 6 (CURRENCY CONVERSION)20 CLS30 INPUT “ENTER AMOUNT IN DOLLARS”;D40 INPUT “ENTER AMOUNTIN RUPEE”;R50 LET RUP = D*55

Page 17: Gw Basic

60 LET DOL = R/5570 PRINT TAB(14); “PROGRAM TO CONVERT CURRNCY RATES”80 PRINT TAB(14) “AMOUNT IN DOLLARS”;D90 PRINT TAB(14); “CONVERTED IN RUPEES”;RUP100 PRINT TAB(14) “AMOUNT IN RUPEES”;R110 PRINT TAB(14); “CONVERTED IN DOLLARS”;DOL120 END  

Example 7: Example3:A) WRITE A PROGRAM TO CONVERT CENTIGRADE INTO FARENHEIT.B) WRITE A PROGRAM TO CONVERT FARENHEIT INTO CENTIGRADE.

GW-BASIC 3.23(C) Copyright Microsoft 1983,1984,1985,1986,1987,198860300 Bytes freeOk 10 REM PRACTICAL 7(A) (CENTIGRADE TO FARENHEIT)20 CLS30 INPUT “ENTER TEMPERATURE IN CENTIGRADE”;C40 LET F=(9/5*C)+3250 PRINT TAB(14); “CONVERTION OF TEMPERATURE”60 PRINT: PRINT TAB(14) “TEMPERATURE IN CENTIGRADE”;C70 PRINT TAB(14); “CONVERTED INTO FARENHEIT”;F80 END  

GW-BASIC 3.23(C) Copyright Microsoft 1983,1984,1985,1986,1987,1988Ok10 REM PRACTICAL 7(B) (FARENHEIT TO CENTIGRADE)20 CLS30 INPUT “ENTER TEMPERATURE IN FARENHEIT”;F40 LET C=(5/9*F-32)50 PRINT TAB(14); “CONVERTION OF TEMPERATURE”60 PRINT: PRINT TAB(14) “TEMPERATURE IN FARENHEIT”;F70 PRINT TAB(14); “CONVERTED INTO CENTIGRADE”;C80 END

Example 9 :- GW-BASIC 3.23(C) Copyright Microsoft 1983,1984,1985,1986,1987,198860300 Bytes freeOk10 REM PRACTICAL 9 (ELECTRICITY BILL)20 CLS30 INPUT “ENTER METER NUMBER”;MN$40 INPUT “ENTER PRESENT READING;PR50 INPUT “ENTER PREVIOUS READING”;P60 LET UC=PR-P70 LET AMT=UC*1.2580 PRINT TAB(14); “##### METER NUMBER######”

Page 18: Gw Basic

90 PRINT TAB(14) “METER NUMBER”;MN$100 PRINT TAB(14); “PRESENT READING”;PR110 PRINT TAB(14); “PREVIOUS READING”;P120 PRINT TAB(14) “UNIT CONSUMED”;UC130 PRINT TAB(14); “AMOUNT CHARGE”;AMT140 END  

Example 10:WRITE A PROGRAM TO PREPARE BIO-DATAINPUT / OUPUT INFORMATIONNAME, FATHERS NAME, DATE OF BIRTH, QUALIFICATION, SEX, NATIONALITY, RELIGON.

GW-BASIC 3.23(C) Copyright Microsoft 1983,1984,1985,1986,1987,198860300 Bytes freeOk10 REM PRACTICAL 10 (BIO-DATA)20 CLS30 INPUT “ENTER STUDENT NAME”:N$40 INPUT “ENTER FAHTERS NAME”:F$50 INPUT “ENTER DATE OF BIRTH”:D$60 INPUT “ENTER QUALIFICATION”:Q$70 INPUT “ENTER STUDENT SEX”:S$80 INPUT “ENTER NATIONLAITY”;P$90 INPUT “ENTER RELIGON”:R$100 CLS110 PRINT TAB(15) “BIO-DATA”120 PRINT TAB(12)“STUDENT NAME= =”:N$130 PRINT TAB(12)“FAHTERS NAME= =”:F$140 PRINT TAB(12)“DATE OF BIRTH= =”:D$150 PRINT TAB(12)“QUALIFICATION =”:Q$160 PRINT TAB(12)“SEX= = = =  = = = =”:S$170 PRINT TAB(12)“NATIONLAITY= = =”;P$180 PRINT TAB(12)“RELIGON= = = =  =”:R$190 END 

Example11:RITE A PROGRAM TO CALCULATE SUM OF FIRST 10 NATURAL NUMBERS.

 GW-BASIC 3.23(C) Copyright Microsoft 1983,1984,1985,1986,1987,198860300 Bytes freeOk10 REM PRACTICAL 11 (SUM OF NATURAL NUMBERS)20 CLS30 PRINT TAB(13) “PROGRAM TO FIND SUM OF FIRST 10

Page 19: Gw Basic

NATURAL NUMBERS”40 FOR N=1 TO 1050 PRINT TAB(24); N60 SUM=SUM+N70 NEXT80 PRINT TAB(20) “THE SUM IS ”;SUM90 END 

Example12:WRITE A PROGRAM TO PREPARE EMPLOYEES SALARY SHEET.TOTAL SALARY =BASIC SALARY+ ALLOWNCE – DEDUCTIONINPUT INFORMATION=EMPLOYEE NAME, DESIGNATION, BASIC SALARY, TOTAL ALLOWANCE, TOTAL DEDUCTIONINPUT INFORMATION=EMPLOYEE NAME, DESIGNATION, BASIC SALARY, TOTAL ALLOWANCE, TOTAL DEDUCTION, TOTAL SALARY

GW-BASIC 3.23(C) Copyright Microsoft 1983,1984,1985,1986,1987,198860300 Bytes freeOk10 REM PRACTICAL 12 (SALARY SHEET)20 CLS30 INPUT “ENTER EMPLOYEE NAME”;EN$40 INPUT “ENTER EMPLOYEE DESIGNATION”;D$50 INPUT “ENTER BASIC SALARY”;BS60 INPUT “ENTER TOTAL ALLOWANCE”;TA70 INPUT “ENTER TOTAL DEDUCTION”;TD80 LET TS = BS+TA-TD90 PRINT TAB(14); “EMPLOYEE SALARY SHEET”100 PRINT TAB(14) “EMPLOYEE NAME”;EN$110 PRINT TAB(14); “EMPLOYEE DESIGNATION”;D$120 PRINT TAB(14); “EMPLOYEE BASIC SALARY”;BS130 PRINT TAB(14) “TOTAL ALLOWANCE”;TA140 PRINT TAB(14); “TOTAL DEDUCTION”;TD150 PRINT TAB(14); “TOTAL SALARY”;TS160 END 

Example13:WRITE A PROGRAM TO FIND OUT GREATEST NUMBER FROM ANY GIVEN THREE NUMBERS.

GW-BASIC 3.23(C) Copyright Microsoft 1983,1984,1985,1986,1987,198860300 Bytes freeOk10 REM PRACTICAL 13 (FINDING GREATEST NUMBER)20 CLS30 INPUT “ENTER FIRST NUMBER”;N140 INPUT “ENTER SECOND NUMBER”;N250 INPUT “ENTER THIRD NUMBER”;N360 LET BIG=N1

Page 20: Gw Basic

70 IF N2 > BIG THEN BIG = N280 IF N3 > BIG THEN BIG = N390 PRINT TAB(14); “FINDING GREATEST NUMBER”100 PRINT TAB(14) “FIRST NUMBER”;N1110 PRINT TAB(14); “SECOND NUMBER”;N2120 PRINT TAB(14); “THIRD NUMBER”;N3130 PRINT TAB(14) “THE BIGGEST NUMBER IS”;BIG140 END 

11. Arrays:Arrays are used to store series of data. If you need to collect marks for a student for 4 subjects, you will create four variables. But what if marks for ten subjects have to be handled. Instead of creating ten variables, create a single array variable of 10 elements. some typical statements: 

DIM x(10)used to define the array. Here an array of 11 elements is defined. starting element is 0 and last one is 10.if you try to access beyond the element 10, then error "subscript out of range" occurs. 

OPTION BASE 1DIM x(10)this statement makes the element numbers to start from 1 instead of 0. In that case total elements will be 10. By default option base is 0. 

DIM x(s)here an array of 0 to s elements is defined. S in a variable. LET X(1)=0each element must be assigned a value separately. The default value is zero. DIM (4,6)Defines 2 dimensional array with 4 rows and 6 columns. Try this program:         10 CLS        15 OPTION BASE 1        20 DIM x(10): 'define array        30 FOR I=1 TO 10        40 INPUT "Enter any number: " ; n        50 x(I) = n    'assign values to each element        60 NEXT I        70 FOR I = 1 TO 10        75 REM access each element        80 PRINT "You entered : "; I ; ") " ; x(I)          90 NEXT I        100 ENDExample 8:

WRITE A PROGRAM TO CALCULATE RECORDS OF 10 STUDENTS. RECORD CONTAINS NAME AND GENDER EITHER (MALE OR FEMALE). CALCULATE TOTAL NUMBER OF MALE AND FEMALE STUDENTS.

GW-BASIC 3.23(C) Copyright Microsoft 1983,1984,1985,1986,1987,198860300 Bytes freeOk10 REM PRACTICAL 8 (STUDENT’S RECORD)

Page 21: Gw Basic

20 CLS30 DIM N$(10), S$(10)40 FOR A=1 TO 1050 INPUT “ENTER STUDENT NAME”;N$(A)40 INPUT “ENTER STUDENTS GENDER(A)50 NEXT A60 CLS70 PRINT “S.NO”,”NAME”,”SEX”80 FOR B=1 TO 1090 PRINT B, N$(B), S$(B)100 IF S$(B)=”MALE” THEN M = M + 1110 IF S$(B)=”FEMALE” THEN F=F+1120 PRINT “TOTAL NUMBER OF MALE STUDENTS ARE:”;M130 PRINT “TOTAL NUMBER OF FEMALE STUDENTS ARE:”;F140 END