Saturday, July 13

Branching and Looping

Introduction

In a computer program the statement are ordinarily executed line by line. In BASIC language the execution is done in the ascending order of line number, unless instructed otherwise by control statement. The control sequence of statement executed can be altered by means of control statement.
A transfer of control or jump from one part of the program to another is referred as unconditional branching. Another situation that arises frequently is transfer to one of two different portion of  program depending on the outcome of comparison between two quantities, such an operation is called a conditional  branching.
Looping involves repeating some portion of the program either a specified number of times or until some particular condition has been satisfied.    
BASIC has the following control statement :

GOTO Statement
The GOTO statement is used to branch unconditionally out of the normal program sequence to a specified line number:
Syntax
LN GOTO  line number
Where,
LN      Line Number
line number  : any valid line number within the program.
  •    if jumped line (line number) is an executable statement, that statement and those following are executed.
  • if the jumped line is not executable statement, execution proceeds at the first executable statement encountered after the jumped line.
Example
1. 40 GOTO 70, Jump to a higher line Number(70)
2. 60 GOTO 40, Jump to a lower line Number(40)
Use of GOTO statement in the program.
START
A = 1
PRINT A
IF
A >=100 (N) A=A+1
(Y)
STOP  
 
            

No comments:

Post a Comment