### 1. Relational Operators

• ```
----------------------------------
Operator    Name
----------  ----------------------
==          Equality
!=          Inequality
>           Greater Than
<           Less Than
>=          Greater Than Or Equal
<=          Less Than Or Equal
----------------------------------

```
• Relational operators formulate a boolean expression

• The result of a boolean expression is either true or false

• Boolean expressions are also called conditional expressions

### 2. Examples of conditional expressions

• The following boolean expressions can be useful in many control statements such as if that compare numeric data:

```
discountPercent == 2.3  // equal to a numeric literal
subtotal != 0           // not equal to a numeric literal
years > 0               // greater than a numeric literal
i < months              // less than a numeric variable
subtotal >= 500         // greater than or equal to a numeric literal
quantity <= reorderQty  // less than or equal to a numeric variable

```

### 3. if-else statement

• The syntax of the if-else statement is

```
if ( booleanExpression ) { statements }
[else if ( booleanExpression ) { statements }]<sub>optional</sub> ...
[else {statements}]<sub>optional</sub>

```
• The if statements compare primitive data types in a boolean expression

• Boolean expressions are enclosed in parentheses and use relational operators

### 4. An if without else

• The if statements without else-if or without else clause might looke like this:

1. With a single statement:

```
if (subtotal >= 100)
discountPercent = .2;

```
2. With a block of statements:

```
if (subtotal >= 100)
{
discountPercent = .2;
status = "Bulk rate";
}

```

### 5. if-else

• An if statement with an else clause:

```
if ( subtotal >= 100 ) {
discountPercent = .2;

} else {
discountPercent = .1;
}

```

### 6. Another if-else example

• ```
if ( age > 17 ) {
System.out.println("Eligible to vote.");
System.out.println("No longer a minor.");

} else {
System.out.println("Not eligible to vote.");
System.out.println("Still a minor.");
}

```

### 7. Comparing Strings

• Unfortunately, Java does not allow relational operators (>, <, >=, <=, ==, !=) to compare strings.

• In Java, Strings are objects, and objects have two methods for comparing Strings:

• equals(String) -- case sensitive

• equalsIgnoreCase(String) -- not case sensitive

• Both methods return a Boolean true or false

### 8. Comparing Strings Examples

• ```
userEntry.equals( "Y" ) // equal to a string literal

userEntry.equalsIgnoreCase( "Y" )
// equal to a string literal

( !lastName.equals( "Jones" ) )
// not equal to a string literal

code.equalsIgnoreCase( productCode )
// equal to another string variable

```

### 9. if with else-if

• An if statement with else-if and else clauses:

```
if ( customerType.equals( "T" ) ) {
discountPercent = .4;

} else if ( customerType.equals( "C" ) ) {
discountPercent = .2;

} else if ( subtotal >= 100 ) {
discountPercent = .2;

} else {
discountPercent = .1;
}

```

### 10. The while loop

• The syntax of the while loop is:

```
while( booleanExpression )
{
statements
}

```

### 11. Coding while statements

• A while statement repeats a block of {statements} as long as the boolean expression in the statement yields true.

• When the boolean expression becomes false the block is no longer executed; instead, the statements following the block are then executed

• Any variable defined within a block is visible only within that block. This also means that the variable is no longer reachable after the thread of execution leaves the block

### 12. While Statement Example

• The following demo code displays the total of all the numbers from 10 down to 1:

```
int x = 10;
int total = 0
while( x > 0 ) {
total = total + x;
x = x  1;
}
System.out.println( "Total = " + total );

```

### 13. An "Endless" Loop Example

• The user controls whether to continue to execute the block of code by entering "y" or "Y":

```
Scanner scnr = new Scanner( System.in );
String response = "y";

while( response.equalsIgnoreCase( "y" ) ) {
// several statements within the block
// ...
System.out.print( "Continue  Y/N?" );
response = scnr.next();
}

```

### 14. Another Loop Example

• A loop that calculates the sum of the numbers 1 through 4

```
int idx = 1;
int sum = 0;

while( idx < 5 ) {
sum = sum + idx;
idx = idx + 1;
}

```

### 15. Logical (Boolean) Operators

• ```
!       // logical NOT

&&      // logical AND

||      // logical OR

```

### 16. Numeric Ranges in Java

• Consider:

```
int value = 7;

if ( 1 < value < 10 ) {
System.out.println(
"The value "
+
value
+
" is greater than 1 and less than 10"
);
}

```

### 17. Numeric Ranges in Java, cont.

• The previos if statement is illegal in Java!

• The expression

```
1 < value < 10

```

is interpreted as

```
( 1 < 7 ) < 10

```

which then becomes

```
true < 10

```

which is illegal expression because Java does not allow comparing boolean values with other data types.

### 18. Numeric Ranges in Java, cont.

• The solution is to use the logical AND operator:

```
int value = 7;

if ( 1 < value && value < 10 ) {
System.out.println(
"The value "
+
value
+
" is greater than 1 and less than 10"
);
}

```