0% found this document useful (0 votes)
10 views

Java Lesson 2

Uploaded by

ronald.greaves
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Java Lesson 2

Uploaded by

ronald.greaves
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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
System.out.println(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 java.util.*; Scanner input=new
public class helloworld { Scanner(System.in);
String i;
int j;
public static void
main(String[] args) { System.out.println("Enter
// TODO Auto- your username: ");
generated method stub i=input.nextLine();
Scanner input=new
if(i.equals("Danielle
Scanner(System.in); Jackson")){
//String i;
int j; System.out.println("Enter
a number: ");

System.out.println("Please }else{
enter your age: ");
j=input.nextInt();
System.out.println("incorrect
username ");
if(j<9){ }

System.out.println("You If construct using Boolean operators


are too young: Goodbye!
");
import java.util.*;
}else{ public class helloworld {

public static void


System.out.println("Welcom main(String[] args) {
e! "); Scanner input=new
Scanner(System.in);
} String i;
int j;

If using the .equals operator System.out.println("Enter


your username: ");
import java.util.*; i=input.nextLine();
System.out.println("Enter // TODO Auto-
your age: "); generated method stub
j=input.nextInt(); int j=0;
while(j<10){
if(i.equals("Danielle
Jackson")&& (j>9)){ System.out.println(j);
j++;
}
System.out.println("Welcome! System.out.println("here
"); ends the loop");

}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

public static void


main(String[] args) {
// TODO Auto-
generated method stub
int j=9;
while(j<=9){

System.out.println("hello"
);

}
A while loop with
increment operator
public static void
main(String[] args) {

You might also like