Red Chapter 3 answers

Chapter 3 exercises/answers.
Given that the wait on the user filling out the buffer for input would be enormous compared to the cost of a few extra conditions, I wasn't overly concerned with efficiency, but what if we _were_ heavily resource constrained?

**tl;dr** there's not a lot in it.

Here's how different options compare (compiled with OpenJDK 1.8.0):

```java
out = ch <= 'Z' && ch >= 'A' ? ch + 32 : ch;
out = ch <= 'z' && ch >= 'a' ? ch - 32 : out;
```
```assembly
       5: iload_2       
       6: bipush        90
       8: if_icmpgt     24
      11: iload_2       
      12: bipush        65
      14: if_icmplt     24
      17: iload_2       
      18: bipush        32
      20: iadd          
      21: goto          25
      24: iload_2       
      25: istore_3      
      26: iload_2       
      27: bipush        122
      29: if_icmpgt     45
      32: iload_2       
      33: bipush        97
      35: if_icmplt     45
      38: iload_2       
      39: bipush        32
      41: isub          
      42: goto          46
      45: iload_3       
      46: istore_3      
      47: iload_3       
      48: iload_2       
```
Next up regular `if-elsif-else`:
```java
if ( ch <= 'Z' && ch >= 'A' )
  out = ch + 32;
else if ( ch <= 'z' && ch >= 'a' )
  out = ch - 32;
else
  out = ch;
```
```assembly
       5: iload_2       
       6: bipush        90
       8: if_icmpgt     25
      11: iload_2       
      12: bipush        65
      14: if_icmplt     25
      17: iload_2       
      18: bipush        32
      20: iadd          
      21: istore_3      
      22: goto          47
      25: iload_2       
      26: bipush        122
      28: if_icmpgt     45
      31: iload_2       
      32: bipush        97
      34: if_icmplt     45
      37: iload_2       
      38: bipush        32
      40: isub          
      41: istore_3      
      42: goto          47
      45: iload_2       
      46: istore_3      
      47: iload_3       
      48: iload_2       
```

Same instruction count, but there are some interesting things to note:
    * The `if-elsif-else` version skips straight to 47, whilst the ternary version has to go to 25 and execute that set of conditions even if the value is already case-switched.
    * The ternary version doesn't bother to store the value to the output register each branch, but does so once after executing either branch. This can allow ternary statements to take up less instructions than the `if-elsif-else` equivalent.

Finally, my preferred nested ternary solution:

```java
out = ch <= 'Z' && ch >= 'A' ? ch + 32 :
      ch <= 'z' && ch >= 'a' ? ch - 32 : ch;
```
```assembly
       5: iload_2       
       6: bipush        90
       8: if_icmpgt     24
      11: iload_2       
      12: bipush        65
      14: if_icmplt     24
      17: iload_2       
      18: bipush        32
      20: iadd          
      21: goto          44
      24: iload_2       
      25: bipush        122
      27: if_icmpgt     43
      30: iload_2       
      31: bipush        97
      33: if_icmplt     43
      36: iload_2       
      37: bipush        32
      39: isub          
      40: goto          44
      43: iload_2       
      44: istore_3      
      45: iload_3       
      46: iload_2       
```

As predicted, we cut down on 2 instructions due to the compiler knowing in advance that the ternary will always assign to the output register - in terms of execution, this is identical to the `if-elsif-else` version, but there are measurable advantages to reducing the instruction count (related to pipelining, and the number of instructions the CPU can fit into the instruction cache before a flush is needed), so ternary's may be more performant.