-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotes3.java
74 lines (61 loc) · 2.19 KB
/
Notes3.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
/*
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TOPICS :: Conditional statements, Switch, Loops(For, While, Do-While, For-each), break/continue.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
*/
public class Notes3 {
public static void main(String[] args) {
// comparision operators: ==, !=. >=, <=, >, <
// logical operators: &&, ||, !
// conditional statements
int age = 21;
if(age<18) System.out.println("Minor");
else if (age>=18 && age<50) {
System.out.println("Mid age: " + ++age); // Output > Mid age: 22
}
else System.out.println("Old");
String ternary = (10 >= 20) ? "Maths!!" : "Stupid!!"; //ternary operator
System.out.println(ternary); // Output > "Stupid!!"
// switch statements
String sw1 = "mentor";
switch(sw1){
case "admin":
System.out.println("Its Admin.");
break;
case "student":
System.out.println("Its Student.");
break;
case "mentor":
System.out.println("Its Mentor."); // Output > "Its Mentor."
break;
default:
System.out.println("Guest");
}
// We cannot use '!=' or '==' to compare two strings as they are of reference type.
String nm = "DSR";
System.out.println(nm.equals("DSR")); // Output > true
// For loop
for(int i=0; i<1; i++){
System.out.println("Crazy"); // Output > "Crazy"
}
// While loop
while(true)
{
break;
}
// Do While loop
do {
age++;
} while(!true);
// For-each loop
String[] fruits = {"Apple", "Mango", "Orange"};
for(String fruit : fruits){
System.out.print(fruit); // Ouput > AppleMangoOrange
}
System.out.println();
/*
break -> to end a loop, switch condition
continue -> to bring at the loop beginning
*/
}
}