8 - Boolean Operators
Created 3 years ago
Duration 0:04:53
Get an introduction to or review of Boolean operators in Java programming.
-
Embed
-
Social
Select the file type you wish to download
Slide Content
-
Slide 2 - Boolean Operators
-
Slide 3 - Boolean Operators
- To test more complex conditions in an if or if-else statement, we can use one of the three Boolean operators:
- && (and) – true if all parts are true
- || (or) – true if any parts are true
- ! (not) – true if condition is false
-
Slide 4 - Boolean Operators
- Some examples:
- true && true
- true && false
- (3 > 5) && (9 < 10)
- int x = 10;
- (x > 0) && (x != 20)
- (x >= 5) && (x < 10)
-
Slide 5 - Boolean Operators
- Some examples:
- true && true
- true && false
- (3 > 5) && (9 < 10)
- int x = 10;
- (x > 0) && (x != 20)
- (x >= 5) && (x < 10)
- true || false
- false || false
- (7 == 9) || (12 > 20)
- int y = -5;
- (y > 10) || (y < 0)
- (y > 0) || (y == -2)
-
Slide 6 - Boolean Operators
- Some examples:
- true && true
- true && false
- (3 > 5) && (9 < 10)
- int x = 10;
- (x > 0) && (x != 20)
- (x >= 5) && (x < 10)
- true || false
- false || false
- (7 == 9) || (12 > 20)
- int y = -5;
- (y > 10) || (y < 0)
- (y > 0) || (y == -2)
- !true
- !false
- !(3 < 2)
- int z = 7;
- !(z > 5)
- !(z <= 0)
-
Slide 7 - Boolean Operators
- Some examples:
- true && true
- true && false
- (3 > 5) && (9 < 10)
- int x = 10;
- (x > 0) && (x != 20)
- (x >= 5) && (x < 10)
- true || false
- false || false
- (7 == 9) || (12 > 20)
- int y = -5;
- (y > 10) || (y < 0)
- (y > 0) || (y == -2)
- !true
- !false
- !(3 < 2)
- int z = 7;
- !(z > 5)
- !(z <= 0)
- !(true && false)
- false || (true && true)
- (5 > 0) && !(7 < 0)
- int a = 4, b = 5;
- (a > 0) && (a != b)
-
Slide 8 - The Boolean operators can be used like any other expression, including in an if or if-else.
- Boolean Operators
- if (3 < 5 && 10 > 0) {
- System.out.println("Yes");
- }
- if (-4 < -8 || -9 < -3) {
- System.out.println("OK!");
- }
- int x = 7;
- if (x == 3 || x == 5) {
- x++;
- } else {
- x--;
- }
- System.out.println(x);
- String str = "Hello";
- if (!(str.length() > 8)) {
- System.out.println("Short");
- }