JBASIC Programming
JBASIC Programming
OBJECTIVE
2
COURSE AGENDA(DAY 1)
SESSION I SESSION II
Introduction to jBasic Jbase environmental
variables
Features of jBasic
Programs , Subroutines
Introduction to Jbasic
programs Control structures
BREAK
3
COURSE AGENDA(DAY 2)
SESSION I SESSION II
File Management Subroutines with
parameters
User Defined Functions
Examples
BREAK
4
INTRODUCTION
5
INTRODUCTION TO JBASIC
6
FEATURES OF JBASIC
7
ARRAYS
8
ARRAYS
Dimensioned arrays
– More efficient means of creating and manipulating tables of data.
– The number of dimensions and the extent is known and is not likely to
change.
– Dimensioned arrays have to be declared using the DIMENSION
statement.
Example
– To declare a dimensioned array use DIM Array2(6,4)
• 6 refers to the number of rows
• 4 refers to the number of columns
9
ARRAYS
Dynamic Arrays
– Dynamic arrays are, as the name implies, dynamic in both the number,
dimensions and their extents.
– Dynamic arrays are especially useful in providing the ability to manipulate
variable length records with a variable length of fields and/or values within fields
etc.
– A dynamic array is just a string of characters that contain one or more delimiter
characters.
Delimiter characters
– ASCII Decimal Description
• 254 Field Marker
• 253 Value Marker
• 252 Sub-Value Marker
– Each field can be delimited by a Field marker, Value marker or Sub-Value
marker.
• Field Marker is denoted as 1
• Value Marker is denoted as 1.1 First level
• Sub-Value Marker is denoted as 1.1.1 – Second level
10
JBASIC PROGRAMS
11
JBASE ENVIRONMENTAL VARIABLES
12
JBCDEV_BIN
13
JBCDEV_LIB
14
JBCOBJECTLIST
15
JLIBDEFINITION
16
STRUCTURE OF
PROGRAMS AND SUBROUTINES
17
STRUCTURE OF JBASIC PROGRAMS
18
STRUCURE OF JBASIC SUBROUTINES
19
COMPILATION
20
CATALOG
21
RUN
22
DEBUGGER
23
EXAMPLE : PROGRAMS
24
EXAMPLE : SUBROUTINES
25
STEP TO EXECUTE SUBROUTINES
26
STEP TO EXECUTE SUBROUTINES
27
INSERT FILES
Insert files are similar to ‘Include’ file that you might have
used in ‘C’ and ‘C++’ programs.
There are number of insert files available. Each one of them
have their own functionality which can be used in our
programs/subroutines to enable re-usability of code.
28
I_COMMON AND I_EQUATE
29
CONTROL SRTUCUTURES IN JBASIC
30
CONTROL STRUCTURES
31
IF-THEN-ELSE STRUCTURE
32
IF-THEN-ELSE STRUCTURE
33
IF-THEN-ELSE STRUCTURE
IF expression THEN
statements
END ELSE
IF expression THEN
statements
END ELSE
statements
END
END
34
LOOP - REPEAT STRUCTURE
35
LOOP - REPEAT STRUCTURE
36
LOOP - REPEAT STRUCTURE - EXAMPLE
PROGRAM TEST.LOOP
X=0
LOOP
UNTIL X>4 DO
PRINT "X= “ :X
X=X+1
REPEAT
END
37
LOOP - REPEAT STRUCTURE - EXAMPLE
Output :
X = 0
X = 1
X = 2
X = 3
X = 4
38
FOR - NEXT STRUCTURE
39
FOR - NEXT STRUCTURE- EXAMPLE
Example :
FOR I = 1 TO 10
PRINT ‘Counter Variable:’: I
NEXT
I is the counter variable.
The PRINT statement executes for a pre-determined 10 times
(i.e) till the value of I is 10
40
FOR - NEXT STRUCTURE- EXAMPLE
Output:
COUNTER VARIABLE:1
COUNTER VARIABLE:2
COUNTER VARIABLE:3
COUNTER VARIABLE:4
COUNTER VARIABLE:5
COUNTER VARIABLE:6
COUNTER VARIABLE:7
COUNTER VARIABLE:8
COUNTER VARIABLE:9
COUNTER VARIABLE:10
41
FOR - NEXT STRUCTURE- EXAMPLE
Example :
FOR I = 1 TO 10 STEP 2
PRINT ‘Counter Variable:’: I
NEXT
I is the counter variable.
The PRINT statement executes for a pre-determined 5 times, I
being incremented by two each time.
42
FOR - NEXT STRUCTURE- EXAMPLE
Output :
COUNTER VARIABLE:1
COUNTER VARIABLE:3
COUNTER VARIABLE:5
COUNTER VARIABLE:7
COUNTER VARIABLE:9
43
CASE STRUCTURE
44
CASE STRUCTURE
45
CASE STRUCTURE - EXAMPLE
Example :
PROGRAM TEST.CASE
NAME="MICHAEL"
BEGIN CASE
CASE NAME[1,2]='DA'
PRINT NAME
CASE NAME[1,2]='RI'
PRINT NAME
CASE NAME[1,2]='BA'
PRINT NAME
CASE 1
PRINT 'NO MATCH'
END CASE
END
Output : No Match
46
OTHER FLOW CONTROL STATEMENTS
EXIT
– exit from loop structure prematurely.
END
– mandatory last statement in a program.
– Signifies end of program.
STOP
– terminates execution of program and the control transfers back to the
process that invoked the program.
CALL
– executes an external subroutine.
EXECUTE
– executes unix / jBase commands.
47
OTHER FLOW CONTROL STATEMENTS
GOSUB
– transfer the control of the program to an internal subroutine
available within the program.
RETURN
– used in conjunction with GOSUB. Transfers the control from
the subroutine back to the next statement after GOSUB in the
main program.
GOTO
– transfers control of the program to a statement within the
program. The control does not transfer back even when a
return statement is encountered
48
RELATIONAL OPERATORS
49
LOGICAL OPERATORS
Example:
– IF EMP.DESG EQ ‘MGR’ AND EMP.SAL GT 10000 THEN In the
logical expression above, the logical operator ‘AND’ combines
the two relational expressions ‘EQ’ and ‘GT’.
50
BASIC COMMANDS
$INSERT
– Inserts and compiles info Basic source code from another
program into the program being compiled
REM
– Identifies a line as a comment line. Same as the *, !, and $*
signs
SUBROUTINE
– Identifies a series of statements as a subroutine.
51
JBASIC FUNCTIONS
52
INPUT
Example :
– INPUT NAME,10
• In this Example The User is Restricted to enter 10 characters only.
53
PROMPT
Example :
– PROMPT “#”
54
CRT
55
PRINT
56
LOCATE
57
FIND
58
FINDSTR
59
ICONV
60
CONVERSION CODES - ICONV
12 MARCH 95 ‘D’
9933
12.3.95 ‘DE’ 9933
61
OCONV
62
CONVERSION CODES - OCONV
1500 ‘MD2’
15.00
9933 ‘D’ 12 MAR
95
63
STRING MANIPULATION
SYNTAX TRIM(string)
64
LEN
65
FIELD
66
INDEX
67
ALPHA
68
CHAR
69
SEQ
70
COUNT
71
DEL
72
INS
73
DCOUNT
74
COUNT
75
MAXIMUM
76
MINIMUM
77
JDEBUGGER COMMANDS
78
WORK SHOPS
79
WORK SHOP
Write a program to find the biggest number from the given three
numbers.
Based on the period for a given principal, calculate the interest as
follows:
• 3 months – 4.5 %
• 6 months – 5%
• 1 – 2 yrs – 5.5 %
• >2 yrs -6%
80
FILE MANAGEMENT
81
FILE MANAGEMENT
82
OPEN
EXAMPLE :
– OPEN ‘FBNK.CUSTOMER’ TO CUSTOMER.FILE ELSE CRT
“Company File Cannot be opened “
83
READ
84
WRITE
EXAMPLE :
R.CUS.REC<1> = ‘TESTWRITE’
WRITE R.CUS.REC ON CUSTOMER.FILE,’100037’ ON ERROR
CRT “cant write”
85
DELETE
86
CLOSE
Example :
OPEN CUSTOMER TO CUSTOMER.FILE
CLOSE CUSTOMER.FILE
87
USER DEFINED FUNCTIONS
88
DEFINING FUNCTIONS
89
DEFINING FUNCTIONS - EXAMPLE
STEP 1:
– Define a Function that accepts two values and return the result
in a variable.
FUNCTION TEST.FUN(A,B)
C=A*B
RETURN(C)
END
STEP 2:
– Compile and Catalog the Function.
90
DEFINING FUNCTIONS - EXAMPLE
STEP 3:
– Write a Program that calls the Function(TEST.FUN) and Prints the
Result.
PROGRAM FUN.CALLING.PRG
A = 5 ; B =10
DEFFUN TEST.FUN(A,B)
RESULT = TEST.FUN(A,B)
PRINT “RESULT : “:RESULT
END
STEP 4:
– Compile and Catalog the Program
STEP 5:
– Run the Program
Result : RESULT : 50
91
SUBROUTINES WITH PARAMETERS
92
SUBROUTINES WITH PARAMETERS
93
SUBROUTINES WITH PARAMETERS-EXAMPLE
STEP 1:
Create a routine that accepts two values and returns the
multiplication of two values in variable.
SUBROUTINE TEST.CALLED.RTN(A,B,C)
C=A*B
RETURN
END
94
SUBROUTINES WITH PARAMETERS-EXAMPLE
STEP 2:
Create another routine that supplies the two values and prints the
result.
PROGRAM TEST.CALLING.RTN
A = 10 ; B =20
CALL TEST.CALLED.RTN(A,B,RESULT)
PRINT “RESULT : ”:RESULT
END
STEP 3:
Compile and catalog the Program and Subroutine.
STEP 4:
Run the Program
Result : RESULT : 200
95
CORE T24 SUBROUTINES
96
OPF
EXAMPLE :
FN.CUSTOMER = ‘FBNK.CUSTOMER’
F.CUSTOMER = ‘’
CALL OPF(FN.CUSTOMER,F.CUSTOMER)
NOTE:
File pointer is initialized with null value and at the time of execution it
will be assigned to the path of the file
97
F.READ
98
F.READ
EXAMPLE:
FN.CUSTOMER = ‘FBNK.CUSTOMER’
F.CUSTOMER = ‘’
R.CUSTOMER = ‘’
CALL OPF(FN.CUSTOMER,F.CUSTOMER)
CALL F.READ (FN.CUSTOMER,Y.CUSTOMER.ID,R.CUSTOMER,
F.CUSTOMER,Y.CUS.ERR)
99
F.WRITE
100
F.WRITE
EXAMPLE
– <Initialize variables FN.CUSTOMER,F.CUSTOMER,….>
– <Open the file using OPF>
– <Read the record using F.READ>
– <Assign the value to the dynamic array which we are going to
write>
– R.CUSTOMER<EB.CUS.SHORT.NAME> = ‘ABC CORPORATION’
– CALL F.WRITE (FN.CUSTOMER,Y.CUSTOMER.ID,R.CUSTOMER)
101
EB.READLIST
102
EB.READLIST
EXAMPLE
– <Initialise File name FN.CUSTOMER>
– SEL.CMD = “SELECT “:FN.CUSTOMER
– CALL
EB.READLIST(SEL.CMD,SEL.LIST,’’,NO.OF.RECORDS,RET.CODE)
103
REMOVE
104
JOURNAL.UPDATE
105
EXAMPLES
106
EXAMPLE 1
107
EXAMPLE 1 : ALGORITHM
108
EXAMPLE 1 : SOLUTION
SUBROUTINE CUST.READ
$INSERT I_COMMON
$INSERT I_EQUATE
$INSERT I_F.CUSTOMER
GOSUB INIT
GOSUB OPENFILES
GOSUB PROCESS
RETURN
INIT:
FN.CUS = "FBNK.CUSTOMER"
F.CUS = " "
Y.CUS.ID = '100037'
Y.NATIONALITY = " "
Y.MNEMONIC = " "
R.CUSTOMER = " "
Y.ERR = " "
RETURN
109
EXAMPLE 1 : SOLUTION
OPENFILES:
CALL OPF(FN.CUS,F.CUS)
RETURN
PROCESS:
CALL F.READ(FN.CUS, Y.CUS.ID , R.CUSTOMER , F.CUS, Y.ERR)
Y.NATIONALITY = R.CUSTOMER<EB.CUS.NATIONALITY>
Y.MNEMONIC = R.CUSTOMER<EB.CUS.MNEMONIC>
PRINT “CUSTOMER ID IS :”:Y.CUS.ID
PRINT "NATIONALITY IS :":Y.NATIONALITY
PRINT "MNENOMIC IS :":Y.MNEMONIC
RETURN
END
110
EXAMPLE 2
111
EXAMPLE 2 : ALGORITHM
112
EXAMPLE 2 : SOLUTION
SUBROUTINE CUST.READ.WRITE
$INSERT I_COMMON
$INSERT I_EQUATE
$INSERT I_F.CUSTOMER
GOSUB INIT
GOSUB OPENFILES
GOSUB PROCESS
RETURN
INIT:
FN.CUS = "FBNK.CUSTOMER"
F.CUS = " "
Y.CUS.ID = ‘’
Y.NATIONALITY = " "
Y.MNEMONIC = " "
R.CUSTOMER = " "
Y.ERR = " "
RETURN
113
EXAMPLE 2 : SOLUTION
OPENFILES:
CALL OPF(FN.CUS,F.CUS)
RETURN
PROCESS:
SEL.CMD = ‘SELECT “:FN.CUS
CALL EB.READLIST(SEL.CMD,SEL.LIST,’’,NO.OF.REC,RET.CODE)
LOOP
REMOVE Y.CUS.ID FROM SEL.LIST SETTING POS
WHILE Y.CUS.ID:POS
CALL F.READ(FN.CUS, Y.CUS.ID , R.CUSTOMER , F.CUS , Y.ERR)
Y.OLD.ACCT.OFFICER = R.CUSTOMER<EB.CUS.ACCOUNT.OFFICER>
114
EXAMPLE 2 : SOLUTION
115
SUMMARY
116