-
Notifications
You must be signed in to change notification settings - Fork 0
/
tu02.java
87 lines (81 loc) · 3.21 KB
/
tu02.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//Conditionals
//If-else got started
/*
The if - else statements doesn't have to be in several lines with {}, if can be used in one line, or without the {},
for a single line stat-ment.
Java uses boolean variables to evaluate conditions.
The boolean values true and false are returned when an expression is compared or evaluated.
*/
public class tu02 {
public static void main(String[] args) {
//eg01
int a = 4, e = 8;
boolean b = a == 4;
boolean d = e == 4;
if (b) {
System.out.println("It's true! No problem.");
}
if (d) { //if-else Working in java
System.out.println("It's true! No problem.");
} else
System.out.println("It's WRONG! Do Again");
//Boolean operators
int x = 7;
int y = 6;
for(int i=0;i<9;i++) { //for loop also initialised
boolean resulti;
resulti = x < y;
System.out.println(resulti);// true
resulti = x > y; // false
System.out.println(resulti);
resulti = x <= 7; // a smaller or equal to 7 - true
System.out.println(resulti);
resulti = y >= 6; // b bigger or equal to 6 - false
System.out.println(resulti);
resulti = x == y; // a equal to b - false
System.out.println(resulti);
resulti = x != y; // x is not equal to y - true
System.out.println(resulti);
resulti = x > y || x < y; // Logical or - true
System.out.println(resulti);
resulti = 5 < x && x < 8; // Logical and - true
System.out.println(resulti);
resulti = !resulti; // Logical not - false
System.out.println(resulti);
}
int l = 49,m = 42,s = 56,t = 56;
if (l == m && s == t)
System.out.println("Another line Wow!");
else
System.out.println("Double Rainbow!");
//The ugly side of if
//
//There is A another way to write a one line if - else statement by using the operator ?
int aa = 4;
int result = aa == 4 ? 1 : 8;
// result will be 1
// This is equivalent to
int res21;
if (aa == 4) {
res21 = 1;
} else {
res21 = 8;
}
System.out.println(res21);
//== and equals
/*The operator == works a bit different on objects than on primitives.
When we are using objects and want to check if they are equal,
the operator == will say if they are the same, if you want to check if they are logically equal,
you should use the equals method on the object.**/
//For example:
String ab = new String("Wow");
String bb = new String("Wow");
String sameA = ab;
boolean r1 = ab == bb; // This is false, since a and b are not the same object
boolean r2 = ab.equals(bb); // This is true, since a and b are logically equals
boolean r3 = ab == sameA; // This is true, since a and sameA are really the same object
System.out.println(r1);
System.out.println(r2);
System.out.println(r3);
}
}