forked from forax/java-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter05-control_flow.jsh
188 lines (159 loc) · 4.84 KB
/
chapter05-control_flow.jsh
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// To starts, run jshell --enable-preview which is a program able to interpret Java syntax
// then cut and paste the following lines to see how it works
// To exit jshell type /exit
// # Expression and Control Flow
// Most of the control flow syntax of Java comes from C with some enhancements
// ## Variables
// if you explicitly type a variable you can declare a variable without initializing it
int x;
// but a variable can be used only after being initialized
x = 3;
System.out.println(x);
// block of code
// a variable declared in a block of code, can not be used outside that block
{
var value = 42;
}
// value can not be used here !
// ## If
// ### Test with `if`
// the construct `if` execute the block of code that follow if the condition in between
// the parenthesis is true
void oldEnough(int age) {
if (age >= 21) {
System.out.println("you are old enough to drink a beer");
}
}
oldEnough(22);
// ### Test with `if ... else`
// you can append the construct `else` after an `if` to execute a block of code
// if the condition is not true
void oldEnough(int age) {
if (age >= 21) {
System.out.println("you are old enough to drink a beer");
} else {
System.out.println("too bad for you !");
}
}
oldEnough(17);
// ## Switch
// ### Test with a `switch` statement
// There are two forms of switch, a switch statement is a switch that doesn't
// produce a value. For those, the `default` case is not mandatory
void vehicle(int wheels) {
switch(wheels) {
case 1 -> System.out.println("monocycle !");
case 2 -> System.out.println("bicycle !");
case 3, 4 -> System.out.println("car !");
default -> {
// if there are several lines
System.out.println("whaat !");
}
}
}
vehicle(3);
// ### Test with a `switch` expression
// A switch that produces a valeur is a switch expresssion. Given that a
// value need to be produced, a `default` case is mandatory
String vehicle(int wheels) {
return switch(wheels) {
case 1 -> "monocycle !";
case 2 -> "bicycle !";
case 3, 4 -> "car !";
default -> "whaat !";
};
}
System.out.println(vehicle(3));
// you can switch on integers, strings and enums
int doors(String kind) {
return switch(kind) {
case "smart" -> 3;
case "sedan", "hatchback" -> 5;
default -> { throw new IllegalArgumentException(kind); }
};
}
System.out.println(doors("sedan"));
// ### Test with a `switch` compatible with C
// You can use the C compatible switch too, using `:` instead of `->`
// (you can not mix them) but in that case don't forget to ends
// each case with a `break`.
void vehicle(int wheels) {
switch(wheels) {
case 1:
System.out.println("monocycle !");
break;
case 2:
System.out.println("bicycle !");
break;
case 3:
case 4:
System.out.println("car !");
break;
default:
System.out.println("whaat !");
}
}
vehicle(3);
// ## Instanceof
// ### `instanceof`
// instanceof test the class of a value at runtime, if instanceof succeeds,
// the value is stored in the variable declared as last argument
record Car(int seats) {}
record Bus(int capacity) {}
int maxPersons(Object value) {
if (value instanceof Car car) {
return car.seats();
}
if (value instanceof Bus bus) {
return bus.capacity();
}
return 0;
}
System.out.println(maxPersons(new Car(4)));
System.out.println(maxPersons(new Bus(32)));
// ### `instanceof` with no variable declaration
// if you don't need the variable declaration, you can omit it
void printKind(Object value) {
if (value instanceof Car) {
System.out.println("it a car");
}
if (value instanceof Bus bus) {
System.out.println("it a bus");
}
}
printKind(new Car(4));
// ## Loops
// ### `while` loop
// a `while` loop execute the block of code while the condition in between parenthesis is true
void printFirstIntegers(int n) {
var i = 0;
while(i < n) {
System.out.println(i);
i++;
}
}
printFirstIntegers(5);
// ### `for` loop
// a for loop is a convenient way to write a `while` loop in case you do a while on a variable,
// so instead of using the `while` loop above, one can write this for loop
void printFirstIntegers(int n) {
for(var i = 0; i < n; i++) {
System.out.println(i);
}
}
printFirstIntegers(5);
// ### `for` loop on array or list
// Java as a special loop for iterating over the content of an array or a list,
// it using the keyword `for`, but the declared variable contains each element one by one
var list = List.of("iron man", "captain america", "black panther");
for(var value: list) {
System.out.println(value);
}
// ### On loops
// Most of the loops can also be abstracted using higher order constructs
// if you don't understand that code now, don't panic, we will come back
// to that later
// using `IntStream.range()`
IntStream.range(0, 5).forEach(System.out::println);
// using `List.forEach()`
list.forEach(System.out::println);