This document provides an overview of operators, selection statements, and looping statements in Java. It begins with a breakdown of the different types of operators in Java including assignment, relational, arithmetic, conditional, bitwise, and logical operators. It then covers selection statements such as if-else statements and switch statements. Finally, it discusses looping statements including for, while, do-while loops as well as the continue and break statements. It also includes brief sections on arrays and arrays of arrays in Java.
3. Agenda
• 10 Selection Statement - If
• 11 Selection Statement - Switch
• 12 Selection Statement - Conditional
• 13 Looping Statement - For
• 14 Looping Statement - While
• 15 Looping Statement - Do While
• 16 Continue Statement
• 17 Break Statement
• 18 Basic of Arrays
• 19 Arrays of Arrays
4. Operator
• An operator is a punctuation mark that says to
do something to two (or three) operands
• An example is the expression "a * b". The "*" is
the multiplication operator, and "a" and "b" are
the operands
6. Operator - Assignment
• Using notation equal (=)
• This Operator using two operand, left operand
and right operand
• Expression on the right operand is evaluated
and the result stored in left operand
7. Operator - Assignment
int num1 = 5; // Assign value 5 to num1
int num2 = num2 + 5; // Add value 5 to num2
int num3 += 5; // Add value 5 to num3
int num4 = num4 - 5; // Substract 5 to num4
int num5 -= 5; // Substract 5 to num5
int num6 = num6 * 5; // Multiply 5 to num6
int num7 *= 5; // Multiply 5 to num7
int num8 = num8 / 5; // Divide 5 to num8
int num9 /= 5; // Divide 5 to num9
8. Operator - Relational
• Relational Operator always give a boolean result
(true or false)
• 6 Relational Operator:
Operator Description
< Less than
> More than
<= Less than or Equal
>= More than or Equal
== Comparison
!= Not equal
9. Operator - Relational
int num1 = 5;
if (num1 < 5) {
System.out.println(“Less than 5”);
}
if (num1 > 5) {
System.out.println(“More than 5”);
}
10. Operator - Relational
int num1 = 5;
if (num1 <= 5) {
System.out.println(“Less than or equal 5”);
}
if (num1 >= 5) {
System.out.println(“More than or equal 5”);
}
11. Operator - Relational
int num1 = 5;
if (num1 == 5) {
System.out.println(“Equal 5”);
}
if (num1 != 5) {
System.out.println(“Not equal 5”);
}
12. Operator – Instance Of
class Vehicle {}
class Car extends Vehicle {}
class Mercedes extends Car {}
Vehicle v = new Vehicle();
Mercedes m = new Mercedes();
if (m instanceof Vehicle) {
System.out.println(“m is Vehicle");
}
if (v instanceof Mercedes) {
System.out.println(“v is Mercedes");
}
13. Operator - Arithmetic
• Arithmetic Operator just like a Math!!!
• 7 Arithmetic Operator:
Operator Description
+ Addition
- Substraction
* Multiplication
/ Divide
% Modulus
++ Increment by 1
-- Decrement by 1
14. Operator - Arithmetic
int addition = 5 + 5;
int substraction = 5 - 5;
int multiplication = 5 * 5;
int divide = 5 / 5;
int modulus = 5 % 5; // 100 % 17 = ?
int num = 5;
int preIncrement = ++num;
int postIncrement = num++;
int preDecrement = --num;
int postDecrement = num++;
15. Operator - Conditional
• Also called Ternary Operator
• This operator using notation ? and :
• Example:
int num1 = (5 == 5) ? 0 : 1;
int num2 = (5 != 5) ? 0 : 1;
int num3 = (5 < 5) ? 0 : 1;
int num4 = (5 > 5) ? 0 : 1;
16. Operator - Bitwise
int a = 60; /* 60 = 0011 1100 */
int b = 13; /* 13 = 0000 1101 */
c = a & b; /* 12 = 0000 1100 */
c = a | b; /* 61 = 0011 1101 */
c = a ^ b; /* 49 = 0011 0001 */
c = ~a; /*-61 = 1100 0011 */
c = a << 2; /* 240 = 1111 0000 */
c = a >> 2; /* 215 = 1111 */
c = a >>> 2; /* 215 = 0000 1111 */
17. Operator - Logical
• To combine other operator
• 2 Relational Operator:
Operator Description
&& And
|| Or
18. Operator – Logical And
• AND Combination table
Operator 1 Operator 2 Result
False False False
False True False
True False False
True True True
19. Operator – Logical And
• Example:
int num = 5;
if (num == 6 && num > 10) { // false and false
System.out.println(“Hari Christian”);
}
if (num == 6 && num > 2) { // false and true
System.out.println(“Hari Christian”);
}
20. Operator – Logical And
• Example:
int num = 5;
if (num == 5 && num > 10) { // true and false
System.out.println(“Hari Christian”);
}
if (num == 5 && num > 2) { // true and true
System.out.println(“Hari Christian”);
}
21. Operator – Logical Or
• OR Combination table
Operator 1 Operator 2 Result
False False False
False True True
True False True
True True True
22. Operator – Logical Or
• Example:
int num = 5;
if (num == 6 || num > 10) { // false and false
System.out.println(“Hari Christian”);
}
if (num == 6 || num > 2) { // false and true
System.out.println(“Hari Christian”);
}
23. Operator – Logical Or
• Example:
int num = 5;
if (num == 5 || num > 10) { // true and false
System.out.println(“Hari Christian”);
}
if (num == 5 || num > 2) { // true and true
System.out.println(“Hari Christian”);
}
24. Operator - Precedence
– calories
Symbol Note Precedence
++ -- pre-increment or decrement 16
++ -- post-increment or decrement 15
~ flip the bits of an integer 15
! logical not 14
- + arithmetic negation or plus 14
(typename) type conversion or cast 13
* / % multiplicative operators 12
- + additive operators 11
<< >> >>> left and right bitwise shift 10
26. Operator - Associativity
– calories
Symbol Note Precedence Associativity
++ -- pre-increment or decrement 16 right
++ -- post-increment or decrement 15 left
~ flip the bits of an integer 15 right
! logical not 14 right
- + arithmetic negation or plus 14 right
(typename) type conversion or cast 13 right
* / % multiplicative operators 12 left
- + additive operators 11 left
<< >> >>> left and right bitwise shift 10 left
27. Operator - Associativity
– calories
Symbol Note Precedence Associativity
instanceof
< <= > >=
relational operators 9 left
== != equality operators 8 left
& bitwise and 7 left
^ bitwise exclusive or 6 left
| bitwise inclusive or 5 left
&& conditional and 4 left
|| conditional or 3 left
? : conditional operator 2 right
= *= /= %= += -
= <<= >>=
>>>= &= ^= |=
assignment operators 1 right
28. Selection Statement - IF
• Format:
if ( Expression ) Statement [ else Statement ]
• Explanation:
Expression must have boolean type
Using { } to make more than 1 statement
32. Selection Statement - IF
• Example:
int number = 5;
if (number == 5) {
System.out.println(“PT”);
}
if (number > 2) {
System.out.println(“GUNATRONIKATAMA”);
}
if (number > 5) {
System.out.println(“CIPTA”);
} else {
System.out.println(“LESTARI”);
}
33. Selection Statement - IF
• Example:
int number = 5;
if (number == 5) {
System.out.println(“PT”);
} else if (number > 2) {
System.out.println(“GUNATRONIKATAMA”);
} else if (number > 4) {
System.out.println(“CIPTA”);
} else {
System.out.println(“LESTARI”);
}
34. Selection Statement - IF
• Example:
int number = 5;
String result = number == 1 ? “a” : “b”;
OR
String result = number == 1 ? “satu” : number == 2? “dua” :
“bukan dua”;
System.out.println(“number = “ + number);
35. Selection Statement - Switch
• Format:
switch ( Expression ) {
case constant_1 : Statement; break;
case constant_2 : Statement; break;
case constant_3 : Statement; break;
case constant_n : Statement; break;
default : Statement; break;
}
36. Selection Statement - Switch
• Explanation:
Cannot have same expression
Default is optional, but bear in mind there can be only
one default
Default doesn’t have to be last
37. Selection Statement - Switch
• Example:
int number = 2;
String a = “”;
switch (x) {
case 1: a = “PT”;
case 2: a = “GUNATRONIKATAMA”;
case 3: a = “CIPTA”;
case 4: a = “JAYA”;
case 5: a = “LESTARI”;
default: a = “MAKMUR”;
}
38. Selection Statement - Switch
• Example:
int number = 2;
String a = “”;
switch (x) {
case 1: a = “PT”; break;
case 2: a = “GUNATRONIKATAMA”; break;
case 3: a = “CIPTA”; break;
case 4: a = “JAYA”; break;
case 5: a = “LESTARI”; break;
default: a = “MAKMUR”; break;
}
39. Looping Statement - For
• Format:
for ( Initial; Test; Increment ) Statement
• New Format (since Java 5):
for ( Object object : Array) Statement
40. Looping Statement - For
• Example:
int i;
for (i = 0; i < 10; i++) {
// loop body
}
OR
for (int j = 0; j < 10; j++) {
// loop body
}
41. Looping Statement - For
• Example:
for (int i = 0; i < employees.size(); i++) {
Employee e = employees.get(i);
e.getName();
}
OR
for (Employee e : employees) { // new
e.getName();
}
43. Looping Statement - While
• Format:
while ( Expression ) Statement
• Explanation:
While boolean Expression remain true, the
Statement is executed
If Expression is false on first evaluation, the
Statement will not execute
44. Looping Statement - While
• Example:
int i = 0;
while (i < 10) {
// loop body
i++;
}
45. Looping Statement – Do While
• Format:
do Statement while ( Expression )
• Explanation:
While boolean Expression remain true, the
Statement is executed
If Expression is false on first evaluation, the
Statement will execute once
46. Looping Statement – Do While
• Example:
int i = 0;
do {
// loop body
i++;
} while (i < 10) ;
47. Looping Statement – Continue
• Format:
continue;
continue Identifier;
• Explanation:
Continue occur only in loops
When a continue statement executed, it will pass to
the next iteration of the loop
48. Looping Statement - Continue
• Example:
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
}
49. Looping Statement - Continue
• Example:
gasi: for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j == 5) continue gasi;
}
}
50. Looping Statement – Break
• Format:
break;
break Identifier;
• Explanation:
When a break statement executed, it will break or
exit the loop
51. Looping Statement - Break
• Example:
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
}
52. Looping Statement - Break
• Example:
gasi: for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j == 5) break gasi;
}
}
53. Looping Statement - Break
• Example:
for (int i = 0; i < 10; i++) {
System.out.println(“i = “ + i);
for (int j = 0; j < 5; j++) {
System.out.println(“j = “ + j);
if (j == 2) break;
}
if (i == 3) break;
}
54. Basic of Arrays
• Arrays are objects it means array types are reference
types, and your array variable is really a reference to an
array
• Here are some ways in which arrays are like objects:
– They are objects because the language specification says so
("An object is a class instance or an array", section 4.3.1)
– Array types are reference types, just like object types
– Arrays are allocated with the "new" operator, similar to
constructors
– Arrays are always allocated in the heap part of memory, never in
the stack part of memory. Objects follow the same rule
55. Basic of Arrays
• We can define an Arrays of:
– Primitive Data Type
– Object
• Index of Array ALWAYS start with zero
• To get size of Arrays we use array.length
56. Basic of Arrays
• Declare an Arrays:
// Recommended
int[] data;
int[] data1, data2, data3;
// Not recommended
int data[];
int data1[], data2[], data3[];
57. Basic of Arrays
• Initialize an Arrays:
int[] data;
data = new int[10]; // Valid
data = new int[]; // Invalid
int[] data = new int[10]; // Valid
int[10] data = new int[10]; // Invalid
int data[10] = new int[10]; // Invalid
int data[10]; // Invalid
58. Basic of Arrays
• Fill an Arrays:
int[] data = new int[5]; // Valid
data[0] = 5; // OK
data[1] = 6;
data[2] = 7;
data[3] = 8;
data[4] = 9; // OK … so far
data[5] = 10; // We have a problem here
59. Basic of Arrays
• Fill an Arrays:
int[] data = {6, 7, 8, 9, 10}; // Valid
int[] data = new int[] {6, 7, 8, 9, 10}; // Valid
int[] data = new int[5] {6, 7, 8, 9, 10}; // Invalid
int[] data = new int[5];
data = {6, 7, 8, 9, 10}; // Invalid
60. Arrays of Arrays
• In other language called Multi Dimension Arrays
– The Visual Basic language only has multidimensional
arrays, and only calls them multidimensional arrays
– The ANSI C standard says C has what other
languages call arrays of arrays, but it also calls these
multidimensional
– The Ada standard explicitly says arrays of arrays and
multidimensional arrays are different. The language
has both
– The Pascal standard says arrays of arrays and
multidimensional arrays are the same thing
61. Arrays of Arrays
• Declare an Arrays:
// Recommended
int[][] data;
int[][] data1, data2, data3;
// Not recommended
int data[][];
int data1[][], data2[][], data3[][];
62. Arrays of Arrays
• Initialize an Arrays:
int[][] data;
data = new int[10][10]; // Valid
data = new int[10][]; // Invalid
int[][] data = new int[10][10]; // Valid
int[10][10] data = new int[10][10]; // Invalid
int data[10][10] = new int[10][10]; // Invalid
int data[10][10]; // Invalid
63. Arrays of Arrays
• Fill an Arrays:
int[][] data = new int[3][3];
data[0][0] = 0;
data[0][1] = 1;
data[0][2] = 2;
data[1][0] = 0;
data[1][1] = 1;
data[1][2] = 2;
data[2][0] = 0;
data[2][1] = 1;
data[2][2] = 2;
64. Arrays of Arrays
• Fill an Arrays:
int[][] data = new int[3][];
data[0] = new int[1];
data[0][0] = 0;
data[1] = new int[2];
data[1][0] = 0;
data[1][1] = 1;
data[2] = new int[3];
data[2][0] = 0;
data[2][1] = 1;
data[2][2] = 2;
65. Arrays of Arrays
• Fill an Arrays:
int[][] data = {{0, 1}, {0, 1, 2}, {0, 1, 2, 3}};// Valid
int[][] data = new int[][] {{0, 1}, {0, 1}}; // Valid
int[][] data = new int[1][2] {{0, 1}}; // Invalid
int[][] data = new int[2][2];
data = {{0, 1}, {1, 2}, {2, 3}}; // Invalid