## Operators in JAVA

Decision Structure

Deceision structure are used so that the program doesn’t have to do the same thing each time a program is executed. They basically answer a question on the spot during execution. Decesion structure need a question and this is done through Boolean. As we have seen boolean are primitive data types. It represent two values true or false.

They can be set directly but is usually by using a set of operators including but not limited to relational and logical operators.

 Operator Meaning > Is greater than < Is less than >= Greater than or equal to <= Less than or equal to == Is equal to != Is not equal to

We can use these operatots in Boolean expressions. Table belows gives some common ways of using the relational operators :

 Operator Meaning X > Y Is X greater than Y ? X < Y Is X less than Y? X >= Y Is X Greater than or equal to Y ? X <= Y Is X Less than or equal to Y ? X == Y Is X equal to Y ? X != Y Is X not equal to Y ?

Logical Operatots

 Operator Meaning Effect Examole && AND Connects two Boolean expressions into one. Both expressions must be true for the overall expression to be true 5 < 4 && 7 > 5 In the above expression 5<4 is false but the expression on right hand i.e. 7 > 5 is true. Because both expression are not true. The overall expression results in false therefore 5 < 4 && 7 > 5 is false However 5 < 6 && 7>5 is true || OR Connects two Boolean expressions into one. One or both expressions must be true for the overall expression to be true. It is necessary for any one condition connected by || to be true for the whole expression What is 5 < 4 | | 7 > 5 ? left hand side 5<4 is false but right hand 7>5 expression is true, therefor the overall expression is true ! NOT This operator complements the value of the boolean expression. It results to false if applied to a true expression and vice versa. If the expression is true, then NOT true will be false !(5 < 4) will be true similarily if expression is false, then NOT false will be true !(7>5) will be false

Let us see some examples in Intellij:

```package com.Example;

public class decisionStructure
{
public static void main(String[] args) {

int x = 5;
int y = 6;
int z = 7;
boolean expression = x == y;
System.out.println("Expression:"+expression);
}
}```

Output:

Expressin: false

similarily we can now chnge the expression on right hand side of ‘=’ . Try writing the following expression and check your result

x > y

x> y && y< z            //will be false

x> y | | y< z              //will be true

x< y && y< z            //will be true

x== y || y== z           //will be false

!(x<y)                      // will be false as NOT of true is false and NOT of false is true

therefore a NOT operator changes the true value to false and vice versa

## Test for Range of numbers

&& and || operators can be used to test for inside range and outside range of numbers respectively.

### Testing for wihin range

We use logical && operators to test for within or inside range. here is an example to test for inside range; let us test for numbers between 30 and 60

30<—————>60

how to use if statement to determine this condition.

x >=30 && x <= 60)

Verify

let x=40 ; so we can verify the expression as:

Is x <= 60; Yes it is

So the number x=40 is within the range of numbers 30 to 60

### OR Operator- To test outside range of numbers

|| operator can be used to test a number for outside the range of numbers. For the logical  || expression to be true either or both side of || expressions must be true.

Example:

boolean expression = x < 25 || x > 50 will be evaluated as follows

let x=20

Is 20 <30 Yes, Is 20 > 50 No. As || evaluates to TRUE if either side is true. Thus the expression evaluates to true

therefore || expression tests for outside range of numbers.

error: Content is protected !!