Java lesson 2
Boolean expressions
Relational operators Tests Boolean operators
● Relational operators are used to test
whether two values are equal,
Boolean a= (2>9);
whether one value is greater than
[Link](a);
another, and so forth.
● The operators are:
A == B Is A "equal to" B?
A != B Is A "not equal to" B?
If construct
A < B Is A "less than" B?
A > B Is A "greater than" B? ● The If construct works with
A <= B Is A "less than or equal to" B? a condition with the use of
A >= B Is A "greater than or equal to" B?
● Most of these operators are only
relational and Boolean
applicable when using numerical operators.
variables with the exception of ● Relational operators are
● You can legally use == and !=
to compare Strings, used only with numerical
variables. However when
Boolean operators
using string or char variables
● In Java, the boolean operator
“and” is represented by &&.
the methods .equals
● The && operator is used to or .matches
combine two boolean values. The
result is also a boolean value.
The result is true if both of the
combined values are true, and
the result is false if either of the
combined values is false.
● For example, “(x == 0) && (y ==
0)” is true if and only if both x is
equal to 0 and y is equal to 0.
● The boolean operator “or” is represented
by ||. (That’s supposed to be two of the
vertical line characters, |.) The expression
“A || B” is true if either A is true or B is
true, or if both are true. “A || B” is false
only if both A and B are false.
The output will return whether public class helloworld {
the variable is true or false. public static void
main(String[] args) {
If using the relational operators // TODO Auto-
generated method stub
import [Link].*; Scanner input=new
public class helloworld { Scanner([Link]);
String i;
int j;
public static void
main(String[] args) { [Link]("Enter
// TODO Auto- your username: ");
generated method stub i=[Link]();
Scanner input=new
if([Link]("Danielle
Scanner([Link]); Jackson")){
//String i;
int j; [Link]("Enter
a number: ");
[Link]("Please }else{
enter your age: ");
j=[Link]();
[Link]("incorrect
username ");
if(j<9){ }
[Link]("You If construct using Boolean operators
are too young: Goodbye!
");
import [Link].*;
}else{ public class helloworld {
public static void
[Link]("Welcom main(String[] args) {
e! "); Scanner input=new
Scanner([Link]);
} String i;
int j;
If using the .equals operator [Link]("Enter
your username: ");
import [Link].*; i=[Link]();
[Link]("Enter // TODO Auto-
your age: "); generated method stub
j=[Link](); int j=0;
while(j<10){
if([Link]("Danielle
Jackson")&& (j>9)){ [Link](j);
j++;
}
[Link]("Welcome! [Link]("here
"); ends the loop");
}else{
For loop
[Link]("Try
again "); import [Link].*;
} public class helloworld {
Loops---while, for
public static void
The while loop main(String[] args) {
● Uses a condition also and loops
for(int j=10;j<20;j++){
the return for a number of times [Link](j);
}
A while loop with no
ending statement
public static void
main(String[] args) {
// TODO Auto-
generated method stub
int j=9;
while(j<=9){
[Link]("hello"
);
}
A while loop with
increment operator
public static void
main(String[] args) {