Skip to content

Commit

Permalink
add more meat
Browse files Browse the repository at this point in the history
  • Loading branch information
forax committed Jan 11, 2020
1 parent 9e6c82d commit 8fc352d
Show file tree
Hide file tree
Showing 3 changed files with 645 additions and 276 deletions.
50 changes: 37 additions & 13 deletions chapter05-control_flow.jsh
Expand Up @@ -5,7 +5,7 @@
// # Expression and Control Flow
// Most of the control flow syntax of Java comes from C with some enhancements

// ### Variables
// ## Variables
// if you explicitly type a variable you can declare a variable without initializing it
int x;

Expand All @@ -15,10 +15,13 @@ 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 !
{
var value = 42;
}
// value can not be used here !


// ## If

// ### Test with `if`
void oldEnough(int age) {
Expand All @@ -39,8 +42,11 @@ void oldEnough(int age) {
oldEnough(17);


// ## Switch

// ### Test with a `switch` statement
// default case is not mandatory
// 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 !");
Expand All @@ -49,13 +55,14 @@ void vehicle(int wheels) {
default -> {
// if there are several lines
System.out.println("whaat !");
}
}
}
}
vehicle(3);

// ### Test with a `switch` expression
// default case is mandatory
// 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 !";
Expand All @@ -71,13 +78,15 @@ int doors(String kind) {
return switch(kind) {
case "smart" -> 3;
case "sedan", "hatchback" -> 5;
default -> -1;
default -> { throw new IllegalArgumentException(kind); }
};
}
System.out.println(doors("sedan"));

// ### Test with a `switch` compatible with C
// (you can not mix `->` and `:` )
// 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:
Expand All @@ -97,10 +106,11 @@ void vehicle(int wheels) {
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
// 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) {
Expand All @@ -115,6 +125,20 @@ int maxPersons(Object value) {
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
void printFirstIntegers(int n) {
Expand Down
52 changes: 39 additions & 13 deletions guide/chapter05-control_flow.md
Expand Up @@ -2,7 +2,7 @@
# Expression and Control Flow
Most of the control flow syntax of Java comes from C with some enhancements

### Variables
## Variables
if you explicitly type a variable you can declare a variable without initializing it
```java
int x;
Expand All @@ -17,11 +17,14 @@ System.out.println(x);
block of code
a variable declared in a block of code, can not be used outside that block
```java
{
var value = 42;
}
{
var value = 42;
}
// value can not be used here !
```
value can not be used here !


## If

### Test with `if`
```java
Expand All @@ -46,8 +49,11 @@ oldEnough(17);
```


## Switch

### Test with a `switch` statement
default case is not mandatory
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
```java
void vehicle(int wheels) {
switch(wheels) {
Expand All @@ -57,14 +63,15 @@ void vehicle(int wheels) {
default -> {
// if there are several lines
System.out.println("whaat !");
}
}
}
}
vehicle(3);
```

### Test with a `switch` expression
default case is mandatory
A switch that produces a valeur is a switch expresssion. Given that a
value need to be produced, a `default` case is mandatory
```java
String vehicle(int wheels) {
return switch(wheels) {
Expand All @@ -83,14 +90,16 @@ int doors(String kind) {
return switch(kind) {
case "smart" -> 3;
case "sedan", "hatchback" -> 5;
default -> -1;
default -> { throw new IllegalArgumentException(kind); }
};
}
System.out.println(doors("sedan"));
```

### Test with a `switch` compatible with C
(you can not mix `->` and `:` )
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`.
```java
void vehicle(int wheels) {
switch(wheels) {
Expand All @@ -112,10 +121,11 @@ 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
instanceof test the class of a value at runtime, if instanceof succeeds,
the value is stored in the variable declared as last argument
```java
record Car(int seats) {}
record Bus(int capacity) {}
Expand All @@ -132,6 +142,22 @@ 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
```java
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
```java
Expand Down

0 comments on commit 8fc352d

Please sign in to comment.