Sunteți pe pagina 1din 5

Computer Studies 2013 Syllabus

Arithmetic Operators (04)


Basic calculations, assignments, unary, type conversion and type casting.

Introduction
A very good idea is to plan the program before start coding it in Java. There are two ways of coding a program; pseudo-code or flowcharts. Pseudo-code use English like statements to plot the program whilst flowcharts use graphical symbols. The following program could have been planned using either method as shown below.

Java Program
class VariablesExample { public static void main (String args[]){ //variables are declared are assigned int N1 = 50; int N2 = 13; int tot; //the total of variables N1 and N2 //is stored in tot tot = N1 + N2; //Finally, we can show the result System.out.println(tot); } }

Mr. A. Gatt

Page 1 of 5

Computer Studies 2013 Syllabus

Pseudo Code 1. Start 2. Store 50 in N1. 3. Store 13 in N2. 4. Add N1 to N2 and store result in tot. 5. Display the value in tot. 6. Stop

Flowchart Start N1 = 50 N2 = 13

tot = N1 + N2

Display tot

End

As you can see from the flowchart there are different symbols which can be used as shown in the next table. However, we will see them in more detail later on.

Symbol

Name Terminator

Use Starts and ends a program.

Process

Processing is anything done by the program which doesnt require input or output, such as calculations and variable declaration and assignments.

Decision

Whenever a comparison must be done, the decision symbol must be used and it must have two outputs which are YES or NO. If the program calls for a method, this symbol must be used.

Method

Mr. A. Gatt

Page 2 of 5

Computer Studies 2013 Syllabus

Connector

If a shape has more than one input, group them in a connector beforehand.

Off-Page Connector

If the flowchart does not fit on a single page it must be continued on a different page using an off-page connector.

Input / Output

Whenever user input is required or something is outputted this symbol must be used.

Arithmetic Operators
The basic arithmetic operators are + (addition), - (subtraction), / (division), * (multiplication) and % (remainder or modulus). These are called binary operators because they involve two variables. Then there are the unary operators which require only one variable. All of these operators can be seen in the next table.

Operator + * / % ++ -+= -= *= /= %=

Equivalent

Operation Addition Subtraction Multiplication Division Remainder

Type

Binary

x =x + 1 x =x 1 x=x+y x=xy x =x * y x = x /y x =x % y

Increment (add 1) Decrement (subtract 1) Add y to x Subtract y from x Multiply x by y Divide x by y Divide x by y and get the remainder Unary

Mr. A. Gatt

Page 3 of 5

Computer Studies 2013 Syllabus

So there are various ways of writing the same arithmetic statement. The following table illustrates the difference.

Operation A x=x+1 x=x1 x =x + 8 x = x 90 x=x*8 x = x / 12 is the same as

Operation B x++ x-x+=8 x-=90 x*=8 x/=12

Also keep in mind the precedence of operators; this means that if various operators are combined in the same formula, they are worked out in the following manner: 1. Multiplication 2. Division 3. Remainder 4. Addition 5. Subtraction So if we have: result = 10 + 4 * 3 / 2; It is worked out as follows: 10 + 4 * 3 / 2 10 + 12 / 2 10 + 6 16 However, brackets can be used which override precedence. So (10+4) * 3 / 2 will be worked out as: (10 + 4) * 3 / 2 (14) * 3 / 2 42 / 2 21 And as you can see its a totally different result.

Mr. A. Gatt

Page 4 of 5

Computer Studies 2013 Syllabus

Activities
1. Draw a simple flowchart for the following program.
class averageTwo { public static void main (String args[]) { int n1 = 56; int n2 = 12; float avg = (n1 + n2) / 2; System.out.println(Average is +avg); } }

2. What is the value of x in the following calculations? a. X = 5%3; b. X = 4 * 6 + (3 9); c. X += 6;

3. Write a program that stores your year of birth in a variable. Then use a calculation to show your actual age.

4. Write a program that shows the result of four calculations (+, -, / and *) of two numbers. The results must be in the following format.
Two Numbers are 5 and 6 Addition Subtraction Multiplication Division : 11 : -1 : 30 : 0.833

***

Mr. A. Gatt

Page 5 of 5

S-ar putea să vă placă și