6.6 Statements in KJP


6.6 Statements in KJP

In programming languages, statements are used for data declarations and for writing the instructions. KJP is an object-oriented programming language that is an enhancement of informal pseudo-code. This section mainly deals with the assignment and I/O statements.

6.6.1 Assignment and Arithmetic Expressions

An assignment statement is used to give a value to a variable, and this means that during execution of a program, the value of the variable changes. The keyword set must be included at the beginning of the assignment statement. There are two basic ways to give a new value to a variable:

  1. Simple assignment

  2. The result of evaluating an expression is assigned to a variable

The first kind of assignment statement simply gives a constant value to a variable. For example, given the following declarations:

            variables                 real length                 integer x 

The assignment statement to assign the constant value 45.85 to variable length is:

                 set length = 45.85 

In a similar manner, the assignment statement to give the value 54 to variable x is:

                 set x = 54 

In writing an assignment statement, the variable that is receiving the new value is always placed on the lefthand side of the equal sign, the assignment operator.

The second kind of assignment statement is a more general assignment, and is used when the value assigned to a variable is the result of evaluating an expression. For example, the value that results from evaluating the expression x + 4.95z is assigned to variable y. The assignment statement is:

                 set y = x + 4.95 * z 

6.6.2 I/O Statements

For console applications, the two simple statements for input/output require the keywords read, for input, and print, for output. The input statement allows the algorithm to read a value of a variable from the input device (e.g., the keyboard). The value is assigned to the variable indicated. The general structure of the input statement is:

                 read  variable_name  

For example, to read a value for variable length, the statement is:

                 read length 

This is similar to an assignment statement for variable length, because the variable changes its value to the new value that is read from the input device.

The output statement writes the value of a variable to the output device (e.g., the video screen). The variable does not change its value. The general structure of the output statement is:

                 display  data_list  

The data list consists of the list of data items separated by commas. For example, to print the value of variable x on the video screen unit, the statement is:

                 display x 

A more practical output statement that includes a string literal and the value of variable x is:

                 display "value of x is: ", x 

6.6.3 Other Statements with Simple Arithmetic

The following statements are similar to the ones previously discussed, and they involve only simple arithmetic operations.

                 add 24 to x                 increment j                 subtract x from y                 decrement counter_a 

The first statement takes the constant value 24, and adds it to the current value of variable x. The result of the addition becomes the new value of variable x. This statement requires the keyword add and is equivalent to the following assignment statement:

                 set x = x + 24 

On the righthand side of the equal sign (the assignment operator), the current value of variable x is used. The variable on the lefthand side of the assignment operator changes its value; variable x now has a new value.

The increment statement, increment j, adds the constant 1 to the current value of variable j. This statement is equivalent to the following assignment statement:

                 set j = j + 1 

The other two statements, subtract and decrement, are applied in a similar manner to the first two.

6.6.4 More Advanced Arithmetic Expressions

The arithmetic expressions used in the assignment statements discussed previously were very simple; only basic arithmetic operations appeared in the expressions. These are addition, subtraction, multiplication, and division.

To use a particular function of the Math library class, the name of the class must appear followed by a dot and then the name of the particular function invoked. For example, consider the value of the expression that is assigned to variable y. The expression in the assignment statement must use the mathematical function sqrt in class Math; the statement is:

                 set y = Math.sqrt(s) 

In a similar manner, to assign the value of the mathematical expression x2 to the variable interest, the complete assignment statement is:

                 set interest = Math.pow(x, 2) 

This statement invokes function pow from class Math, to raise the value of x to the power of two; the value of this expression is assigned to variable interest.

Note

Class Math is a predefined class and is part of the class library supplied with the Java compiler. This class provides several mathematical functions, such as square root, exponentiation, trigonometric, and other mathematical functions.




Object-Oriented Programming(c) From Problem Solving to Java
Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)
ISBN: 1584502878
EAN: 2147483647
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net