Java Programming I on YouTube:
https://www.youtube.com/playlist?list=PLLIqcoGpl73iaXAtS_-V_Xdx3mhTzPwb5
---------------------------------- 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
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
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
The if statements without else-if or without else clause might looke like this:
With a single statement:
if (subtotal >= 100) discountPercent = .2;
With a block of statements:
if (subtotal >= 100) { discountPercent = .2; status = "Bulk rate"; }
An if statement with an else clause:
if ( subtotal >= 100 ) { discountPercent = .2; } else { discountPercent = .1; }
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."); }
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
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
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; }
The syntax of the while loop is:
while( booleanExpression ) { 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
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 );
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(); }
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; }
! // logical NOT && // logical AND || // logical OR
Consider:
int value = 7; if ( 1 < value < 10 ) { System.out.println( "The value " + value + " is greater than 1 and less than 10" ); }
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.
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" ); }