Java Lesson 2
Java Lesson 2
Boolean expressions
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 {
System.out.println("Please }else{
enter your age: ");
j=input.nextInt();
System.out.println("incorrect
username ");
if(j<9){ }
}else{
For loop
System.out.println("Try
again "); import java.util.*;
} 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 System.out.println(j);
}
A while loop with no
ending statement
System.out.println("hello"
);
}
A while loop with
increment operator
public static void
main(String[] args) {