## Using operators and decision constructs in Java

**Module 1: Using operators and decision constructs.**

** **

**Objectives:**

- Identify relational and conditional operators.
- Create if and if / else constructs.
- Use the switch construct.

**1. Identify relational and conditional operators:**

In everyday life, we make many decisions. For instance, if its raining I will use umbrella so that I dont get wet. This is a type of branching. If one condition is true, that’s if raining, I take my umbrella. If the condition is not true but false, then I will not take my umbrella. This type of branching decision making can be implemented in Java programming using ‘if’ statements.

But before we can discuss about “if” statements, we need to know about relational operators. Because in Java to make a decisions you have to test the relationship between things.

** **

**Relational operators:**

The following is the relational table which shows the relational operators in Java. As the name relational implies, it says how 2 things are related to each other. All the operators listed here are binary operators. What this means is, they always work on 2 things which are left and right operands. These operators will compare the 2 operands together and returns is a boolean value, a true or false value. For ex, the == operator tells us if the 2 operands are equal, if they are it returns a true value, if not it returns a false value.

In the above example,

int i=1;

if(i==1), will return true.

Similarly we have different operators such as not equal to, less than, less than or equal to, is greater than and greater than or equal to operators. These operators are not going to sit by themselves and we are going to use them in the “if” statements.

** **

**Conditional operators:**

A lot of times in Java we just want to ask very simple question, such as ‘is x greater than 2’. But there are other times when we want to ask bit complicated questions. For example, in real life we might want to ask something like “if its raining and if its muddy outside, i will take my umbrella and my boots”. To do this in Java we use the conditional operators. We have AND, OR and NOT operators to implement the conditional operations as shown in the table.

The AND and the OR are binary operators just like the relational operators were, that means that they have left and right operands and they also return true / false. The NOT is a unary operator, it only takes 1 operand and it also returns a true / false.

For the AND operator to return true, both the left and the right operand have to evaluate to true. If either one of them are false, it will return a false. For the OR operator, if either side of the operator evaluate to true, then the operator returns a true. If both of the operands are false, then it will return false. For the NOT operator, it basically just reveres the boolean value of the single operand. That is if a NOT value is applied to a true value then it returns false, and if its applied to a false value then it returns true. We shall take a look at some examples to better understand them.

Lets start with the AND operator represented by double ampersand (&&)

ex:

int i = 2;

int j = 8;

An && operator needs a true/false value on the left hand side and a true/false value on the right hand side. So one way to do that is using relational expressions, because we know relational operators return true or false.

((i<1)&&(j>6))

So, here on the left hand side we have, “is i < 1”, and as i = 2 and hence i is not less than 1, we get false. So the whole expression now will be false. Java doesnt even have to evaluate the expression on the right hand side here because for && operator to return true, both the operands have to be true. So, the minute it finds a false value, it returns false.

true && true = true

true && false = false

false && false = false

false && true = false

Lets take a look at the OR example represented by (||)

ex:

int i = 2;

int j = 8;

Again, || operator needs both sides to be true / false values.

((i<1)||(j>=10))

So here, i<1 is false. At this point it cant determine if it has to return true / false for the whole expression. So, it also has to check the right hand side. So j>=10, this is false. So both sides of the OR expression is false. so it returns false.

true || true = true

true || false = true

false || false = false

false || true = true

Lets take a look at the NOT operator represented by (!)

ex:

int i = 2;

(!(i>3))

Here we are first asking the question, i>3. it is not. so its false. But here we are applying the NOT operator. So, NOT operator returns reverse which is true.

!true = false

!false = true

** **

** **

**2. Creating if and if / else constructs: Read more…**

## Exercise 1 – Using if and if else in Java

**Exercise – 1 for module 1 of Using operators and decision constructs:**

This post has the exercise – 1 for module 1 of Using operators and decision constructs . Let’s practice what we have learnt. Before practicing the exercise, follow the instructions below which will give you a step-by-step instruction:

You can download the source code (WJ-1103A-module1-exercise1.zip) for this exercise from the Box widget in the sidebar. Follow the video tutorial below if you ever get stuck while executing the programs. Read more…

## Exercise 2 – Using switch construct in Java

**Exercise – 2 for module 1 of Using operators and decision constructs:**

This post has the exercise – 2 for module 1 of Using operators and decision constructs. Let’s practice what we have learnt. Before practicing the exercise, follow the instructions below which will give you a step-by-step instruction:

You can download the source code (WJ-1103A-module1-exercise2.zip) for this exercise from the Box widget in the sidebar. Follow the video tutorial below if you ever get stuck while executing the programs. Read more…

## Recent Comments