Chapter 4: Difference between revisions
m (→4.2 Numeric expressions: The word "user" is not a good way to refer to an operator) |
(→4.3 String constants & variables: corrected program error by adding a line number and moving line segment to a separate line) |
||
Line 248: | Line 248: | ||
00310 PRINT "Hello, isn't it a great day!!!!" | 00310 PRINT "Hello, isn't it a great day!!!!" | ||
00320 LET CITY$='Honolulu' | 00320 LET CITY$='Honolulu' | ||
00330 PRINT "This is Bruno's computer -- keep your hands off" -----------------------PRINT 'A double quotation mark means " should be used' | 00330 PRINT "This is Bruno's computer -- keep your hands off" ----------------------- | ||
00340 PRINT 'A double quotation mark means " should be used' | |||
00350 PRINT "A double quotation mark means "" should be used" | 00350 PRINT "A double quotation mark means "" should be used" | ||
Revision as of 17:16, 24 November 2019
4.1 Building Blocks
This chapter marks a shift in the tone of this tutorial. In the first three chapters, we tried to avoid technical definitions and give complete examples to allow you to get off to a successful start with a minimum of things to remember from previous chapters. However, this book would be very long if we always focused on specific examples, never giving you any rules, that would allow you to figure out some of the examples by yourself, and always repeat material from previous chapters. Also, you would not become a very good programmer. Although we still want to review major information at critical points in the remaining chapters, from here on you will have to carry a little more of the load yourself. For example, we won't always be reminding you that a string variable is one whose name ends in a dollar sign. This section describes some important terminology. The terms are new, but most of the ideas are things that you have already seen. The reason that this terminology is important is that it will be used later to describe different features of Business Rules!. Some parts of Business Rules! will only work with numeric constants, but others will work with variables or expressions, too. When you finish this chapter, you will be able to:
- Define and use numeric constants, variables, and expressions.
- Define and use string constants, variables, and expressions.
- Use Business Rules!'s built-in numeric and string functions in numeric and string expressions.
- Use parentheses to alter the order of arithmetic operations in numeric expressions.
- Use single and double quotation marks to define and use string constants.
- Use the DIM statement to alter the maximum length of string variables.
- Use the string operations of concatenation and substring.
- Use substring to replace, remove, and insert in string variables.
- Use the system functions DATE$ and TIME$ along with the commands DATE and TIME.
Numeric constants & variables
A constant is something that does not change.
“a”, 100, “Pete”, -27 are all examples of constants.
A constant is different from a variable because the value of a variable can change. These rules apply to both numeric and string constants and variables, but let's look first at examples with numeric constants and variables.
00010 PRINT 1986 ! constant 00020 PRINT YR ! variable 00030 YR=2001 ! here we change the value of the variable 00040 PRINT YR ! print the NEW value of the variable 00050 PRINT 1986 ! 1986 is printed again as on line 10, since it cannot change its value
Except for the line numbers, lines 10 and 50 look exactly the same, and they both print the four-digit number 1986; this information is coded as a constant, it does not change. Except for the line numbers, lines 20 and 40 also look exactly the same, but what they print is not the same. If you are not sure what they will print, type in this program and run it. YR is a variable and can have different values at different times. A numeric expression may be either a constant or a variable or it could be an arithmetic formula that includes constants and variables.
Valid examples of numeric expressions in LET statements would be:
00110 LET X=5 ! right side of "=" is a constant 00120 LET Y=X ! right side of "=" is a variable 00130 LET Z=Y*5+3 ! right side of "=" is an arithmetic expression
There are very specific rules for constants, variables and expressions. There are also very specific rules for Business Rules! statements; these statement rules are stated using terms like constant, variable and expression. For example,
A variable must appear on the left side of the equal sign in a LET statement. The left side of a LET statement cannot be a constant. This rule is a syntax rule.
A syntax rule is a rule about the parts of a statement and the order in which those parts can be used or combined.
The following statements will produce syntax errors because they violate this syntax rule.
00210 LET 5=X+Y ! illegal, left side can't be a constant 00220 LET Y*5+3=Z*2 ! illegal, left side can't be a complex expression
Now do you see why it is important to know what we mean by constant, variable and expression? Even after you read "a variable must appear on the left side of the equal sign in a LET statement", you could memorize that sentence but still make a lot of errors if you confused the terms constant, variable and expression.
Besides being important for syntax rules, these terms are also important for semantic rules, which is a rule about the purpose of the statement.
The LET statement evaluates the expression on the right side of the equal sign, and then assigns the calculated value to the variable on the left side. In other words, it computes the thing on the right, and stores the answer in the variable named on the left. Now let's examine the rules for a numeric constant, which means a constant used as a number. String constants will be explained in a lesson later in this chapter.
- A numeric constant may include the digits 0-9, an optional decimal point, and an optional plus sign or minus sign. If a sign is included, it must be at the beginning.
- A numeric constant cannot include commas. Later, you will learn how to print numbers using PIC to add commas as the number is being printed; but commas cannot be part of the number as it is stored on disk.
- If a number is negative, it must begin with a minus sign.
- If no plus or minus sign appears at the beginning of a numeric constant, it is assumed to be positive.
- A numeric constant with a decimal fraction is called a fixed-point number. A numeric constant without a decimal fraction is called an integer, or a whole number.
You have already studied most of the rules for names of numeric variables. Briefly, the name of a numeric variable can contain up to 30 letters, numbers or underscores, but must always begin with a letter. A numeric variable name represents a number (stored in one of Zippy's manila folders). In later chapters, you will learn about different types of variable names, such as arrays, subscripted variables and simple variables. But now let's get back to the basics.
Quick Quiz 4.1
1) In the following statement, which variables are changed?:
00100 LET A=B
a) Only A gets a new value.
b) Only B gets a new value.
c) After this statement, A and B will always have the same value; when one changes, the other changes automatically.
2) Which items represent a legal numeric constant?
a) 10-
b) 10+
c) -10
d) +10
e) 10.
f) 12.34
g) 12+3
h) -12.3+
i) 12.3000
j) 12,345.67
k) $100.10
3) Which items represent a legal integer constant?
a) 10-
b) 10+
c) -10
d) +10
e) 10.
f) 12.34
g) 12+3
h) -12.3+
i) 12.3000
j) 12,345.67
k) $100.10
4) Match the following:
1. syntax rules | a. Parts of a statement and their order | |
2. Semantic rules | b. Purpose (or meaning) of a statement |
5) True or False:
a) The value of a constant never changes.
b) The value of a variable never changes.
c) The value of an expression never changes.
d) A LET statement must have a variable on the left side of the equal sign.
e) A LET statement must have a variable on the right side of the equal sign.
f) A numeric variable name must always begin with a letter.
g) A numeric variable name can contain up to 30 letters, digits or underscores.
h) A numeric variable name must end with a dollar sign.
i) Numeric variable names can represent (i.e. can have as values) either a number or a string of letters.
4.2 Numeric expressions
a=7 a=7+4 a=x+4 a=x+4/y-8*5+(7**2+y)
What are the rules for numeric expressions? First, any numeric constant or any numeric variable name is an example of a numeric expression. This means that you can use a numeric constant whenever a statement allows a numeric expression. However, the opposite is not true. If a statement calls for a numeric constant, you cannot use a complex numeric expression. Reviewing information from the last lesson and adding numeric formulas that will be explained next, we have an overview picture of numeric expressions that looks like this:
Note that an expression is not a type of statement, and should not be used as a statement by itself. It is a part or building block of a statement. Line 10 is a legal statement because it is a legal use of an expression (A*5) in a LET statement. Line 20 below should not be used as a statement, even though it is a legal expression (technically speaking, line 20 will not generate a syntax error because of a subtle point about the way Business Rules! handles functions. It wont give present an error, but it wont do anything else either; even though it is not illegal, it does not perform any useful purpose).
00010 LET B = A * 5 00020 A * 5
Now let's examine what makes a complex expression.
A complex expression is an expression that combines two or more operands, i.e. numeric constants or numeric variables, using operations such as addition, subtraction, multiplication and division.
For example, 51+3 combines the constants 51 and 3 using the addition operator, the plus sign. The operands 51 and 3 are combined using the operator for addition. The calculated value of this particular numeric expression is 54. So 51+3, 51-3, 51*3 (* is the operator for multiplication) and 51/3 (/ is the operator for division) are all fine upstanding examples of numeric expressions.
One other operations that you might see occasionally is ** for exponents, also called raising a number to a power. A**B means to take the number A and multiply it times itself B times. For example:
5 ** 3 is the same as 5 * 5 * 5 or 5 cubed
The statement “PRINT 5**3” will print the value 125.
A complex expression can also include more than one operator. Remember using Business Rules! as a calculator in the previous chapters?
What answer do you get from the following?
PRINT 1+2*3
Do you think it will be 9 or 7? The correct answer is 7. When there is more than one operator, it is important to know which operations get done first. Here are the rules that Business Rules! uses:
1. Parenthesis
2. Exponents
3. Multiplication and division.
4. Addition and subtraction.
These three rules are called the standard algebraic hierarchy. The term "algebraic hierarchy" means priorities for arithmetic. In school you probably heard it called "order of precedence" or "order of operations" which means 'rules about order'. Multiplication and division are treated together because the order that they are performed does not change the answer. If a computation has more than one of these, it may help you to think of it as being performed from left to right -- but again, it does not change the answer. Similarly, addition and subtraction are grouped for the same reason.
To compute ((5+1)*(1+2))+2**3/4:
1) Business Rules! must first look to see is if there are any parentheses [()]. Calculations inside parentheses are performed first. Also, if there are more than one set of parentheses, BR! starts with the innermost pair and perform those calculations first. In this case there are two innermost pairs: (5+1) and (1+2). After the first pass the equation looks like this: (6*3)+2**3/4
After the second pass, the outermost parentheses are computed: 18+2**3/4
2) Second, BR! looks for any exponentiation (**). 2**3 is the exponent here, so after another pass, the equation looks like this: 18+8/4
3) Next the system looks for either multiplication or division. In our example, 8/4 would be calculated next, and the expression would now become: 18+2
4) On the last pass, the addition of 18+2 would give the final answer of: 20
Let's look at one more: 2 ** (9/ (5 - 2) ) + 4 * 3
First the inner parentheses: 2 ** (9 / 3) + 4 * 3
Next the outer parentheses: 2 ** 3 + 4 * 3
Next Exponentiation: 8 + 4 * 3
Next Multiplication: 8 + 12
Finally addition: 20
Here is a saying among some algebra classes that may help you remember the algebraic hierarchy: PLEASE (parentheses) EXCUSE (exponents) MY (multiplication) DEAR (division) AUNT (addition) SALLY (subtraction)
One final part of our study of expressions concerns the use of functions. Business Rules! provides over 50 built-in functions and the capability to add your own functions. We can illustrate all this with the built-in function SQR, the square root function. Wherever a numeric constant or variable name can appear, you could also use SQR(x) where x is the number to take the square root of this number.
For example:
00010 PRINT SQR(36) 00020 LET x = 25 - SQR(16) 00030 PRINT 25 - (SQR(25) - 3) * 2
Let's take a minute to put this in perspective. The examples of numeric expressions that you have seen in this section are probably more complex than the ones you will see in most commercially available business programs and more complex than the calculations that you will write in most of your programs. Balancing the company financial report usually does not require exponential calculations inside two levels of parentheses. The main point of this section is to show you the principles and definitions, and to give you practice using them.
These definitions and examples are important for your future understanding of what can go where in Business Rules! statements. Spend as much time as you need on reading, reviewing, typing in the examples, and studying the quiz on this section. Just remember, you don’t have to be good at the math part. The computer does it for you. We’re just trying to explain what goes on behind the scenes.
Quick Quiz 4.2
True or False:
1) An integer constant can appear wherever a numeric expression is allowed.
2) A numeric expression can appear wherever an integer constant is allowed.
3) A numeric expression is a type of statement.
4) For numeric expressions with more than one operator, the order of execution is multiplication or division before addition or subtraction, then exponentiation.
5) ((1+2)+3)**(1)+(1)=7
6) What number would Business Rules! compute for the following numeric expressions? Figure them out first by yourself, then use PRINT to check your answers - but be careful not to make any typing mistakes.
a) 36 / 4 + 2 * 3 - 1
b) 36 / ((4 + 2) * 3) - 1
c) 2 ** (5 - 2 * 2)
d) 2 ** (5 - 2) * 2
e) 2 ** 5 - 2 * 2
4.3 String constants & variables
This lesson is much like the one at the start of this chapter, except that it tells about strings instead of numbers. Again, the main ideas are not new, but the terminology is new.
Character strings, unlike numeric expressions, can also include letters and punctuation marks instead of only numbers. Since letters can be included, the values of string variables cannot be used for arithmetic.
A string constant must be enclosed in quotation marks. Either double quotation marks or single quotation marks may be used.
All the PRINT and LET statements below use string constants.
00310 PRINT "Hello, isn't it a great day!!!!" 00320 LET CITY$='Honolulu' 00330 PRINT "This is Bruno's computer -- keep your hands off" ----------------------- 00340 PRINT 'A double quotation mark means " should be used' 00350 PRINT "A double quotation mark means "" should be used"
NOTICE that line 330 uses a single quote inside the string; this is possible because double quotes are used as delimiters to enclose this string constant. Similarly, line 340 uses a double quotation mark in the middle of a string enclosed by single quotes. In line 350 after the word "means", there are two quotation marks back-to-back. This is another way of inserting a single double quotation mark in the middle of a string constant. NOTE Lines 340 and 350 both print the same result.
- String variable names differ from numeric variable names only in that string variable names must end in a dollar sign. Both types of names can be from one to thirty characters long, and can mix letters, numbers and underscores. Both must have a letter as the first character in the name.
A very important part of each string variable name is the maximum string length associated with that variable name. Business Rules! assumes that each string variable can only hold 18 characters unless you tell it that certain strings will need more.
The default maximum length of a numeric variable in BR is 18 characters. Attempting to store a longer string into a string variable requires a DIM statement.
At one time or another, every programmer forgets this and gets a string overflow error because of it. So let's try something to get your first string overflow error and learn how to correct it. Type in the following one line program and then type the RUN command.
00020 INPUT long_message$
As soon as the bottom four lines of your screen look like the screen below, type in all 26 letters of the alphabet and hit ENTER.
00020 INPUT long_message$ RUN INPUT
Because you tried to type in 26 characters to a string variable that can only hold a maximum of 18 characters, you got an error. What error code is it? Look up the explanation of this error code in BR Help by pressing function key F1. A new statement called the DIM statement is the solution to this problem.
The DIM statement is a statement used to specify how long a string variable used in your program will be if its size may be longer than 18 characters. The DIM statement can also be used to decrease this limit.
For future reference: When we refer to a DIM statement, we may also say that a string variable was dimensioned. For example, DIM S$*255 is the same as saying that the string variable S$ was dimensioned to 255 characters.
To allow 80 characters of input in our program, you should add the following line:
00010 DIM LONG_MESSAGE$*80
Notice the star or asterisk toward the end of this statement. This asterisk separates the variable name from the new maximum string length specified above as 80 characters. After this statement is added to the program, line 20 would accept an input string that goes all the way across the screen (assuming that you have an 80 column BR! screen).
Is your program still sitting with the word ERROR in the left of the status line? If it is, type STOP to stop the program. When you see READY in the left of the status line, add line 10, and then type RUN. Your program should now accept up to 80 characters of input.
Remember that this value from the DIM statement is the maximum string length.
Rule: The actual string length can be less than specified by the DIM statement, even zero if no string has yet been assigned to this variable.
The null string is a string of zero length.
You can find out the length (the total number of characters or keystrokes) of any string with a built-in BR function called LEN. For example, to find out the actual current length of the variable A$, you would type:
PRINT LEN(A$)
To demonstrate how string length can change, add lines 15 and 25 to the program that you currently have in memory. The result should be:
00010 DIM LONG_MESSAGE$*80 00015 PRINT LEN(LONG_MESSAGE$) 00020 INPUT long_message$ 00025 PRINT LEN(LONG_MESSAGE$)
If you type in the entire alphabet when asked for input, can you predict what two numbers will be printed by this program? Type RUN and see if you are correct. You may also want to experiment with this program by typing in your name as input and adding spaces on the front, back or middle to see how this changes string length. In case the user types in more characters than you specified in your DIM statement, your program will be interruted by a string overflow error. This error can be trapped by adding the SOFLOW (string overflow) error condition to the end of line 20. SOFLOW works like CONV on the end of the statement. It can be used to refer to a line that will PRINT a message and let the user try again.
00010 DIM LONG_MESSAGE$*80 00015 PRINT LEN(LONG_MESSAGE$) 00020 INPUT long_message$ SOFLOW 35 00025 PRINT LEN(LONG_MESSAGE$) 00030 STOP 00035 print “Message too long” 00040 retry
Quick Quiz 4.3
True or False:
1) The value of any string variable can be used for arithmetic.
2) In PRINT and LET statements, a string constant must be enclosed in quotation marks.
3) String constants cannot include quotation marks inside the string.
4) String constants can be up to 520 characters long.
5) String variable names follow the same rules as numeric variable names except that string variable names must end in a dollar sign.
Fill in the blanks:
6) The default maximum length of any string variable is ________.
7) The maximum string length can be increased or decreased by using the ____________ statement.
8) The length of the current value of any string variable can be found by using the __________ function.
9) A special term for string variables with zero length is ____________________.
4.4 Concatenation & Substrings
As we said with numeric expressions, any string constant or any string variable name is a simple case of a string expression. Is there anything like a complex string expression? Can you do operations with strings? Yes. The operands can be string constants, string variables, or string functions. There are two operations. Concatenation is the joining of strings and is like addition of strings. Taking a substring is the operation like subtraction.
Concatenation is the joining of two or more strings using the ampersand &. Concatenation may be used in any PRINT or LET statement to join any string expressions. Lines 130 and 220 illustrate the concatenation of three strings:
00100 A$ = "1" 00110 B$ = "B" 00120 C$ = "234" 00130 LINE$ = A$ & B$ & C$ 00140 PRINT LINE$ 00200 NAME1$ = "Jack" 00210 NAME2$ = "Jill" 00220 PRINT NAME1$ & NAME2$
Can you predict what will be printed in lines 140 and 220? After you have made a guess, type in this short program and see if you were right.
Notice that it is likely that putting strings together can result in a combined string length larger than the default of 18 characters. If you plan to do a lot of concatenation, remember that you may need to add some DIM statements.
Substring refers to a part of a string instead of the whole string.
For future reference: We will use the following notation to refer to substrings:
string$ ( start : end )
where start and end are the substring positions in the given string For example, assume A$ = “abcdef”. Then A$(1:2) is “ab”
In any string expression, a string variable can be replaced by a substring. For example, A$(4:6) uses the substring of A$ beginning with position 4 up to and including position 6; the numbers inside the parentheses could be replaced by any numeric expression. Can you predict what will be printed in the following program? Do you think there will be any errors as you type in or run the program?
01000 DIM N$*100 01100 N$ = "Application Development Systems" 01200 PRINT N$(1:12);N$(13:22);N$(23:40) 01300 PRINT N$(70:80);N$(101:105) 01400 PRINT LEN(N$);LEN(N$(101:105))
The substring feature may also be used to replace, remove, or insert characters in a string. These techniques require substring notation on the left side of the equal sign in an assignment statement.
Replacing characters in a string
Line 20 replaces "BC" with "23" and prints the value "A23D" for X$.
00010 X$="ABCD" 00020 X$(2:3)="23" 00030 PRINT X$
Removing characters from a string
Line 120 removes the "CD" in positions 3 and 4, and then prints the value "AB" for X$.
00110 X$="ABCD" 00120 X$(3:4)="" ! “CD” gets replaced by the empty string 00130 PRINT X$ ! the result is “AB”
=Inserting characters into a string
If you try to insert a string shorter than specified by the substring positions, no error will occur. Instead, the string will be shortened and the characters replaced. For example:
00010 let a$ = “123456789” 00020 let a$(1:4) = “0” 00030 print a$ ! a$ will be “056789”, since “1234” is replaced by “0”
However, if you try to insert a string longer than specified by the substring positions, an error will occur.For example:
00010 let a$ = “123456789” 00020 let a$(1:2) = “abcdef” ! execution of this line will result in an error
Using 0 and inf as positions in substrings
1) If you want to add characters to the beginning of a string, use 0 as the start and end position:
00010 let a$ = “xyz” 00020 let a$(0:0) = “%%%” 00030 print a$ ! now a$ is “%%%xyz”
2) If you want to add characters to the end of a string, use inf as the start and end position:
00010 let a$ = “xyz” 00020 let a$(inf:inf) = “%%%” ! inf:inf means “insert at the end” 00030 print a$ ! now a$ is “xyz%%%”
inf is short for infinity and is the highest possible number in BR. For example, in BR version 4.20, this number is 10307 or 1E+307 in scientific notation.
3) If you want to add characters in the middle of a string without having to count how many characters you are inserting, use a number between 0 and the length of the string as the start and use 0 as the end position.
00210 X$="ABCD" 00220 X$(2:0)="123" ! inserts "123" at position 2; X$ is now "A123BCD" 00230 PRINT X$
The above example works if x$ is dimensioned long enough to fit the characters we want to insert, i.e. a proper DIM statement was used.
The example below would fail, because the string we are trying to insert is longer than the default 18 characters, and we did not dimension X$ to fit the extra characters:
00210 X$="ABCD" 00220 X$(2:0)="12345678901234567890" ! error
Using variables as positions for substrings
The numbers inside parentheses can be replaced by any numeric expression. In the following example of replacing characters, line 40 sets Z$ to "XXC".
00010 Z$ = "ABC" 00020 A=1 00030 B=2 00040 Z$(A:B) = "XX"
Quick Quiz 4.4
1) A$(2:4) means:
a) String value of array A$ in row 2, column 4.
b) A substring of A$ starting in position 2 that is 4 characters long.
c) A substring of A$ that starts in position 2 ends in position 4.
2) Concatenation refers to:
a) The combining of two or more numbers.
b) The combining of two or more characters strings.
c) A nation of ex-criminals and their kitty-cats.
3) The substring feature can be used on the left side of the equal sign in LET statements to:
a) Add characters anywhere inside a string.
b) Delete characters anywhere inside a string.
c) Change characters anywhere inside a string.
d) All of the above.
4) When S$="54321", which statement would change the string value to "12344321"
a) S$(1:1)="1234"
b) S$(5)="1234"
c) S$(5:5)="1234"
5) When S$="54321", which statement would change the string value to "521"
a) S$(2:3)="43"
b) S$(2)="21"
c) S$(2:3)=""
4.5 String Functions and Numeric Functions
For future reference: When we say BR returns something, we mean that BR performs some operation, after which a number or a string is returned to the place in your program where this operation is located
Examples
1) 3 + 5 returns 8 after the addition operation is carried out
2) “abc” & “def” returns “abcdef” after the concatenation operation is carried out
3) len(“longword”) returns the number 7 after the len function is carried out
A user-defined function in BR is a portion of code within a program that performs a specific task and is relatively independent of the remaining code. User-defined functions will be discussed later.
An internal function in BR works the same as a user-defined function, but the code for this function has been built into the language. Internal functions may be used directly without defining what they do. In fact the programmer cannot change the way internal functions behave. Internal functions may also be referred to as system functions or built-in functions.
All functions in BR return either a string or a number, and hence are either called string functions or numeric functions.
The following are all string functions: RPT$, DATE$,TIME$, LWRC$, UPRC$, SREP$, RPAD$,LPAD$. Here are some examples of how these string functions are used:
1) Example of the RPT$ function, which returns a string repeated a specified number of times:
print RPT$(“+=”, 10) ! prints the “+=” string 10 times
Result:
+=+=+=+=+=+=+=+=+=+=
2) UPRC$ example:
print UPRC$(‘this will print in uppercase letters’)
Result:
THIS WILL PRINT IN UPPERCASE LETTERS
To make functions more clear, we should define certain aspects of how functions work.
When you use a function in your program, this is referred to as a function call.
In the following example, print DATE$ is a statement. Within this statement, DATE$ is a function call:
print DATE$
In another example, UPRC$(“upper”) is a function call to the BR internal function UPRC$, which converts strings to upper case:
print UPRC$(“upper”)
Result:
UPPER
Note, that there is a difference between a function name and a function call. In the example above, UPRC$ is the function name, while UPRC$(“upper”) is the function call. A function call not only includes the function name, but may also include parameters (also known as arguments).
Function parameters, also known as function arguments, are string or numeric values passed to a function in a function call. Such parameters are enclosed in parentheses and separated by commas. Not all functions require parameters.
For example, the RPT$ function takes 2 arguments: a string to repeat, and the desired number of repetitions.
Syntax:
RPT$(string_to_repeat$, number_of_repetitions)
Example:
RPT$ ( “az”, 2)
Result:
azaz
Although most of Business Rules!'s built-in functions include parentheses and operate on the parameters passed to them, there are a few that have no parentheses and no parameters. TIME$ and DATE$ are good examples of system functions which do not need parentheses. TIME$ generates the current system time which is set on your own computer or a server that you are logged into (depending on whether BR! is installed on your computer or on the server). If Business Rules! is installed on your own computer, then it will be linked to the time you have set on your computer. Type in the following:
PRINT TIME$
Business Rules! will respond with 8 characters in the format hh:mm:ss. The first two digits are the hour of the day on a 24-hour clock (6:21pm and 39 seconds is 18:21:39). The middle two digits are the minutes. The last two digits are the seconds.
Now type this statement:
PRINT DATE$
Again, Business Rules! will generate an 8-character string, but this one tells you about the current system date. The format of this date string will be yy/mm/dd. The first two digits yy contain the last two digits of the year. The month is found in the two digits in the middle. The last two digits represent the day of the month. It should be mentioned that extended date functions exist, but they won't be described in detail here.
There are many more numeric and string functions functions in BR. Instead of giving you a long list now, we have sprinkled them throughout this tutorial, placing them in sections where you are likely to use them. Look for them as you go. You can also look for them in the wiki or in the BR help, which may be launched by pressing the F1 key at the BR prompt.
Date and Time Command
Although the system date and time are normally set from the operating system, it is easy for you to change them from Business Rules! using the DATE and TIME commands. On multi-user systems, you can only change the time and date from your terminal. Other users will not be affected by this.
Both of these commands have one optional string parameter. Without this parameter, both report the current system value, but when the parameter is included, the values are changed.
For example:
TIME 15:10:00
will reset the time to be 3:10pm (15:10 on a 24-hour clock).
Then when you type TIME, Business Rules! will reply with something like:
TIME 15:10:23
This reply assumes that you requested the time 23 seconds after changing the time.
The DATE command is very similar. Before you use this command, you must know that the date should be entered in mm/dd/yy format. For example:
DATE 12/25/86
will change the date to Christmas day of 1986. However, when you use the DATE command without any numbers, the format is like the DATE$ format, yy/mm/dd.
Do not confuse the DATE and TIME commands with the DATE$ and TIME$ functions. The difference is that the DATE and TIME commands are designed to set the system date and time, while the DATE$ and TIME$ functions are designed to get the system date and time.
Quick Quiz 4.5
True or False:
1) Names of string functions always end with a dollar sign, but names of numeric functions never end in a dollar sign.
2) The output of a string function is always a string.
3) RPT$(":<-->",40) will produce a string 40 characters long.
Chapter Exercise
1) Write a program to calculate how much an investment amount will be worth at an interest rate compounded monthly for a given number of years.
You should ask the user to type in 3 things:
a) The amount of the investment in dollars.
b) The interest rate in percent.
c) The number of years for the investment.
Here is the standard compound interest formula as a mathematician would write it:
A=P(1+r/n)^nt
where,
P=principle amount (initial investment)
r=annual interest rate (as a decimal)
n=number of times the interest is compounded per year
t=number of years
A=amount after time t
The formula for your program will assume monthly compounding of interest and use the following terms:
According to the standard compound interest formula above:
P = PRNCPL = principal amount of investment in dollars (input by user)
R = RATE = annual interest rate in percent, divided by 100 (otherwise times 0.01)
N = Given: 12 times a year (compounded monthly)
T = YRS = number of years (input by user)
A = BAL = Final balance you are trying to calculate. (Value of investment).
Variables for INPUT: PRNCPL, RATE, YRS.
Calculated variable: BAL
The challenge is simply putting this formula into terms that Business Rules! can understand. As an employed programmer, you will need to think of the formulas or algorithms for the problems you are trying to solve before you translate them into BR! language.
After you’re successful or give up, here's an example answer to compare yours with.