## Introduction: Variables & Expressions

Hello Ladies and Gentlemen,

Today In this lesson you will learn how to get the value of a certain variable, different ways of expression, the difference between each expression, and many others.

The Variables and the Expressions in Blue J are very similar to the ones with Java, so if you know Java, Blue J should be no problem for you...

P.S. Just to tell you guys that I will start the first Lesson for Blue J, which is an actual programming called "Object-Oriented- Programming" and (if you have blue J) you will need for this program to import something called: gpdraw*/. or something like that. It has to be typed at the very top (outside everything).

### Teacher Notes

Teachers! Did you use this instructable in your classroom?

Add a Teacher Note to share how you incorporated it into your lesson.

## Step 1: Getting a Value

As seen on the example provided above, the "equation" in the method "x = 5 , return x" Let's the program to know that "x" equals 5, but what is actually happening?

When we type : "x = 5" The "=" sign in Blue J doesn't just mean equals. The "=" sign in Blue J means that a Variable is getting The Value of an expression. (" x = 5 " is also known as "variable(x) = expression (5)" ).

Notice on the top: "private INT x;"- That means " x " is a whole number ( if we want to be 5.0, than we need to change the integer ("int") to a double ("double").

**Things to know:**

- " -x " doesn't need to be a 1 number, it can be an expression such as: x = 5 + 5; or x = 5 - 5;
- The integer (int) doesn't get a whole number, If we say that "x"
__get's the value__of 5/4, the output will be NOT 1.25, but ,instead, The output will be 1.( if you want to have the exact number, you will need to change the instance variable ("private int x;" into "private double x;"). - The assignment operator is right - associative. This means that the statement ( a = b = 5) is really solved in that order ( a = (b = 5)), which means that " b " gets the value of 5, and after that " a " gets the value of 5 too.
So the variable " 5 " is also assigned into " a ".

## Step 2: Quotes and " + ",and " - " Sign.

The "+" sign can play two different roles in a program. It can add two numbers together, or combine 2 sentences into one another. As seen on the example above, the second line : "y = x + 5;" the variable (y) gets the value of the expression (" x + 5 "). But at the 3rd line: "System.out.print(" " + y); Y is printed to the screen.

(optional) Notice how I inserted those : -" ", whatever it goes between these comas is printed to the screen and added with the number 10 ( + y) in other words the output will be "10" without anything else.

The placement of the quotes affects the output:

System.out.println (2 + 2); //Output : 4 System.out.println("2 + 2"); //Output : 2 + 2 System.out.println( "2" + "2"); //Output : 22

The " - " sign doesn't have a huge difference than the " + " sign when adding. Though it can not be used in the System.out.print.

**Things to know:**

- The placement of the quotes.
- Different ways of expressing the " + " and " - " sign. (For example :x = x+5; can be also expressed as x +=5;).
- If you are not adding/subtracting a whole number, you have to make sure your
**instance variable**is a double (not int). - The Program solves an equation from right to left( the opposite of the usual).

## Step 3: Division and Multiplication

(There is not that much to talk about them)

The multiplication sign is " * ". A star found at the top right side of the keyboard. In Blue J the multiplication sign (as in math ) is done before the adding / subtracting.

For all the operators, if both operands are integers, the result is an integer.

Examples:

2 + 3 --> 5

4 * 8 --> 32

9 - 3 --> 6

11/2--> 5

Notice that 11/2 is 5, and not 5.5. This is because integers (ints) work only with whole numbers. The remaining half (0.5) is lost in integer division.

If either of the operands is a double type, the result is a double type. Examples:

2 + 3.000 --> 5.000

25/ 6.75 -->3.7073

11.0/2.0 --> 5.5

When an integer and a double are used in a binary math expression, the integer is promoted to a double alue, and then the math is executed. in the example " 2 + 3.000 --> 5.000, the integer value 2 is promoted to a double (2--> 2.000) and then added to the 3.000.

## Step 4: The Modulus Operator + Solving Math Operators.

The modulus operator is the hardest one. It's symbol is known as % (which is SHIFT + 5). The modulus operator (%) returns the remainder of dividing the first operand by the second. Examples:

10 % 3 --> 1

16 % 2 --> 0

2 % 4--> 2

27.475 % 7.22 --> 5.815

In other words, whatever is leftover is the answer. as seen at the example: "16 % 2 --> 0".16 divided by 2 (16 / 2) is 8 has no left over.

But the example "10 % 3 --> 1" is 10 / 3 = 3 + 1 leftover. And that 1 leftover is our answer...

An example follows (everything is an INTEGER) :

9 + 16 / 3 * 7 % 8 - 5 (Solve / first)

9 + 5 * 7 % 8 - 5 (Solve * Second)

9 + 35 % 8 - 5 (Solve % next)

9 + 3 - 5 (Solve left -to- right)

7

## Step 5: P.S.

I know I am missing some stuff. Some important, some not, but I promise you I will put everything about BlueJ in this website until every little detail is expressed. You don't know everything ,yet, but I promise you that you will know how to program perfectly with no trouble, until an year. The next lesson will be about the data types. Thanks for all the support ( seriously I don't know where I am... I am on this website for less than 3 days :) ) And if there are any questions about something related to blue J, feel free to ask me... :)

## Discussions