Monday, August 5

IF...THEN..ELSE statement

The IF..THEN..ELSE statement allows us to make the decision regarding program flow based on the result returned by an expression.
Syntax
LN IF expression (,) THEN statement(,) ELSE statement

  • if the result of expression is true, the THEN statement is executed.
  • if the result of expression is false, the THEN line number is ignored and the ELSE line number is executed.
  • THEN and ELSE may be followed by either a line number for branching or one or more statements to be executed.
  • IF..THEN..ELSE is a single statement. They must be written in one line.

Example

1. 10 IF A>B THEN PRINT "A is greater " ELSE PRINT "B is Greater"
It will print the message "A is Greater" if A is greater than B and will print "B is Greater" if B is greater than A  

Use of IF..THEN...ELSE statement in the program.
1. Write a program to decide whether a student has passed or failed in the exam. ( use IF...THEN..ELSE) Pass marks=32

CS 7.7 Display on the screen for IF..THEN..ELSE statement

Ok
10 REM Program to check whether a student is passed
15 REM or failed in the S.L..C. Exam
30 INPUT "Enter the marks obtained :" ;M
40 IF M>=32 THEN 45 ELSE 65
45 PRINT  "MARKS OBTAINED :"; M
50 PRINT "CONGRATULATIONS :"; HE PASSED
60 GOTO 80
65 PRINT "MARKS OBTAINED :"; M
70 PRINT "SORRY ! HE FAILED"
80 END
Ok
RUN
Enter the marks obtained : ? 76
Marks OBTAINED : 76
CONGRATULATIONS ! HE PASSED
Ok
RUN
Enter the marks obtained : ? 23
MARKS OBTAINED : 23
SORRY ! HE FAILED 


2. CS 7.8 Display on the screen for IF...THEN...ELSE statement

Ok
LIST 
10   CLS
15 REM Learning the use of IF...THEN..ELSE statemet
20 INPUT "ENTER THE FIRST NUMBER" ; X
30 INPUT "ENTER THE SECOND NUMBER" ; Y
40 INPUT "THE THIRD NUMBER" ; Z
50 IF X.>Y THEN GOTO 90 ELSE GOTO 100
90 IF X>Z THEN GOTO 130
95 GOTO 150
100 IF Y>Z THEN GOTO 140
105 GOTO 90
130 PRINT "THE LARGEST NUMBER IS" ; X
135 GOTO 160
140 PRINT "THE LARGEST NUMBER IS "; Y
145 GOTO 160
150 PRINT "THE LARGEST NUMBER IS"; Z
160 END
OK 
RUN
  


   

Thursday, August 1

IF...THEN statement

The IF... THEN statement allows us to make a decision regarding program flow based on the result returned by an expression.
Syntax
LN IF expression [,] THEN statement[,]
Points to remember

  • If the result of expression is true (or non zero), THEN then statement is executed.
  • If the result of the expression is false (or zero), the THEN statement is ignored and execution continues with the next executable statement.
  • A comma is allowed before THEN

CS 7.5 Display on the screen for IF..THEN statement

Use of IF..THEN statement in the program.
1. To check the number for given data and print error message. 
Minimum and maximum value data are 10 and 100 respectively.
Ok
LIST
10    REM Learning the use of IF...THEN statement
30    INPUT A
40    IF A<10 THEN PRINT "DATA BELOW THE RANGE"
45    IF A>=10 AND A<=100 THEN PRINT "DATA WITHIN THE RANGE"
50    IF A>100 THEN PRINT "DATA ABOVE THE RANGE"
60    IF A<>0 THEN 30
70    END
Ok
RUN
?  5
DATA BELOW THE RANGE
? 10
DATA WITHIN THE RANGE
? 35
DATA WITHIN THE RANGE
? 150
DATA ABOVE THE RANGE
? 0
DATA BELOW THE RANGE
Ok  

2.CS 7.6 Display on the screen for IF...THEN statement
Ok
LIST 
20   REM To find the largest number among X.Y and Z
30  INPUT " ENTER THE FIRST NUMBER = "; X
40  INPUT " ENTER THE SECOND NUMBER = ";Y
50  INPUT " ENTER THE THIRD NUMBER = ";Z
60  IF X>Y THEN GOTO 100
70  IF Y>Z THEN GOTO 110
80  PRINT " THE LARGEST NUMBER IS "; Z
90 GOTO 140
100 IF X>Z THEN GOTO 130
105 GOTO 80
110 PRINT " THE LARGEST NUMBER IS " ; Y
120 GOTO 140
130 PRINT " THE LARGEST NUMBER IS " ; X
140 END
Ok
RUN
ENTER THE FIRST NUMBER = ? 68
ENTER THE SECOND NUMBER = ? 139
ENTER THE THIRD NUMBER = ? 23
THE LARGEST NUMBER IS 139  
   
          


Saturday, July 27

FOR and NEXT statements

The FOR and NEXT statement is used to execute a series of instructions a specified number of times in a loop.
Syntax
LN FOR variable = X To Y [STEP  Z]
.
.
.
NEXT [variable][,variable...]
Where,
LN      : Line Number
variable  : variable used as a counter
X, Y and Z  :numeric expressions
STEP  Z  :  specifies the counter increment for each loop.

Points to remember

  • The first numeric expression(x) is the initial value of the counter.
  • The second numeric expression is the final value of the counter.
  • Program lines following the FOR statement are executed until the NEXT statement is encountered. Then the counter is incremented by the amount specified by STEP.
  • When STEP is not mentioned, the increment is considered as 1.
  • It is checked if the value of the counters is greater than the final value(Y). If it is not greater, GW - BASIC branches back to the statement, immediately after the FOR statement, and the process is repeated. If it is greater, execution continues with the statement following the NEXT statement. Hence, FOR and NEXT statement forms a loop (i.e. FOR-NEXT loop)
  • If STEP value is negative, the final value of the counter will be less than the initial value.

Examples

1. 10 For A = 0 To 20   
.
.
.
80 NEXT A
2. For A = 0 To 20 STEP 2
.
.
.
80 NEXT A
3. 10 For A = 20 To 0 STEP-2
.
.
.
30 NEXT A

Use of FOR and NEXT statement in the program.
1. To sum the numbers from 1 to 5000 in step of 7

1. CS 7.3 Display on the screen for FOR and NEXT statement

Ok
LIST
5      REM Learning the use of FOR...NEXT statement
10    S = 0
20    FOR I = 1 TO 5000 STEP  7
30    S =S+I
40    NEXT I
50    PRINT "SUM =" ; S
60    END
Ok  
RUN
SUM = 1787500
Ok


2.To generate the natural number up to 10 (i.e. 1 to 10)

CS 7.4 Display on the screen for FOR and NEXT statement

Ok
LIST
10    REM To generate numbers from 1 to 10
20    FOR I = 1 TO 10
30    PRINT I
40    NEXT I
50    END
Ok
RUN
1
2
3
4
5
6
7
8
9
10
Ok      






Thursday, July 18

ON...GOTO Statement

The ON...GOTO Statement is used to branch to one of several specified line numbers depending on the value return, when an expression is evaluated.
Syntax
LN ON expression GOTO line numbers
Where,
LN      :  Line Number
Expression  :  Valid numeric expression
Line number  :  Line numbers to where jump has to take place.

  • In the ON...GOTO statement the value of expression determines which line number in the list will be used for branching.
For example  :
If the value of expression =3, the third will be the destination of the branch.

  • If value expression is zero the execution continues with next executable statement in the program.
  • If value of expression is negative or greater than 255, error 0 classes.
Examples
1.  100 ON  N  GOTO  200, 300, 400
2.  150 ON  M  GOTO  215, 50, 300
In Example 1, the branching takes place depending on the value of N
if N=1, the program branches to line 200
if N=2, the program  branches to line 300
if N= 3, the program branches to line 400 and so on.

START
A =1
PRINT A  (infinite loop)
N
A = A+1     

Note : The program displays the number 1,2,3... and so on infinitely (forming infinite loop)
1.  Press Pause key to stop the display of numbers temporarily.
2.  Press CTRL+Break to stop to the execution of the program (or to terminate the infinite loop)


CS 7.2 Display on the screen for GOTO statement

Ok
LIST
10   REM Learning the use of GOTO statment
20   CLS
30   LET A =1
40   PRINT A
50   LET A = A+1
60   GOTO 40
70   END
Ok    
            

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