Page 1 :
CHAPTER -6, , Introduction to QBasic, Every simple operation to complex mathematical calculations is, performed by computers very easily. It happens only because computer, , follows, , a, , set, , of pre-defined instructions. This set of instruction is called, , a program., , All Purpose, stands for Beginner's, It, beginners., language for, George Kemeny and, a programming, is, in 1964, BASIC, designed, was, understand., Code. BASIC, language to, programming, Symbolic Instruction, simple, , BASIC, , by John, , Thomas Eugene, , GW BASIC, Quick, , Kurtz., , It is, , an, , easy and, , are, BASIC and QBASIC, , QBasie, level programming, QBasicis high, , the popular, , language, , versions, , created, , by, , of BASIC., , Microsoft., , It is based upon the, , a, , originallanguage BASIC., , Itis very easy to, , learn and, , can, , your, Itcan help you developand, , be used to, , utilities, games,, , demos etc., , learn other languages, and enable you to, skills, programming, , many others, , such as C, Visual Basic, , create, , quickly., , Elements ofQBasic, constitute a program in, Let us learn about the elements that, , QBASIC., , Character Set, dracter sets are all, , used to write a program, those characters which are, , character set includes:, Alphabets: Ato Zor a to z, , Numerals: 0to 9, Specialcharacters: + - /; $ #!?& | etc., , in QBasic., , The
Page 2 :
heywords or Reserved Words, QBasic have a pre-defined words called, keywords, , reserved words. They have, specifi., cific, , or, , meaning in the program. A few QBasic, keywords are:, REM, , LET, , INPUT, , NOT, , OR, , AND, , PRINT, , END, , Constants, Constants, , identities in a QBasic that do not, change while executing, QBasic deals with 2 type constants:, Numeric constant:, It refers to numbers. Example,, 56,-89,45 etc., are, , String/Alphanumeric constant: The string, enclosed within, , Variables, Variables, , constants refer, , quotes. Example, "Name", "India", , etc., , characters, , memory location where data is, stored. Variables may be, , Numeric Variable: It, stores only, numbers. Arithmetic, them. For, example, A=9, , Alphanumeric/String, , Variable:, Arithmetic operations cannot, , It stores, any one, , be used, , (dollar) symbol to differentiate, , Rules for, , operations can be performed on, , character, , them. These variables, itself from numeric, , on, , A$="India", 1., , to valid set of, , program., , are, , names, to, broadly classified as: given a, , the $, , the, , or a, , are, , group of characters., , generally suffixed with, For example, LET, , variables., , Writing Variables, , A variable can be, , single letter or, , than one letter but not, digits but should always begin more, more than 40 or, with a letter. Thus, can be, B, A1, A2, variables but 2A, is, , 2., , 3., , invalid., , are, , completely, There should not be, any space in between the, is a valid variable, variable name. For, but BIG NAME isn't., example,, No, , BIGNAME, , punctuators or operators can be used with a, , valid variable., 4., , valid, , Reserved words cannot be used as, , the, , name, , of, , a, , variable. For, , example, A+B is not a, , variable., , QBasic Operators, Operators are special symbols which, generate a new value from one or more, Operators are of following types:, given values., 64
Page 3 :
ssignnent Operator, variable. The, , hand side is always a, ot assignment operator is =. The left, The symbol, variable on the LHS., stores it in the, and, RHS, the, on, value, of, the, takes a copy, , operator, , Meaning, , Example of Assignment Operator, , Stores, , A7, , A, 7 in the variable, , Stores, , ABC- PQR, , Arithmetic Operators, These operators work, , on, , or variable), numeric type data (constant, , and give a, , numeric, , type, , value., , Symbol, , Arithmetic Operator, Addition, Subtraction, , Example, to5, 3+2 will give result, result to 4, 8-4 will give, to, 4*3 will give result, , Multiplication, , 25/5 will give, , result to, , 5, , Division, , 25 will give result, , Exponent, , result in 5, 12 MOD 7 would, , MOD, , Modulation, , to 32, , calculation., , the operators performa, upon which, Operands are the quantities, and + is the operator., indicates 7 and 6 are operands, 7+6, the expression, , 1, , 12, , For example, , Hierarchy of Operators, mathematical expression, Whenever you have a, the order of calculation., Hierarchy, which operator will be, is necessary to understand, it, operator, one, having more than, calculation of arithmetic operators., below shows the order of, list, The, first., evaluated, evaluated first., maximum priority and will be, brackets have the, means, , 0, , 2, , gets the next priority., , 3., , /Or whichever, , 4., , MOD gets the next, , 5., , +Or whichevercomes first from left to right gets evaluated lastly., , comes, , first from left to right gets, , evaluated next., , priority., , Thus, 4+ (6-2)* 3 will resultin 16., , 65
Page 4 :
Relational Operators, Relational operators, , used for, , comparison. They are used to compare two quantitio., ties, and give logical value true (-1) or false (0)., are, , a, , Relational Operator, , Symbol, , Equal, , Example, 8-8 it gives result true or -1, , Less than, Less than, , 39 it gives result true, , equal, , or, , to, , 6<=6 it gives result true, , Greater than, , Greater than or, , equal, , 8>4 it gives result true, to, , 9<9 it gives result, , Logical Operators, operators are used to combine two or, , Logical Operator, , more, , Example, , NOT, , (A 3) AND (B=8), , QBasie Statements, A, , QBasic statement is a, , out one, , line of, , action. The, , -1, , or, , -1, , false or 0, , Result, , (A>3)OR (B=8), , AND, , -1, , relational expressions., , NOT (A>3), , OR, , -1, , or, , or, , 9>=9 it gives result true, , Not equal to, , These, , or, , True if (A>3) is false and, vice- versa., True if either or, both (A>3) and, , (B-8) is true., True if both, (A>3) and (B=8), , are, , true., , executable code. A statement, tells the, rule, , computer to carry, that a statement must, the statement. A grammatical, follow, is, called the syntax of, statement violating the, will, syntax, an, give error when executed., Statements can be entered in any order but, the execution will, be in, statement numbers. Statements, cannot be used as, ascending order of, variable, names., , LET Statement, It is an, , variable, , assignment statement and its function is to, name is, , written, , to, , and then the value., , Syntax:, , LET variable, , the, , assign a value, right of the LET statement, followed to a variable. The, by an equal to'=' sign, , name, , =, , Constant or variable or, , Example: LET N ="computer", , Variable N will store the string'computer, 66, , expression
Page 5 :
LETA2=30, Number 30 will be stored in the variable A2, , PRINT&tatement, This statement is used to display the output on the screen. The computer executes a PRINT, statement working from left to right. It displays numeric value, expressions, variables and, messages. The PRINT statement separates items by using commas and semicolons. It, , prints exactly what is given within quotes., Syntax: PRINT variable or constant or expression, , Example: PRINT X, The value stored in variable X will be printed on the screen., PRINT "X", , Character X will be, , printed on the screen, , PRINT 4+2, 6 will be printed on the screen, , INPUT Statement, The INPUT statement is a variation on the LET statement. In a LET statement, the value, given to a variable is fixed and you cannot change the value of the variable in that, particular program. The INPUT statement gives a choice to the user to enter any value, making the program interactive. The INPUT statement is used to accept values from the, user into variables and use these values for evaluation in a program. The variables in an, INPUT statement are separated by commas., , Syntax: INPUT "Message'; variable variable, Example: INPUT "Enter name and age"; N, Age, OR, INPUT a, b, c, , END Statement, Every program must have an END statement which is written as the last statement in a, program. END marks the end of the progranm. There should be only one END statement, given in a program as it signifies the termination of a program., , Syntax: END
Page 6 :
TOP Statement, , STOP statement makes, , the STOP statemens, Athough this may appear to be identical to the END statement,, , the computer halt the execution of a process at that particular point, where, ere itit has, has, rountered. There can be more than one STOP statement in a single program., , bee, , Syntax: STOP, , REN Satement, The REM statement is used to, give remarks to a program. It is like a program, aoes not take, part in the execution of a program and just give better, documentation, , heading, heading, It, , program., , Syntax: REM message, Example: REM **this is a, , program, , to, , of a, , add two numbers**, , CIS Statement, This statement is used, , to, , clear the screen., , Syntax: CLS, , Starting QBasie, To write, , QBasic program, , you need to download, You can download it, from, QBASIC.EXE, however, will not run on Windows 7., (See appendix at the end of the book to, learn how to run, QBASIC.EXE on Windows 7.), You will get the, following, , 'QBASIC.EXE., , the internet., , QBASIC window., , DOSBox, , 0.74. Cpu speed, , 3000 cycies,, , Frameskip 0, Prog, File Edit Uleu Search Atn, Debug Calis Options, tnttled, , Misroof t, , kckS IC 450 C Coporlyht, Mierositf Coratio 1985-1998, You can type, your program here., , 68
Page 7 :
Yile Eait Vieu, Save a P'rogram, , 1.Clickon, , File, , menu, , and select Save. The Save, , Ne Program, , dialog box opens., , 0pen Program, erge, SSave, , OR, , Save A, , Press Alt+ F to select File, Give, , 2, , menu, , Save All, , and then press S., , Create File, , to your program in the file name box and press Enter., saved with extension .BAS. Make sure that file should not, , Load File., Unload File., , a name, , The file willbe, have more than 8 characters., , Print.., , DOS Shell, , Open and Exit QBasie, 1., , To open a QBasic program click Open from File menu., , OR, Press Alt + F to select File menu and then press O., , Select the program from the list and press Enter, , 2, , To exit a QBasic window, click on Exit from File menu., , Run a Program, You can run a program in many ways to see the output:, , By pressing F5., By typing RUN in the immediate mode and pressing Enter., , eBy clicking Startfrom Run menu., , How to Write a Program in QBasic?, Let us write, , a, , very simple QBasic Program., , To start with a new program click,ón File menu and click on New option. You get a blank, screen where you can type your first program. The space where you type your program is, called the Editor window., 1., , Write a program to print "Hello", DOSBar, , In the blank screen type the following:, , 974 Cpi peet 90 cyces Frameskip 0Preg, , Tle AI Uieu oArck in Bebug Cails ptioRS, , thtit led, , PRINT "Helo", , PRINT "Hello", , END, , END, To run this program press the F5 key, For the program above you will get the output as, , 69
Page 8 :
hello, , Press any key to continue, , hen press any key to continue will bring back the QBasic Editor., 2, , Write a program to input two numbers from keyboard and print their sum., , INPUT A, INPUT B, LET C = A+B, , PRINT C, END, , INPUT A, , INPUT B, , LET C= A+B, , 121, , PRINT C, END, , Press any key to continue, , 3., , Write, , a, , program to input two numbers and print their, , INPUT "Enter number A"; A, INPUT "Enter number B"; B, , LET C= A*B, PRINT C, END, , product.
Page 9 :
NTIR A? 4, ENTER B? 6, 24, , INPUT Enter number AA, INPUT Lnter mumber, B, , LETC A', PRINT C, , END, , Write, , 4., , a, , program, , to, , print your name, age and address., , LET AS ="MEENA GUPTA", , LET B 15, LET CS, , ="12/6, CHANDIGARH", , PRINT "NAME IS "; A$, PRINT "AGE IS "; B, , PRINT "ADDRESS, , IS "; C$, , END, , oor4, CLS, LT AS MEENA GIPTA, , LET B-1, LAT E, 2/6.CHANDIGARH, PHIE, , A, , DOSo034 (puest 300 gos fane hg, NAME IS MEENA GUPTA, AGE IS 15, ADDRESS IS 12/6, CHANDIGARH, , FNT E1SE, ENDD, , Bugs, While programming, there might be situations when your program may not run correctly., This might happen because of errors in the program. Error in a program is called bug., , Whenever you get a bug in your program you start with the process of removal of such, , bugs. The process of removal of bug is called debugging, In QBasic, it highlights the position where the error has occurred and shows you what, error has occurred.
Page 10 :
009to0K Cospend 3000 yes Pranesup 0,Progran, , 2 e tait ie Search n Debug Calle Oytion, Detit led, , Highlighted Error, Esapected, 0X, , elp, , Error message, , Quick Review, o QBASIC stands for Quick Beginners All Purpose Symbolic Instruction, Code., , BAS is an extension of a QBasic file., The character set of QBasic consists of alphabets, numerals and, special characters., , oKeyword is a specific command used for programming., Variable is a name assigned to a memory location., o Constant is a number having a fixed value throughout the program., oLET is an assignment statement used to assign a value to a variable., o PRINT is a command to display results on the screen., , o INPUT is a statement to enter a value from keyboard., o END is a statement that terminates a QBasic program., o STOP is a statement that halts the execution of a program at the, , particular point where it is given., , oREM is a statement to write remarks in a program., o CLS is used to clear the screen., o, , Operators are symbols or commands that are used to evaluate an, expression.
Page 11 :
o Arithmetic operators are used to evaluate a mathematical expression., o Relational operators are used for comparison of two quantities., , o Logical operators are used to join two or more relations., , Bugs are, , errors, , that, , occur, , during the execution of a program, , and the, , process of removing is called Debugging., , ASSESSMENT, 1. Answerthe following questions:, Differentiate between constants and variable., State the rules you should follow while, , b., , Give the, , C ., , d., , Explain, , names., , hierarchy of the arithmetic operators., , the difference between LET, , an example., , logical, their meaning., , What, , using variable, , are, , statement, , and INPUT, , statement, , with the, , help of, , and, of logical operators, Name the different types, for?, used, operators, , variable., between Numeric and Alphanumeric, State the difference, , g., , 2., , What, , are, , the QBasic operators?, , Fill in the blanks:, is used to clearthe screen., , command, , ., , b.nailal, C., , operators, , PRTN, , are, , statement, , used tojoin two, , is used to, , d., , BASIC stands for Aeainre_P, , e., , called, A set of instructions is, , a, , relations., , display items on the screen., PwpO, , Su, , Cao, , _pirAOI9am, , The process of removing bugs is called e, , 3, , or more, , buain, , Write "Yes'or "No:, o, , a. Bugs are errors in, , programs.
Page 12 :
The INPUT statement is used to display the output on the screen., , Symbol is used to check the greater value of a variable., The shortcut, , key for opening file, , The extension of, , menu is Alt +, , F, , QBasic program file is .cpp., Alphanumeric variables ends with symbol., a, , ", , 4., , Give, a., , one, , d., e., , 5., , QBasic., , The command that is, used to accept values from the user., The shortcut, command for executing the QBasic, , The statement that, halts the execution of, , a. PRINT "Amit", , a, , following programs:, , PRINT " Rahul", , END, LET, , A, , 5, , LET B 6, , LET C A*B-25, PRINT C, END, C., , Alb, , The, , Give the output of the, , b., , ye, , word for the, following:, , command that is used to terminate a, program., b.b., The extension of a, program file in, , C., , ND, , LET A= 17, , LET B 3, LET C A/B, ., , LET D =A MOD B, , PRINT C, PRINT D, , END, , 74, , program., program., , END, TNPUT, STOR
Page 13 :
d, , PRINT "India", PRINT "Is", , PRINT "my", , PRINT "country", END, , LAB, , ACTIVITY, , Write the QBasic programs for the following:, , product of three numbers., of rectangle., b. Write a program to calculate the area, marks in four subjects, to print your name, roll number and, C. Write a program, 3 friends and print it., Write a program to input the name of your, , a., , d., , Write, , a, , program to calculate