Qbasic Tutorial 3

Document Sample
Qbasic Tutorial 3 Powered By Docstoc
					                                         LESSON 26
            INPUT/OUTPUT AND PROGRAM CONTROL STATEMENTS


26.1      INTRODUCTION
In the previous lesson, we studied about the constants, the variables and the arithmetic
operation used in the BASIC language. We also saw how these constants, variables and the
arithmetic operations may be combined to form expressions and statements. Now we shall
learn how the complete program in BASIC language can be written using the basic principles
we have learnt so far.


For a computer program 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 BASIC language achieve these two important
functions. Besides these we shall also discuss the use of repetitive and branching statements
used in BASIC language.


26.2   OBJECTIVES
At the end of this lesson, you should be able to:
      understand clearly the use of INPUT/OUTPUT statements such as LET, READ-
       DATA, INPUT, PRINT, PRINT USING etc.
      learn the use of LOOPING and CONTROL statements.
      develop BASIC programs using the various basic commands offered by the language


26.3   INPUT-OUTPUT STATEMENTS
It should be always kept in mind that computer cannot do anything by itself. It will act
according to the instructions supplied to it by the user. To achieve the required output in the
desired format, a user has to provide correct data and a series of processing instructions in
proper order. In this section, we will discuss various input/output statements provided in
BASIC language.


26.3.1 Getting Data into Memory
As stated earlier, one of the basic features of a programming language is the input/output



                                               1
facility offered by it. BASIC 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.


26.3.2 LET Statements
Syntax:
Line number LET (variable name)=(Constants or Variables or expression)
Example of LET statement


10     LET X =5.32 ---> This is numeric constant
20     LET A = 6+9/3.2--> This is an arithmetic expression
30     LET Y = X+A --> This is also an arithmetic expression
40     LET I = 2-->numeric constant
50     LET I =I+Y -->I and Y are variable names
60     LET A$ = "NEW DELHI”--> String variable
70     LET F$ = "99983" -->String variable
80     LET A = B--> numeric variable




These are all valid LET statements. The value of the variable on the left hand side of the
equal to sign is assigned the value given in the right hand side of the equal to sign


EXAMPLE
15     LET A =10
25     LET B =5
35     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.




                                                2
                                            Memory location          Memory location
                                                    A                        B
                 Execution of line 15               10                  Not known
                 Execution of line 25               10                       5
                 Execution of line 35               15                       5




Note here that after execution of line 15 it has been shown “Not known” in location B,
because in this program we have not given any value to the variable name B on line number
15. It's value is given in the next line number 25. Since variable A has been already given the
value 10 that continue to stay in A until we assign a different value to A which replaces the
earlier value. So in the next line number 35, A+B the values of A and B given before are
added and put in the location A Now the latest value of A is 15 and that of B will remain as 5.
Let us suppose that line 35 has been replaced as
35       LET C = A + B
Here computer will add the values of A and B, i.e. 15 and put it in a third location C.


                              Location A                Location B               Location C
Line number 15                    10                    Not known                Not known
Line number 25                    10                        5                    Not known
Line number 35                    10                        5                       15


26.3.3 INPUT Statement
Syntax
Line number INPUT Variable,List...


EXAMPLE
12 INPUT A,B,X
16 INPUT A$,F$,N$



                                               3
20 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.


26.3.4 READ-DATA Statement
The LET and INPUT statement have certain limitations. By a LET statement we can assign
one value for each variable and if we have to get 10 values for 10 variables we have to use 10
statements with LET. Also if a value is assigned through a LET statement its value can be
changed only by replacing that assignment statement by another assignment statement. In
case of an input statement, the data is provided at the prompt at execution time. If there is a
large amount of data to be processed, it is inconvenient to key in the entire data during the
execution of the program. In such cases READ statements are found useful. READ statement
will always have a DATA statement along with it.
Syntax
Line number READ Variable, List....
Line number DATA Constant, List....
Example of a READ....DATA Statement


1        REM PROGRAM EXPLAINING READ....DATA STATEMENT
5        READ X2,Y,Z1,K
9        READ A,B3, C4, L
22       DATA 8,9,13,15,16,51,30,92
30       END


When the machine encounters the line number 5 with 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 READ-DATA statement, i.e. X2=8,
Y=9, Z1=13, K=15.
In the next READ statement in line number 9 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.



                                              4
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 list in line number 22. A DATA statement can be anywhere in the program but
must be before the END statement. It is a normal practice to keep all DATA statements
together at the end of the program before END statement, so that in case you want to alter
any data at the end it will be easy.


26.3.5 DATA BLOCK
The BASIC organises the contents of all the DATA statements into a data block. When the
READ statement are executed a pointer moves along with data block starting from the first
element:
EXAMPLE
1      READ X1, Y1, Z1
7      READ N, M, L, K
12     READ A
19     READ X,Y
24     DATA 122
26     DATA-35,49, -101
28     DATA -691,81
30     DATA 8,10.-5,5
40     END


DATA BLOCK
               Value           Pointer            Variable
               122             <---        X1
               -35              <---       Y1
                49             <---        Z1
               -101             <---       N
               -691             <---       M
                81              <---       L
                8              <---        K
                10              <---       A
                -5             <---        X



                                            5
                5           <---          Y




Now look at the following some examples where all the programs do exactly the same work.


(a)    10      DATA "RAMA","DELHI",100,200,300
       20      READ A$,B$,A, B, C
       30 - - - - -
       40      -----
       --------------
       80      END


(b)    10      READ A$,B$
       20      READ A,B,C
       30      ------
       40 - - - - - -
       60      DATA "RAMA","DELHI"
       70      DATA 100,200,300
       100     END


(c)    10      DATA "RAMA","DELHI"
       20      DATA 100
       30      DATA 200
       40      DATA 300
       50      READ A$,B$
       60      READ A,B,C
       -------------
       100     END


(d)    10      READ A$
       20      READ B$,A,B,C
       30      DATA "RAMA"
       40      DATA "DELHI"



                                            6
         50     DATA 100,200,300
         -------------
         100    END
In all these cases A$ is assigned the value RAMA and B$ the value DELHI.All the numeric
variables A,B,C are assigned the values 100,200,300 respectively.


So we note that for READ....DATA statement we should follow the following


        There could be any number of READ and DATA statements.
        As many variables are read by READ statement, at least the same number of values
         should be there in the DATA statement. It may be more. If the number of values in
         DATA is less, then there will be an error message OUT OF DATA given by the
         computer.
        The type of variables (i.e., numeric, string) in the READ statements containing many
         variables.
        Although normally we keep all the DATA statements at the end of the program just
         before END statement, DATA statement can occur anywhere in the program but
         before END statement.


26.3.6 Restore Statement
The RESTORE statement beings the pointer to the first value of the DATA statement
regardless of the current position of the pointer.
Syntax
Line number RESTORE
For example,
    10 RESTORE
The RESTORE statements may be used anywhere in the program, of course, before the END
statement.
The following example will clearly illustrates the use of RESTORE statement
10       READ X,Y
20       DATA 10,12,-7,,3.2, "PANKAJ"
30       RESTORE
40       READ A,B,C,D,N$



                                                7
50     END
When the statement number 10 is executed, the variables X and Y are assigned the values of
10 and 12, the position of the pointer at this stage is shown below:




       10       12      -7      3.2 "PANKAJ"
                          (Pointer)
        (Position of pointer before execution of RESTORE statement)


When the statement 30 RESTORE is executed, the pointer is brought back to the first data
value of data statement as shown below:


10              12             -7              3.2     "PANKAJ"
  ( Pointer)


Now, the execution of the statement number 40 against the value to A,B,C,D and N$ will be
as shown below:
                       A               10
                       B               12
                       C               -7
                       D               3.2
                       N$              PANKAJ


The RESTORE statement can be used in the following forms also:


                Statement number RESTORE *
                                and
                Statement number RESTORE $


If the key word RESTORE is followed by asterisk (*), then only numeric pointer is reset as
shown in the previous example. However, if the key word RESTORE is followed by a dollar
sign ($), then only string pointer is reset to the first string data. The simultaneous use of both
asterisk (*) and dollar sign ($) is not allowed.



                                                   8
For example, if a BASIC program contains the following statements,


              10     READ A,B,N$,M$
              20     -------
              30     -------
              40     RESTORE *
              50     READ P,Q,A$,B$
              60     -------
              70     -------
              80     DATA 5,8,"DINESH",UPMA",10,12,"RAJESH","SANJU"
              90     END


When the statement 10 is executed, A,B are assigned values 5 and 8 whereas the variables N$
and M$ are assigned the values DINESH and UPMA respectively. The execution of
statement 40, restores the numeric pointer to the first numeric value in the DATA statement
i.e. to the value 5. The execution of the statement 50, assigns the value 5,8, RAJESH and
SANJU respectively to the variable P,Q,A$ and B$. The values DINESH and UPMA are
being ignored. Similarly, the execution of RESTORE$ can also shown by taking similar
example.


Consider another example
              10     READ A,B,N$,M$
              20     RESTORE $
              30     READ P,Q,A$,B$
              40     DATA 5,8, "DINESH", "UPMA", 10,12,"RAJESH",SANJU"
              50     END
Here the statement RESTORE$ is used. The values assigned to the variable A,B,N$ and M$
are same as in the previous example. However, when statement 20 is executed, the string
pointer is reset to the first string data namely DINESH. The execution of the statement 30
would assign the values 10 and 12 to the variables P and Q and then the pointer moves back-
ward to assign the values DINESH and UPMA to the variables A$ and B$


26.4   PRINT STATEMENT
The PRINT statement of BASIC provides limited methods of controlling the alignment and


                                            9
spacing of printout in terminals. The general from of the PRINT statement is


Line number PRINT (Variable) separator (variable) separator or item......


The separation may be comma (,) or semicolon (;)


26.4.1 The Semicolon (;) Control
         Program
               10      LET S =1175.50
               20      PRINT "TOTAL SALARY =";S; " RUPEES"
               30      END


The output of the program 1 will be as
 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,
26.4.2 The Comma (,) Control
100      PRINT "NUM","TEMP","SIZE", REMARK"
110      PRINT 65,-15.56,36,34
These two lines will be printed as follows
1              17                    33                49                   65           85
    Zone 1          Zone 2               Zone 3             Zone 4          Zone 5
    NUM             TEMP                 SIZE               REMARK
    65              -15.56               36                                 34


The number of columns in each zone may be checked from the literature of manual for the
given computer.


26.4.3 Print Using Statement
The PRINT USING statement is included in most versions of microcomputer BASIC. It
allows printed output to be formatted, giving the appearance of each data item. This PRINT
USING statement can specify both string and numeric data.



                                                  10
There are several different ways to format. The most commonly used are given below:
100                     PRINT USING                      "##. ##";        A,B,C,
Line number             PRINT USING                      Format String   Numeric Variables
The format string "##. ##", which a numeric field containing a decimal point with not more
than two digits on each side.
What will be the output of the following program?
10      LET A =17.32
20      LET B = -5.38
30      LET C =40
40      PRINT USING "##. ##",A,B,C,
50      END
OUTPUT
        17.32           -5.38           40.00


If string values are used instead of numeric values, the format string for PRINT USING will
be as follows:
100 PRINT USING "!!!!!!" ; N$
If N$ ="UNITED"
Then the above PRINT USING statement will give output as:
UNITED
Observe, there are six alphabets and we have used six exclamation sign (!). Each exclamation
sign (!) is used to represent each character of a string.
Note: Some computers use # for presenting a character.
Example
If N$ = "FROG" then
(a) 100 PRINT USING "!!!!";N$ gives output as bbFROG
(b) 100 PRINT USING ">!!!!!!";N$ gives output as
(> character is used for the right justification of a string.)
Since "!!!!!!" has six character representations and FROG has only four characters,">" sign
gives characters b b in the left side as the PRINT USING with > gives right justified. Space
characters (b b ) are used to fill left space.
With the same explanation
100 PRINT USING “<!!!!!!”; N$ gives output FROG b b


                                                  11
(< is for left justified), Left justified space filled to right.
Note: b is used as space character.
26.4.4 TAB FUNCTION
The TAB (abbreviation of TABULATION) function is a very important function because it
enables the user to exercise exact control over the print positions. The use of a COMMA and
a SEMICOLON to space out the output is not so flexible as the TAB function.


TAB (n) moves the printer head to the nth column and printout of any data starts from that
column.


Syntax:
Line number PRINT TAB (N); X


Where N is a positive number, a variable or an expression having positive value and X shows
output data
The execution of TAB function is explained below:-
(a) If N is an integer, the printer head moves to the Nth column and printing of the value of X
starts from that column
(b) In case N is a variable, not having the internal value, it is rounded and the printer head
moves according to this value.
For example,             5    X = 12
                         10    K = 5.3
                         20    PRINT TAB(k) ; X




(c) On the other hand, if N is an expression then first of all, the expression is evaluated and
rounded, if necessary. The printer head move based on the value of this expression.
The backward movement of the printer head is not allowed.
For example, consider the statement
10 PRINT TAB (25); "DINESH"; TAB(20); "RAJESH"
Here TAB (25) instructs the computer to start printing DINESH from column number 25
onwards. Then TAB (20) instructs the computer to write RAJESH from column 20. Since the
printer head is already beyond the column 20 and backward movement is not allowed, so
TAB (20) is ignored and the output is same as if TAB (20) was not present. The above


                                                   12
statement is equivalent to
10 PRINT TAB (25); "DINESH"; "RAJESH"
Consider another example, where TAB function is ignored
20 PRINT "10.3", TAB (10); "15.6"
The comma preceding the function TAB (10) Positions the printer head to the column 16 so
that the function TAB (10) becomes redundant as printer head cannot move backward and
hence it is ignored. The value 15.6 will be printer from column 16 onwards.
The use of TAB function is an elegant method to prepare patterns, producing tables and to
plot graphs etc.


26.5   PROGRAM CONTROL STATEMENTS # # THEIR SYNTAX AND USE
We have already seen a few BASIC statement like REM, (in Lesson 1),and
LET,INPUT,READ,DATA etc in this Lesson itself. The other few BASIC statements and
their explanations are given in this section. We will discuss them in details using different
program.


26.5.1 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: Line number GO TO n
 n is the line number of the statement where control will be transferred.




Program 1
        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


                                              13
          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
program. Line number 30,40,50 are never executed. If we want to execute 30,40,50 then our
program will add few more jump statements, i.e. few more GO TO statements.
Consider the following program 2.
Program 2
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




                                      IN-TEXT QUESTION1


1.       Fill in the blanks


(a) In the LET statement, the value of a variable on the ......... hand side of the equal to sign is
     assigned to the variable given in the .......... hand side of the equal to sign.



                                                  14
(b) READ statement cannot be used without a ........... statement.
(c) DATA statement can occur anywhere in the program, but must be before the ...........
     statement.
2. State the difference between READ and INPUT statement?
3. What is the use of PRINT USING statement?
4. What is the use of RESTORE statement?
5. Define briefly the use of TAB function.




26.5.2 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 2
Evaluate the expression Y =X2 for X =1,2,3...

Program 1
10      LET X = 1
20      LET Y = X*X
30      PRINT Y
40      LET X = X+1
50      GO TO 20
60      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


                                               15
>=31) then the program ends otherwise it keeps on printing the value of expression Y = X2
for X =1,2,3....30.


Program 2
10             LET X = 1
20             LET Y = X*X
30             PRINT Y
40             LET X = X+1
50             IF X> = 31 THEN 70
60             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 3
Problem 1
Write a program 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.
Program 1


10             INPUT "KILOMETER", K
20             IF K < = 2 THEN PRINT "RS.3” : END
30             LET CHARGE =3+ (K-2) *.75
40             PRINT CHARGE
50             END


Here in line number 20, if K is less than or equal to 2 (i.e., K<=2) then it will print Rs. 3 and
(: END) means END, in continuation to the line number 20 next statement is END.
Otherwise if K is not less than or equal to 2 then line number 30,40 then 50 will be executed.
26.5.3 The Branching Statement IF...THEN...ELSE
Another important statement is IF ...THEN...ELSE.
Syntax:


                                               16
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
program. 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.


EXAMPLE 4
Problem 1


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 program for this problem.


Program 1
10             INPUT “AGE”; A
20             IF A>= 17 THEN 30 ELSE 50
30             PRINT “WELCOME FOR BOARD EXAMINATION”
40             GO TO 60
50             PRINT “YOU ARE NOT ELIGIBLE FOR BOARD EXAM.”
60             INPUT “WANT TO INPUT AGAIN (Y/N)”; Y$
70             IF Y$ = “Y” THEN 10
80             END


The line number 10 will cause the message in screen AGE? We input the age through the
keyboard, say 18. Line number 20 tests whether A>17 or not. Since A=18>17 line number 30
is executed.
Line number 30 prints WELCOME FOR BOARD
Line number 40 causes the control to pass to line number 60.
Line number 60 causes the message WANT TO INPUT AGAIN (Y/N)? We input either Y or
N.
In line number 70 if input is Y then control goes to line number 10. Otherwise if input is N


                                             17
then control goes to the line number 80 i.e. END.
Now if in line number 70 we input Y then control will pass again line number 10, we give
another age, say 13
In line number 20 value of A (i.e. 13) is not greater than 17, therefore ELSE part will be
executed and control will go to line number 50. Line number 50 will print YOU ARE NOT
ELIGIBLE FOR BOARD EXAM. Then line number 60 as before will be executed. In this
way a large number of students age can be tested. When we want to stop we should input N
in line 60 for Y$.


Problem 2


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
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 program can also be written as follows:



                                              18
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 program of problem 2 is equivalent to line numbers
50,55,60,65 in the above program 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)
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.
26.5.4 The Looping Statement FOR-TO...NEXT
We have already seen that a loop can be built in BASIC by using the IF-THEN and GOTO
statements.
When it is known in advance how many times the loop must be repeated the statement FOR-
TO...NEXT is the most effective statement.


A loop is built up by FOR-TO 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


                                              19
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 FOR-TO 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 5
Problem 1
Suppose we want to print the output in the following format:
*****
****
***
**
*


Program 1


10     FOR S=5 TO 1 STEP-1
20     FOR X=1 TO S
30     PRINT "*" ;
40     NEXT X
50     PRINT
60     NEXT S
70     END


This program contains two loops: the outer loop is from the line number 10 to 60 and inner
loop from line numbers 20 to 40. In line number 10, initially S is assigned a value 5. Since


                                               20
the value of S is greater than 1, control is transferred to line number 20, which causes the
inner loop to execute 5 times resulting into printing of 5 stars (*) in one row. The statement at
line number 50 will transfer the printer control to the beginning of the next line. When line 60
is encountered, the control goes back to line number 10. Now the value of S becomes 4 and
once again the inner loop is executed 4 times resulting in printing of 4 (*) stars in second
row. This process will continue till the value of S becomes 1. After that it will come to end.


Problem 2


Suppose we want to sum to following series:
1,2,3,4_ _ _ _ 100.


Program 2
10      LET S=0
20      FOR I=1 TO 100
30      LET S=S+I
40      NEXT I
50      PRINT " SUM OF SERIES =" ; S
60      END


26.5.5 The Multiple Branching Statement ON... GOTO


The GOTO statement allows only one transfer point, IF...THEN allows two transfer points,
the ON...GOTO can have more than two transfer points, thus providing multiple branching
facility.
Syntax: line number ON (Numeric variable or expression) GOTO n1,.n2,n3,..
The expression is a valid BASIC expression and n1,n2,n3,.... are the statement numbers or
line number where the control will be transferred.


If ON is followed by numeric variable name and if the value of numeric variable is 1 control
is transferred to n1, if 2 then n2, if 3 then n3 and so on.


If ON is followed by an expression by an expression then it is evaluated and truncated to an
integer value. Control is transferred in the same manner as discussed above.


                                                 21
If the integer value is less than or more than number of line numbers present in the list then
an "OUT OF RANGE" error message will be given by the computer.


EXAMPLE 6


Problem
Write a program to add, subtract, divide and multiply two numbers A and B.
Program


5      INPUT A,B
10     INPUT " 1-ADD, 2-SUB, 3-MUL, 4-DIV"; N
20     ON N GOTO 30,40,50,60
30     PRINT A+B : END
40     PRINT A-B : END
50     PRINT A*B : END
60     PRINT A/B       : END


When line 5 is executed, there will be question mark (?) then you have to input the values of
A and B through keyboard. Line number 10 will cause a message on the screen 1-ADD, 2-
SUB, 3-MUL, 4-DIV and then display of question mark (?) for N.
If we input through keyboard 1 then N takes the value 1.If we enter 2 or 3 or 4,N will take 2
or 3 or 4 accordingly. If the value of N is


1      control is transferred to 30,
2      control is transferred to 40,
3      control is transferred to 50,
4      control is transferred to 60


On execution of line number 30 addition of A and B will be displayed on the screen and
program will come to an end. Similarly, in line number 40,50,60 printing of the result of
subtraction, multiplication and division will be displayed respectively.




                                              22
                   IN-TEXT QUESTIONS 2
1. Write a LET statement for each of the following:
       (a) To assign the value 5.2 to the variable A
       (b) To assign the string PENTIUM-SYSTEM TO the variable B$
       (c) To assign the value of the variable C to the variable D
       (d) To increase the value of the variable E by 1
       (e) To assign the value equal to three times the value of the variable P to the variable Q


2.          In the following FOR statements, write the values taken by the control variable
       (a) 10             FOR      L=    3 TO 12          STEP 2
       (b) 20             FOR      M=    1 TO 50          STEP 10
       (c) 30             FOR      N=    100 TO 0         STEP -20
       (d) 40             FOR      K=    5 TO 5
       (e) 50             FOR      I=    10 TO 6          STEP -1


3.          Give the output of the following short program
       10          PRINT "PANKAJ" , "GOEL"
       20          PRINT "PANKAJ" ; "GOEL"
       30          PRINT "PANKAJ",, "GOEL"


4.          Give the output of the following short program
            10     LET    A = 40
            20     LET    B = 50
            30     LET    C = 60
            40     LET    D = A+B+C
            50     PRINT “A= A”; A “B = B”; B “C=”; C, “D=”; D
            60     END


26.6        WHAT YOU HAVE LEARNT
This lesson gives us the introductory features of BASIC language, which are very essential
for writing small programs in BASIC. Some additional statements and functions of BASIC
will be discussed in Lesson 4. The example programs given in this lesson should be run on a
computer so that the concept is clear before going to the next lesson.



                                                   23
26.7    TERMINAL QUESTION
1. What does an unconditional GOTO statement do?
2. What is the difference between an IF... THEN statement and an IF... THEN... ELSE
     statement?
3. How is the multiple branching facility provided in BASIC?
4. Write a program in BASIC using For-Next loop to get the following output:
        1     ****
        2     **********
        3     *****
        4     **
        5 *******
5.      Write a program in BASIC to print out the largest number out of 10 given numbers.
26.8    FEEDBACK TO IN-TEXT QUESTION


IN-TEXT QUESTIONS 1
1.      (a)        Right, Left
        (b)        DATA
        (c)        END
2. In case of an INPUT statement, data is read from the keyboard at the time of execution of
     the program. In READ statement, data is provided in the program itself through a DATA
     statement.
3. Print using statement allows printed output to be formatted corresponding to the given
     specification.
4. The restore statement brings the pointer to the first value of the DATA statement,
     regardless of the current position of the pointer.
5. TAB function enables the user to exercise exact control over the print positions.


IN-TEXT QUESTIONS 2
1.      (a) 10 LET A = 5.2
        (b) 20 LET B$="PENTIUM-SYSTEM"
        (c) 30 LET D = C
        (d) 40 LET E = E+1
        (f) 50 LET Q =3*P



                                                 24
3 (a) Values taken by L are : 3,5,7,9,11
  (b) Values taken by M are : 1,11,21,31,41,
  (c) Values taken by N are : 100,80, 60,40,20,0
  (d) Value taken by K is : 5
       (e) Values taken by I are : 10,9,8,7,6,


4.      PANKAJ        GOEL
        PANKAJ GOEL
        PANKAJ                  GOEL


5.     A= 40 B= 50 C= 60 D =150




                                                 25