The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by Oasis Publication, 2023-10-01 05:52:52

Computer 7 New

Computer 7 New

201 Oasis Radiant Computer Science, Book 7 Rules of developing flowchart • Analyse the input, process, storage and output of the programme. • Use standard symbols and arrowheads to the direction of flow of data and instructions. • Use unambiguous signs which can be easily understood by other programmers. • List activities inside each symbol. • Express all sections like input, process, logic and output. • It should not be any programming language oriented. Advantages of flowchart • It is an easy method of communication to represent the programme. • It serves as a guide for programme coding. • It is easier to understand than the programme and algorithm. • It is programme independent so applicable for all types of programming languages. • It represents all the components of a programme like input, process, logic and output which makes it easy for users to develop the programme. Disadvantages of flowchart • It cannot replace all types of logic used in a computer programme. • It takes a long time to represent all modules of a computer programme. • It is not suitable for big programmes. Some Examples of Algorithm and Flowchart 1. Draw flow chart to find area of rectangle. Algorithm Flowchart Step 1: Start the programme Step 2: Read L and B Step 3: Calculate Area = L × B Step 4: Print Result area Step 5: End the programme Start End L, B A A = L × B


Oasis Radiant Computer Science, Book 7 202 2. Draw flow chart to find simple interest and total amount. Algorithm Flowchart Step 1: Start the programme Step 2: Read P, T, R Step 3: Calculate Interest ð I = P*T*R/100 Step 4: Calculate amount A = P+I Step 5: End programme Key Points • Computer system needs instructions to perform different tasks. These instructions are called commands. • A programme is a collection of such commands in a systematic order to perform some work. • Programming is the technique or art to develop the programme. • Programmer is a person who develops the programme using standard programming language. • Algorithm is step by step description of programme in a systematic order. • Flow chart is the diagrammatic representation of programme using standard symbols. • There are two types of flowchart. Thy are called system flowchart and programme flowchart. • The module of programme is the functional procedure. • Programme design has three structures called sequence, selection and iteration (Loop). 1. Answer the following questions. a. What is command? b. What is programme? c. What is algorithm? d. What is flowchart? Start P, T, R I = P*T*R/100 A=P+I I, A End Exercise


203 Oasis Radiant Computer Science, Book 7 e. Write some advantages of flowchart. f. Explain the rules for designing flowchart. g. What are the limitations of flowchart? h. Draw the symbols of flowchart. 2. Draw a flow chart and write algorithm: a. To calculate simple interest. b. To find area of circle. c. To find perimeter of circle: d. To find volume. 3. Differentiate between: a. Programme and Command. b. Algorithm and flowchart. c. Input and output of programme 4. Fill in the blanks. a. A computer needs __________ to do some task. b. An __________ is step by step approach of programme. c. Flowchart is __________ representation of programme. d. Decision making symbol is used to process __________ expressions. e. __________ symbol defines the process used in the programme. 5. State True and False. a. Flowchart represents programme using-language. b. Algorithm is similar to steps written in English to develop programme. c. Looping structure repeats the logic according to criteria given in the programme. d. Processing symbol accepts input. e. Input and output symbol is same in flowchart. 6. Class Activities and Project Work a. Draw a flowchart to explain how you reach school from your house. b. Write algorithm to explain how your teacher instructs you in the class.


Oasis Radiant Computer Science, Book 7 204 Programming in QBASIC 16 Chapter 8 The concept of programming 8 Basic structure of QBASIC 8 Control statements of QBASIC Relational Operator Meaning = Equal to > Greater than < Less than < = Less than or equal to > = Greater than or equal to < > Not equal to Programme 1 LET X = 1 20 LET Y = X*X PRINT Y LET X = X+1 50 GO TO 20 END


205 Oasis Radiant Computer Science, Book 7 Introduction QBASIC is the most popular high level programming language. Various versions of BASIC have been developed by Microsoft Company. This language is quite simple to understand and has been adopted by most of the microcomputers. It is suitable for both mathematical and business problems. It is compatible with MSDOS environment and it has two basic files QBASIC. EXE and QBASIC.HLP. We can edit, debug and execute the programme using these two files. Starting QBASIC It occupies very low memory so it may be possible to run the system using a floppy disk also. It is compatible with MSDOS and have only two files QBASIC.EXE and QBASIC.HLP. Steps 1. Get MSDOS System and MSDOS Prompt C:\> 2. Change the Directory C:\> CD QBASIC C:\QBASIC> 3. Type QBASIC and press Enter C:\QBASIC>QBASIC enter When you are working with Windows system, Open QBASIC Folder on Windows. Double click over the QBASIC.EXE icon. We will get the QBASIC System as below. Press ESC key and get the programming window of the QBASIC and type the programme on the working window of QBASIC. QBASIC Programming 16 Chapter


Oasis Radiant Computer Science, Book 7 206 Structure of Basic To construct the QBASIC programme we have to arrange some standard elements. The elements are 4. Character set 5. Variables & constants 6. Arithmetic expressions 7. Relational or logical expressions 8. Keywords 9. Syntax 10. Library functions The Character Set QBASIC has the character set consisting of the following elements: a. Alphabets: A, B, C, ----. Z b. Digits: 0,1,2, ----, 9 and c. Special characters: + - * / ( ) . , $ ; : = > < “ ^ The symbol ^ (caret) is used to denote exponentiation operator, the symbol * (asterisk) is used to denote multiplication and the other symbols have their usual meanings. Constants and Variables A quantity in a computer programme which does not change its value during


207 Oasis Radiant Computer Science, Book 7 the execution of the programme is called a constant and the quantity which may change its values during the execution of the programme is called a variable. QBASIC allows the following constants: 1. Numeric constant 2. String constant Numeric constant is one that is formed by a sequence of digits 0,1,2, ---, 9 and may include a decimal point. A numeric constant known as number may be an integer or a real number: 383, + 57, 0, - 6.2 and 6.15E4 are valid numeric constants. The number 6.15E4, in fact, represents 6.15 × 104 . The notation E is used to represent exponential form. The number after E is the exponent which can be positive or negative. However, its length cannot exceed two digits. It is also important to keep in mind that 1. QBASIC does not distinguish between an integer and a fraction. 2. Commands are not allowed in a numeric constant. 3. The limit on the number of digits that can be used varies from computer to computer. Normally, a numeric constant can have up to a maximum of eight digits. A string constant consists of a sequence of characters. It must be enclosed by a quotation mark. This may contain blank space as a character but it should not include the quotation mark. String constants are used to represent nonnumeric quantities such as names, addresses, etc. For example, “SUYASH ADHIKARY”, “SUM = Rs 75”, “162” are a few valid string constants. In QBASIC, variables are also of two types. They are 1. Numeric variable 2. String variable Numeric variable can assume numeric value and is represented by an alphabet or an alphabet followed by another alphabet or a digit. For example A, C, A2, ABC, A6 etc, represent numeric variables. A string variable is represented by an alphabet followed by dollar ($) sign. It should be kept in mind that while constructing the string variable, dollar ($) should be the last character. For example, B1$, NAME$, BOOK1$, etc., are valid string variables.


Oasis Radiant Computer Science, Book 7 208 Arithmetic Expressions A QBASIC system can handle arithmetic expressions involving the five arithmetic operators + (addition), - (subtraction), *(multiplication), /(division) and ^ (exponentiation). The hierarchy of operations is as follows: 1. Exponentiation 2. Multiplication and division 3. Addition and subtraction Thus, in a particular arithmetic expression, the order of execution is as per this hierarchy, i.e. all exponentiation operations are performed first, and then multiplication/division and the addition/subtraction operations are the last to be carried out. Note that within a particular hierarchical group, the operations are executed form left to right. Normal hierarchy of operations can be altered by use of parentheses. The operations within the innermost parentheses are performed first and then the second innermost and so on. In addition to this hierarchy of operations, the following rules must be kept in mind in arithmetic expressions: 1. Two operations must not appear together. For example, C+-D, A/-C, etc are not permitted. 2. String constants and string variables should not be used in arithmetic expressions. For example, P+P$ is wrong. 3. When brackets are used, they must be used in pairs, i.e., every left bracket must be matched with a right bracket. 4. Denominator of an expression should not be zero. 5. Within a given pair of parentheses, the natural hierarchy of operations will apply. Let us take an example where we give QBASIC equivalents of a few algebraic expressions: Algebraic Expression QBASIC Equivalent 2A+B 2*A+B (A+B)/ (C-D) A(B+C) A*(B+C) B2 -4AC B^2-4*A*C


209 Oasis Radiant Computer Science, Book 7 Relational or Logical Expressions In the execution of programmes, it is sometimes desired to compare two numerical quantities (or sometimes string quantities) and take decisions on achieving certain conditions. Expressions written to compare two quantities using certain relational operators are known as relational expressions. A relational expression is formed by using any of the following relational operators: Relational Operator Meaning = Equal to > Greater than < Less than < = Less than or equal to > = Greater than or equal to < > Not equal to Logical Operators Like relational operators, QBASIC, also supports logical operators to perform logical operations on numerical values. Logical operators are used to connect two or more relations and return a TRUE or FALSE value to be used in a decision. The common logical operators are: 1. AND Conjunction 2. OR Disjunction 3. NOT Logical Negation Keywords Specific words which are not applicable to use as a variable on computer programme are Keywords. These are also called reserved words. For example INPUT, GOTO, PRINT etc. Syntax A set of rules and regulations that must be followed to construct the programme structure is called Syntax. It is a type of grammar to use the command and statement of a computer programme structure.


Oasis Radiant Computer Science, Book 7 210 Input/output and Programme Control Statements We have studied about the constants, variables and arithmetic operations used in the QBASIC language. We also saw how these constants, variables and arithmetic operations may be combined to form expressions and statements. Now we shall learn how the complete programme in QBASIC language can be written using the basic principles we have learnt so far. For a computer programme to be executed using a computer, we should give some values to the computer. Once the computer, completes the required computation work, we should ask the computer to produce the results in a proper format that can be understood by the common man. The INPUT/ OUTPUT statements of the QBASIC language achieve these two important functions. Besides these we shall also discuss the use of repetitive and branching statements used in QBASIC language. Getting Data into Memory As stated earlier, one of the basic features of a programming language is the input/output facility offered by it. QBASIC offers three such kinds of input statements. They are LET, INPUT and READ-DATA. These statements are basically used to assign values to various variables. LET Statements Syntax LET (variable name)=(Constants or Variables or expression) Example of LET statement LET X =5.32 ---> This is a numeric constant LET A = 6+9/3.2 --> This is an arithmetic expression LET Y = X+A --> This is also an arithmetic expression LET I =I+Y --> I and Y are variable names LET A$ = “LAMJUNG” --> String variable LET F$ = “99983” --> String variable LET A = 50 --> Numeric variable


211 Oasis Radiant Computer Science, Book 7 These are all valid LET statements. The value of the variable on the left hand side of the equal sign is assigned the value given in the right hand side of the equal to sign. Example LET A = 10 LET B = 5 LET A = A+B The example shows A is the variable name, which is, assigned the value 10 and B is the variable name assigned the value 5. In the memory the line number 15 gives an instruction by which A will be the address or name of a cell/location where the value 10 will be stored. Similarly B will be name or address of a cell/location where 5 will be stored. Line number 35 instructs that the values in the address A and B be added and the resulting value be kept at the cell/location whose address is A. INPUT Statement Syntax INPUT Variable,List... Example INPUT A,B,X INPUT A$,F$,N$ INPUT Y1,F$,C2,NB Whenever computer reads a line with the keyword INPUT it displays a sign? on the VDU and waits for the data. We have to give the values of the variable at this prompt through the keyboard. READ-DATA Statement Syntax READ Variable, List.... DATA Constant, List.... READ....DATA Statement


Oasis Radiant Computer Science, Book 7 212 REM Programme EXPLAINING READ....DATA STATEMENT READ X2,Y,Z1,K READ A,B3, C4, L DATA 8,9,13,15,16,51,30,92 END When the machine encounters the READ statement followed by the variables X2,Y,Z1,K it will collect from the DATA statements the values for these variables in the same order. A one-to-one correspondence exists in READDATA statement, i.e. X2=8, Y=9, Z1=13, K=15. In the next READ statement we have four more variables A,B3,C4,L. These values will follow in the same order and one-to-one correspondence after the earlier READ variables values, i.e., after 8,9,13,15, i.e., A=16, B3=51, C4=30, L=92. Since all the values in the DATA here are exhausted, another i.e. third, READ statement cannot be used until another DATA statement is included or more DATA is included in the above DATA. A DATA statement can be anywhere in the programme but must be before the END statement. It is a normal practice to keep all DATA statements together at the end of the programme before END statement, so that in case you want to alter any data at the end it will be easy. Data Block The QBASIC organises the contents of all the DATA statements into a data block. When the READ statement is executed a pointer moves along with data block starting from the first element: Example READ X1, Y1, Z1 READ N, M, L, K READ A READ X,Y


213 Oasis Radiant Computer Science, Book 7 DATA 122 DATA -35,49, -101 DATA -691,81 DATA 8,10.-5,5 END Print Statement The PRINT statement of QBASIC provides limited methods of controlling the alignment and spacing of printout in terminals. The general from of the PRINT statement is PRINT (Variable) separator (variable) separator or item...... The separation may be comma (,) or semicolon (;) The Semicolon (;) Control Programme LET S =1175.50 PRINT “TOTAL SALARY =”;S; “ RUPEES” END The output of the programme will be T O T A L S A L A R Y = 1 1 7 5 . 5 0 R U P E E S Using semicolon in a print statement, the items are close to each other, and as a result more items can be printed in one line, The Comma (,) Control PRINT “NUM”,”TEMP”,”SIZE”, REMARK” PRINT 65,-15.56,36,34 These two lines will be printed as follows NUM 65 TEMP -15.56 SIZE 36 REMARK 34


Oasis Radiant Computer Science, Book 7 214 The number of columns in each zone may be checked from the literature of manual for the given computer. Unconditional Go To Statement GOTO statement is used to transfer control from a statement, say S1 to another statement, say S2, generally, S2 does not follow S1 immediately in sequence. Syntax: GO TO n n is the line number of the statement where control will be transferred. Example 10 READ A,B,X,Y 20 GO TO 60 30 LET X = X*X+A 40 LET =Y*Y +B 50 PRINT X, Y 60 LET X =A*X 70 LET Y =B*Y 80 PRINT X,Y 90 DATA 6,3,4,5 100 END This shows after reading the values of A,B,X,Y, in line number 10, the control jumps to line number 60 and calculate X =A*X, Y =B*Y then prints X,Y in line number 80 and ends the programme. Line number 30,40,50 are never executed. If we want to execute 30,40,50 then our programme will add few more jump statements, i.e. few more GO TO statements. Consider the following programme 2.


215 Oasis Radiant Computer Science, Book 7 Example 10 READ A,B,X,Y 20 GO TO 60 30 LET X =X*X+A 40 LET Y =Y*Y+B 50 PRINT X,Y 55 GO TO 100 60 LET X =A*X 70 LET Y =B*Y 80 PRINT X,Y 85 GO TO 30 90 DATA 6,3,4,5 100 END Thus by introducing line number 55 and 85 all the statements are executed. So line number 30 and 40 prints X =X*X +A (value), Y =Y*Y+B (value) and line number 80 prints X = A*X and Y =B*Y Some Examples 1. Write a programme to calculate the sum of two numbers. REM “to calculate the sum of two numbers CLS INPUT “Enter the first number”; a INPUT “Enter the second number”; b sum = a + b diff = a - b PRINT “The sum is :”; sum PRINT “The difference is”; diff END 2. Write a programme to calculate simple interest and amount CLS INPUT “enter the principal amount”; p INPUT “enter the rate”; r INPUT “enter the time”; t i = (p * t * r) / 100


Oasis Radiant Computer Science, Book 7 216 PRINT “The Simple interset is:”; i a = p + i PRINT “The total amount to be paid is Rs:”; a END 3. Write a programme to calculate surface area and volume of a cone CLS INPUT “Enter the value of Pi”; pi INPUT “enter radius”; r INPUT “enter slant height”; l INPUT “Enter height”; h A = pi * r * l V = 1 / 3 * pi * r ^ 2 * h PRINT “The area is”; A PRINT “The volume is”; V END 4. Write a programme to find out total marks and percentage of any five subjects CLS INPUT “enter marks for Computer”; C INPUT “enter marks for Science”; S INPUT “enter marks for Social”; So INPUT “enter marks for English”; E INPUT “enter marks for Nepali”; N Total = C + S + So + E + N PRINT “The total marks is”; Total AVG = Total / 5 PRINT “The percentage is”; AVG END 5. Write a programme to accepts a temperature on centigrade and converts it into Fahrenheit. CLS INPUT “Enter the temperature on centigrade”, C F=C*(9/5)+32 PRINT “The Fahrenheit temperature is “,F END


217 Oasis Radiant Computer Science, Book 7 6. Write a programme find the area and perimeter of circle. CLS INPUT “Enter the radius of circle”, R A=3.1416 *R*R P=2*3.1416*R PRINT “ Area and perimeter is “,A,P END 7. Write a programme to find the sales amount of a computer according to following conditions. Purchase Amount, Profit = 20% CLS INPUT “ Enter purchase amount “, PAMT PROFIT=SAMT *20/100 SAMT=PAMT+PROFIT PRINT “ Sales Amount is “, SAMT END 8. Write a programme to find the area and perimeter of square. CLS INPUT “Enter Length”, L A=L^2 P=4*L PRINT “Area and perimeter “, A,P END 9. Write a programme to find the distance between two point (Coordinates). CLS INPUT “Enter coordinate of first point”, X1, Y1 INPUT “Enter coordinate of second point”, X2, Y2 D=SQRT ((X1-X2) ^2 + (Y1-Y2) ^2) PRINT” Distance between two points is”, D END 10. Write a programme to find the slope of given line. CLS Input “ Enter Y Intercept”,y Input “Enter X Intercept”,X


Oasis Radiant Computer Science, Book 7 218 Input “Enter Constant “, C M=(Y-C)/X Print “Our Slope is “,M End The Branching Statement IF... THEN The IF...THEN is a decision making statement, depending upon the decision, it can change the order of execution. It helps the computer to check whether a relation is TRUE or FALSE. Syntax: Line number IF (relational expression) THEN n where n is a line number or an instruction itself. If the relation expression is true then n will be executed otherwise the statement following this IF...THEN statement will be executed. Example Evaluate the expression Y =X2 for X =1,2,3... Programme 1 LET X = 1 20 LET Y = X*X PRINT Y LET X = X+1 50 GO TO 20 END In this process we see each time the line number 50 is executed the control is transferred to line number 20 unconditionally. Thus we have an infinite loop which is never ending. Computer goes round and round and never reaches END To stop the infinite loop we can add a condition, i.e. say if X is greater than or equal to 31 (X >=31) then the programme ends otherwise it keeps on printing the value of expression Y = X2 for X =1,2,3....30. Programme 2 LET X = 1 20 LET Y = X*X PRINT Y


219 Oasis Radiant Computer Science, Book 7 LET X = X+1 50 IF X> = 31 THEN 70 GO TO 20 70 END Thus we see that IF-THEN is one of the powerful statements which can stop or get the computer out of an infinite loop. Example Problem Write a programme to find out the auto fare depending on the Kilometers traveled. The minimum fare charged is Rs. 3.00. This minimum fare remains for 2 kms or less of travel. After 2 kms, the charges are 75 rp per kilometer. Programme 1 INPUT “KILOMETER”, K IF K < = 2 THEN PRINT “RS.3”: END LET CHARGE =3+ (K-2) *.75 PRINT CHARGE END If K is less than or equal to 2 (i.e., K<=2) then it will print Rs. 3 and (: END) means END Otherwise if K is not less than or equal to 2 then it will print calculated change. The Branching Statement IF...THEN...ELSE Another important statement is IF ...THEN...ELSE. Syntax: Line number IF (condition or relational expression) THEN (line number or Instruction) ELSE (line number or Instruction) The IF...THEN...ELSE statement is a decision making statement as it decides the path of the programme. It helps in making comparisons and testing whether a condition is true or not. IF is always followed by a valid BASIC condition or expression. If the condition is found true then the line number or Instruction after THEN is performed otherwise line number or instruction after ELSE is performed.


Oasis Radiant Computer Science, Book 7 220 Example Problem Ages of different students appearing in the Board examination are taken. If the age is below 17 the student is not eligible, otherwise he can appear in the Board examination. We are asked to write a programme for this problem. 10 INPUT “AGE”; A IF A>= 17 THEN PRINT “WELCOME FOR BOARD EXAMINATION” ELSE PRINT “YOU ARE NOT ELIGIBLE FOR BOARD EXAM.” INPUT “WANT TO INPUT AGAIN (Y/N)”; Y$ IF Y$ = “Y” THEN 10 END We will get the message AGE? We input the age through the keyboard, say 18. It tests whether A>17 or not. Since A=18>17 it prints WELCOME FOR BOARD We get a message WANT TO INPUT AGAIN (Y/N)? We input either Y or N. if input is Y then control goes to line number 10. Otherwise if input is N then control goes to the END. If input yes or Y then control will pass again line number 10, we give another age, say 13 Value of A (i.e. 13) is not greater than 17, therefore ELSE part will be executed and control will print YOU ARE NOT ELIGIBLE FOR BOARD EXAM. Problem To find the greatest number among any three numbers. 10 INPUT A,B,C 20 IF A>B AND A>C THEN 50 ELSE 30 30 IF B>A AND B>C THEN 60 ELSE 40 40 IF C>A AND C>B THEN 70 50 PRINT “A IS THE LARGEST NUMBER”: GO TO 80 60 PRINT “B IS THE LARGEST NUMBER”: GO TO 80


221 Oasis Radiant Computer Science, Book 7 70 PRINT “C IS THE LARGEST NUMBER” 80 INPUT “WANT TO INPUT AGAIN (Y/N)” ; Y$ 90 IF Y$ = “Y” THEN 10 100 END In line number 20,30,40 comparisons are made among the values of A,B, and C. AND is a logical operator which combines A>B and A>C. If both the relational expression preceding and following AND are true then the line number following THEN will be executed otherwise if one of the relational expression, A>B or A>C is true then the line number following ELSE will be executed. Same procedure follows for the next two IF statements also. If expression at 20 is found true, the line number 50 will print A is THE LARGEST VALUE and then GO TO 80 will be executed as the next statement to line number 50. If it is found false the control passes to statement 30 and the expression at that statement is checked. This part of the programme can also be written as follows: 50 PRINT “A IS THE LARGEST NUMBER” 55 GO TO 80 60 PRINT “B IS THE LARGEST NUMBER” 65 GO TO 80 Thus line number 50 and 60 in earlier programme of problem 2 is equivalent to line numbers 50,55,60,65 in the above programme statement. Syntax: For IF...THEN...ELSE with AND is given as; IF (Condition 1 or Relational expression) AND (Condition 2 or Relational expression) THEN (Instruction 1) ELSE (Instruction 2) Like AND another logical operator is OR. Syntax: for IF…. THEN...ELSE with OR is: IF (Condition 1 or Relational expression) OR (condition 2 or Relational expression) THEN (Instruction 1) ELSE (Instruction 2)


Oasis Radiant Computer Science, Book 7 222 This means if either of the conditions (or relational expression) followed or preceded by OR is true then the instruction 1 is executed. If neither of them is true then instruction 2 is executed. Some Examples 1. Write a programme to accept a number and check it is positive or negative number. CLS INPUT “ Enter a number ”,N IF N<0 THEN PRINT “Negative Number” ELSE IF N>0 THEN PRINT “Positive number” ELSE PRINT “ Zero Number” END 2. Write a programme to accept a day code and display the name of the days according to day code. CLS INPUT “ Enter day Code ”,DC IF DC=1 THEN PRINT “ Sunday” ELSE IF DC=2 THEN PRINT “Monday” ELSE IF DC=3 THEN PRINT “Tuesday” ELSE IF DC=4 THEN PRINT “Wednesday” ELSE IF DC=5 THEN PRINT “Thursday” ELSE IF DC=6 THEN


223 Oasis Radiant Computer Science, Book 7 PRINT “Friday” ELSE IF DC=7 THEN PRINT “Saturday” ELSE PRINT “Invalid Code” END 3. Write a programme to calculate the tax according to the income as below; (Sno, Name, Income) If Income>=50000, tax= 15 % If Income>=25000 and less than 50000, tax= 10 % Else No Tax CLS INPUT “Enter the Serial No, Name and Income”, SNO, NAME$, I IF I>=50000 THEN TAX= I*15/100 ELSE IF I>=25000 THEN TAX = I*10/100 ELSE TAX= 0 PRINT “Serial No, Name and Tax: “, SNO, NAME$, TAX END Loops “Loops” make it easier to do an action multiple times. There are at least four types of loops: Loop is defined as the repeated execution of the statement that performs the work until the condition will satisfy. Popular lops is FOR...NEXT, IF... GOTO, WHILE...WEND and DO...LOOP


Oasis Radiant Computer Science, Book 7 224 FOR..NEXT LOOP We have already seen that a loop can be built in QBASIC by using the IFTHEN and GOTO statements. When it is known in advance how many times the loop must be repeated the statement FOR...NEXT is the most effective statement. A loop is built up by FOR and ended by NEXT. Syntax: Line number FOR I = M TO N STEP J Loop - - - - - - - - - - - - Line number NEXT I The numeric variable name following FOR is called the control variable or loop variable. M and N are numeric constants where M gives the initial or starting value of the loop and N gives the final value, J followed by keyboard STEP gives the increment in M till N is reached. The increment can be negative also. M,N,J can be numeric variable names. In such cases their numeric values should be assigned before the starting of the loop, i.e. before coming to FORTO statement. They key word NEXT should have the same control variable I followed by it. Thus one loop can be started with FOR-TO and ended with NEXT. Inside one FOR-TO ...NEXT loop there can be more FOR-TO...NEXT loop. But once a FOR-TO...NEXT is inside another FOR-TO...NEXT, it should remain completely inside the former loop. Such FOR-TO...NEXT loops are called Nested loops. In the absence of the STEP clause, the increment is assumed to be 1. Example REM display the first ten natural umbers CLS


225 Oasis Radiant Computer Science, Book 7 FOR I = 1 TO 10 PRINT I NEXT I END Output 1 2 3 4 5 6 7 8 9 10 Example REM display odd numbers upto 20 CLS FOR I = 2 TO 20 STEP 2 PRINT I NEXT I END Note : STEP 2 is used to step up the number with the value 2 like 2, 4,6,8,10,12,14,16,18,20 IF...GOTO LOOP This programme uses IF...GOTO to create a loop: x = 10 start: PRINT x x = x + 1 (This adds 1 to x) IF x < 15 THEN GOTO start


Oasis Radiant Computer Science, Book 7 226 Output: 10 11 12 13 14 WHILE...WEND The WHILE...WEND commands continue a loop until a specified expression is false. To use WHILE...WEND: 15. Place an expression after WHILE 16. Enter a list of commands 17. Place WEND at the end Run the following: x = 10 WHILE x < 15 PRINT x x = x + 1 WEND Output: 10 11 12 13 14 DO...LOOP DO...LOOP is exactly the same as WHILE...WEND, except it has at least two slight advantages. With DO...LOOP you can:


227 Oasis Radiant Computer Science, Book 7 18. Loop until an expression is true 19. Loop at least one time regardless of whether the expression is true or not. To use DO...LOOP: 20. Specify whether the loop continues “while” the expression is true or “until” the expression is true, using the WHILE and UNTIL statements, respectively. 21. Place an expression after WHILE/UNTIL 22. Enter a list of commands 23. Place LOOP at the end The following uses the WHILE statement: x = 10 DO WHILE x < 15 PRINT x x = x + 1 LOOP This programme uses the UNTIL statement: x = 10 DO UNTIL x = 15 PRINT x x = x + 1 LOOP END They both output: 10 11 12 13 14 If you place the expression at the end of the loop instead, the programme goes through the loop at least once. x = 32 DO PRINT x


Oasis Radiant Computer Science, Book 7 228 x = x + 1 LOOP WHILE x < 5 This is the output because the loop was only gone through one time: 32 Solved Examples 1. Write a programme to find the sum of first ten natural umbers REM sum of first ten natural umbers CLS FOR I = 1 TO 10 sum = sum + I NEXT I PRINT “The sum is”; sum END 2. Write a programme to print first 10 odd numbers REM to print first 10 odd numbers CLS FOR i = 1 TO 20 STEP 2 PRINT i NEXT i END 3. Write a programme to print the odd numbers and sum upto a given number REM “to print the odd numbers and sum upto a given number” CLS INPUT “Enter any number”; n sum = 0 PRINT “The odd numbers are”; FOR i = 1 TO N STEP 2 PRINT i;


229 Oasis Radiant Computer Science, Book 7 sum = sum + i NEXT i PRINT PRINT “The sum of odd numbers is:”; sum END 4. Write a programme to display the series 2, 4, 8, 16, 32 ....10 terms: REM to display the series 2, 4, 8, 16, 32 ....10 terms: CLS LET a = 2 FOR x = 1 TO 10 PRINT a LET s = s + a LET a = a + 2 NEXT x PRINT “The sum is:”; s END 5. Write a programme to find the area of a triangle REM to find the area of a triangle CLS INPUT “Enter the first side:”; a INPUT “Enter the second side:”; b INPUT “Enter the third side:”; c IF (a + b < c) OR (a + c < b) OR (b + c < a) THEN PRINT “A triangle is not possible” ELSE PRINT “A triangle is possible” END IF s = (1 / 2) * (a + b + c) area = SQR(s * (s - a) * (s - b) * (s - c)) PRINT “The area of a triangle is:”; area END


Oasis Radiant Computer Science, Book 7 230 6. Write a programme to find the cube of the first 20 numbers REM to find the cube of the first 20 numbers CLS FOR i = 1 TO 20 PRINT i ^ 3 NEXT END 7. Suppose we want to print the output in the following format: * * * * * * * * * * * * * * * FOR S=5 TO 1 STEP-1 FOR X=1 TO S PRINT “*” ; NEXT X PRINT NEXT S END 8. Suppose we want to sum to following series: 1,2,3,4_ _ _ _ 100. LET S=0 FOR I=1 TO 100 LET S=S+I NEXT I PRINT “ SUM OF SERIES =” ; S END


231 Oasis Radiant Computer Science, Book 7 Key Points • QBASIC is most popular high level programming language developed by Microsoft Company. • QBASIC has two basic files QBASIC.EXE and QBASIC.HLP • QBASIC is applicable for all types of applications so these are called general purpose programming language. • Basic components of QBASIC are: character set, variables and constants, logical expressions keywords, syntax and library functions. • Mathematical operation performs mathematical work using +,-,*,^ etc. and logical operation performs the work using OR and AND operators. • Read – Data statement is used to store and display data. • Functions are built in operator that performs the work like a command. • To perform work according to logic and condition we have to use IF THEN ELSE condition. • Repeated logic that performs the expressions until the condition will satisfy is called Loops. 1. Answer the following questions. a. What is QBASIC? b. Write the files used on QBASIC? c. What is variable? d. What are operators? Write its types. e. Write the use of logical and mathematical operators. f. What is loop? g. List any three types of looping structure with syntax. h. What is keyword? i. Write the advantages of QBASIC. j. How to start QBASIC on MSDOS? Exercise


Oasis Radiant Computer Science, Book 7 232 2. Write short notes on a. Key word b. Character set c. Loop d. Control statement e. Library function. 3. Differentiate between a. Variable and constant b. General word and keyword c. Loops and general logic d. Read and Input e. While.. Wend and For I Loop 4. Write a programme for the following. a. To calculate area of circle. b. To find simple Interest and total amount. c. To find volume d. To find perimeter of square. e. To find area of rectangle. f. To display even numbers from 10 to 100. g. To write a programme to calculate tax according to income. (Input Income, If Income>=15000, Tax= 20% else tax=5%) h. To display your name twenty times using the FOR … I Loop. i. To accept rollno and name using Read and Data statement. j. To convert the temperature from Fahrenheit to centigrade 6. Class activity and project work. a. Make a list of command to handle input and output of QBASIC application. b. Prepare a result analysis using standard conditions. ( Use Rollno, Name and five subject marks) c. Make a project report by collecting any twenty programmes and show this to your teacher.


Click to View FlipBook Version