CHAPTER NO-3 "Transfer of Control"

Transfer of Control


Q NO 1:-What is transfer of control? What type of jumps can be made in BASIC? Explain with example.
Introduction: – In a basic program statements are executed one after other in a sequence of increasing line numbers. Sometimes, however, it is necessary to jump from one part of the program to another, thus changing the default sequence of execution. The statements used for such jumps are known as transfer of control statements. There are two types of such statements:

10th   5
  1. Unconditional transfer of control statements
  2. Conditional transfer of control statements
    Unconditional transfer of control statements: –
    Those statements which do not need any condition to jumps form one part of the program to another e.g., GOTO, ON, GOTO.
    Example:-
    10 a = 0
    20 a = a+1
    30 print a
    40 goto 20
    50 end
    Conditional transfer of control statements: – Those statements which transfer control to other part of the program based on same condition are known as conditional transfer of control statements e.g. IF/THEN/ELES.
    Example:-
    10 cls
    20 input marks
    30 if marks>33 then 50
    40 if marks<33 then 70
    50 print” you are pass”
    60 goto 80
    70 print” you are fail”
    80 end
    Q NO 2:-What is nested loop? What rules must be followed when using nested loops?
    Nested Loops:- By nested loops we means one loop within another loop.
    Actually, sometimes we need to perform repetitive tasks within a loop. In such a situation we use Nested loops. Its structure looks like.
    10 FOR K =1 TO 10
    20
    30
    40 FOR X = 1 TO 20
    50
    60
    70 NEXT X
    80
    90
    100 NEXT K
    110 END

The rules for written single loops apply to nested loops. Other rules are;

  1. Each nested loop must begin with its own FOR-TO statement and end with its own NEXT statement.
  2. An outer loop and an inner lop cannot have the same index variable.
  3. Each inner (nested) loop must be completely embedded with in the outer loop i.e; the loops cannot overlap.
  4. Control can be transferred from an inner loop to a statement in an outer loop or to a statement out side of the entire nest. However, control cannot be transferred to a statement with in a nest from a point out side the nest.
    Q NO 3:-What is the difference between?
    Loop: – a loop is a set of statement which is executed repeatedly.
    Nested loop: – By nested loops we mean one loop within another loop.
    Conditional transfer of control statements: – Those statements which transfer control to other part of the program based on same condition are known as conditional transfer of control statements e.g. IF/THEN/ELS.
    Unconditional transfer of control statements: – Those statements which do not need any condition to jumps form one part of the program to another e.g., GOTO, ON, GOTO.
    IF/THEN/ELSE and ON/GOTO statements: – It is similar to the IF-THEN statement, with an added power to transfer control to another part of the program whether the condition is true or false.
    e.g., 10 IF marks>33 THEN PRINT “PASS”ELSE PRINT”FAIL”
    THE ON/GO TO statement: – It is multiple branching unconditional transfer of control statement which is used to transfer control to any one of the indicated line numbers.
    e.g., 40 on choice GO TO 15,35,60,100,60.
    FOR/NEXT LOOP statement: – This is a loop which is used to perform repetitive tasks and is used when we know in advance the number of times the loop will be executed.
    WHILE/WEND LOOP statement: – It is used when the numbers of repetitions are not known in advance. It has the capability to be terminated any time depending on the condition.

]]>

admin

Leave a Reply

Your email address will not be published. Required fields are marked *