(This is my second time typing this entire thread)
The Switch Statement
Part of this knowledge comes from: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
The switch statement is a cleaner, faster way to show multiple if else statements. In a switch block, a Character, Byte, Short, Integer, String or Enum can be used. Here is an example using String
switch(myString) { case "A": break; case "B": break;}
Enums use the Enum constants when switching. For example:
switch(myEnum){ case MEnum.Foo; break; case MEnum.ooF; break;}
Note that the only time the cases are enclosed within quotation marks( " ) is when we are using a String. Also notice the fact that we use a break after every case. This is to prevent falling through to the next case. For example
switch(myString) { case "A": case "B": break;}
In the case of A, not only will everything under the case of A be executed, but so will everything following that unless it is broken with a break
Extra Information from thiefmn6092:
The Ternary Operator
You can consult: http://alvinalexander.com/java/edu/pj/pj010018 if you find this unclear. However, if you do please let me know.
The ternary is another way of replacing an if else statement that might look like
if(myInt == 2) { someInt = 5;}
In a ternary, this would look like
someInt = myInt == 2 ? 5 : someInt;
Here's a breakdown of what just happened. The structure of a basic ternary is
field = (Condition) ? (Action) : (Default Action/What happens if the condition is failed)
The condition is any condition that you would normally meet in an if statement. If you have an if statement like this
if(myInt == 2 || myInt == 4 || myInt == 6) { someInt = 5;}
Then the ternary would look like
someInt = myInt == 2 || myInt == 4 || myInt == 6 ? 5 : someInt;
You can also stack multiple ternaries into one statement so where the default action would be we can say
someInt = myInt == 2 || myInt == 4 || myInt == 6 ? 5 : myInt == 8 ? 10 : -1;
That is the same as saying
if(myInt == 2 || myInt == 4 || myInt == 6) { someInt = 5;} else if(myInt == 8) { someInt = 10;} else { someInt = -1;}
The ternary just puts it into one neat little line. Of course you can apply your java syntax knowledge and use brackets.
Let me know if I missed anything, this is my second attempt so I'm kind of worn.