mirror of https://github.com/deavmi/JavaOCP
306 lines
6.9 KiB
Java
306 lines
6.9 KiB
Java
package ocp.Basics.flow;
|
|
|
|
/**
|
|
* I am aware of most control-flow in Java
|
|
* so this will really just be practicing
|
|
* the things I don't know
|
|
*
|
|
*/
|
|
public class App
|
|
{
|
|
public static void main( String[] args )
|
|
{
|
|
/**
|
|
* Practicing the standard traditional
|
|
* switch statements
|
|
*/
|
|
// basicSwitchStatements();
|
|
|
|
/**
|
|
* Usage of the default case
|
|
*/
|
|
// defaultCaseSwitchStatement();
|
|
|
|
/**
|
|
* Switch case expressions with
|
|
* a singular expression
|
|
*/
|
|
// switchCaseExpressionsSingular();
|
|
|
|
/**
|
|
* Switch case expressions with
|
|
* multiple statements and an
|
|
* expression
|
|
*/
|
|
// switchCaseExpressionMultiple1();
|
|
|
|
/**
|
|
* Switch case expressions with
|
|
* multiple statements and an
|
|
* expression but with COMPILE-TIME
|
|
* checks that a yield is present
|
|
* PER EACH CASE!
|
|
*/
|
|
switchCaseExpressionMultiple2();
|
|
|
|
/**
|
|
* Switch statements
|
|
*/
|
|
// switchTests();
|
|
}
|
|
|
|
/**
|
|
* Practicing your BASIC switch statements
|
|
* that we are all used to
|
|
*/
|
|
private static void basicSwitchStatements()
|
|
{
|
|
int number = (int)(Math.random()*2);
|
|
|
|
/**
|
|
* Note that the first match is jumped to,
|
|
* then it runs all the code below it,
|
|
* THEREFORE place a BREAK after the match
|
|
* to avoid running the cases BELOW
|
|
* the matched one
|
|
*/
|
|
switch(number)
|
|
{
|
|
case 0:
|
|
System.out.println("We got a zero!");
|
|
break; // VERY important!
|
|
case 1:
|
|
System.out.println("We got a 1!");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Practicing the use of <code>default</code>
|
|
* in a switch statement
|
|
*
|
|
* This let's you have a case which is matched
|
|
* if none of the other cases matched.
|
|
*/
|
|
private static void defaultCaseSwitchStatement()
|
|
{
|
|
int number = (int)(Math.random()*4);
|
|
|
|
switch(number)
|
|
{
|
|
case 0:
|
|
System.out.println("We got ZERO!");
|
|
break;
|
|
case 1:
|
|
System.out.println("We got 1!");
|
|
break;
|
|
default:
|
|
System.out.println("We got SOMETHING ELSE '"+number+"'");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Switch case expressions (singular/implicit-yield)
|
|
*/
|
|
private static void switchCaseExpressionsSingular()
|
|
{
|
|
int number = (int)(Math.random()*4);
|
|
|
|
/**
|
|
* A singular expression has
|
|
* an implicit `yield` added
|
|
* so NO fallthrough can occur
|
|
*/
|
|
String result = switch(number)
|
|
{
|
|
case 0 -> "ZERO";
|
|
case 1 -> "ONE";
|
|
default -> "SOMETHING ELSE '"+number+"'";
|
|
};
|
|
System.out.println(result);
|
|
}
|
|
|
|
/**
|
|
* This shows the old-style switch expression
|
|
*
|
|
* NOTE! Because these have multiple statements
|
|
* that a yield is now required, and just like
|
|
* a break, if missing then fallthrough can occur
|
|
*/
|
|
private static void switchCaseExpressionMultiple1()
|
|
{
|
|
int number = (int)(Math.random()*4);
|
|
|
|
String result = switch(number)
|
|
{
|
|
case 0:
|
|
System.out.println("ZERO");
|
|
yield "ZERO"; // If not present you fall through till hitting a yield
|
|
case 1:
|
|
System.out.println("ONE");
|
|
yield "ONE";
|
|
default:
|
|
System.out.println("UNKNOWN");
|
|
yield "Unknown '"+number+"'";
|
|
};
|
|
|
|
System.out.println("Result: "+result);
|
|
}
|
|
|
|
/**
|
|
* New-style switch expressions for multiple
|
|
* statements with a compile-time check thaty
|
|
* each case contains a yield expression
|
|
* to prevent accidental fallthrough
|
|
*/
|
|
private static void switchCaseExpressionMultiple2()
|
|
{
|
|
int number = (int)(Math.random()*4);
|
|
|
|
String result = switch(number)
|
|
{
|
|
case 0 -> {
|
|
System.out.println("ZERO");
|
|
yield "ZERO";
|
|
}
|
|
case 1 -> {
|
|
System.out.println("ONE");
|
|
yield "ONE"; // Remove it and see the compiler complain
|
|
}
|
|
default -> {
|
|
System.out.println("SOMETHING ELSE '"+number+"'");
|
|
yield "ELSE '"+number+"'";
|
|
}
|
|
};
|
|
|
|
System.out.println("Result: "+result);
|
|
}
|
|
|
|
/**
|
|
* Switch-statement related tests
|
|
*/
|
|
private static void switchTests()
|
|
{
|
|
/**
|
|
* Re-call you need to have the break statements else it falls
|
|
* right through
|
|
*/
|
|
int number = (int)(Math.random()*2);
|
|
switch(number)
|
|
{
|
|
case 0:
|
|
System.out.println("Zero is the hero!");
|
|
break;
|
|
case 1:
|
|
System.out.println("1 gets the bun 🍞️");
|
|
}
|
|
|
|
/**
|
|
* One can also have a `default` case which is,
|
|
* well, the match all case
|
|
*/
|
|
number = (int)(Math.random()*4);
|
|
switch(number)
|
|
{
|
|
case 0:
|
|
System.out.println("Zero is the hero!");
|
|
break;
|
|
case 1:
|
|
System.out.println("1 gets the bun 🍞️");
|
|
break;
|
|
/**
|
|
* Equivalent to <code>case 2: case 3:</code>
|
|
* which falls through from 2 to 3 to the end
|
|
* of the switch case
|
|
*/
|
|
default:
|
|
System.out.println("Default! "+number);
|
|
}
|
|
|
|
/**
|
|
* One can also switch on strings, in which case
|
|
* it is checking the <code>.equals</code> method
|
|
* on these items
|
|
*/
|
|
String text = (int)(Math.random()*2) == 0 ? "Hello" : "Bye";
|
|
switch(text)
|
|
{
|
|
case "Hello":
|
|
System.out.println("Hi there");
|
|
break;
|
|
case "Bye":
|
|
System.out.println("Bye then!");
|
|
}
|
|
|
|
/**
|
|
* Below we will investigate the use of the so-called
|
|
* "switch expression" which let's us use a switch case
|
|
* as part of an expression with the resulting case being
|
|
* the evaluation
|
|
*/
|
|
String result = switch(text)
|
|
{
|
|
case "Hello" -> "Result is Hi there!";
|
|
case "Bye" -> "Result is Bye then!";
|
|
default -> "Ek weet nie";
|
|
};
|
|
System.out.println("Result: '"+result+"'");
|
|
|
|
/**
|
|
* Of course we can return whatever, the switch is
|
|
* just a selection expression
|
|
*/
|
|
Object i = switch(text)
|
|
{
|
|
case "Hello" -> new Object();
|
|
case "Bye" -> new Integer(1);
|
|
default -> null;
|
|
};
|
|
System.out.println("i: "+i);
|
|
|
|
/**
|
|
* We also can do the switch expressions WHILST still doing
|
|
* some other code before returning by using the <code>yield</code>
|
|
* keyword
|
|
*/
|
|
result = switch(text)
|
|
{
|
|
case "Hello":
|
|
System.out.println("Hiiiiii");
|
|
yield "We got Hiiiii";
|
|
case "Bye":
|
|
System.out.println("Biiiii");
|
|
yield "We got Biiiii";
|
|
default:
|
|
yield "Niks";
|
|
};
|
|
System.out.println("Result: '"+result+"'");
|
|
|
|
|
|
/**
|
|
* Now the only problem with the above is forgetting a
|
|
* <code>yield</code> means it can fall through, but
|
|
* if we write it as follows then the compiler will always
|
|
* warn us
|
|
*/
|
|
result = switch(text)
|
|
{
|
|
case "Hello" ->
|
|
{
|
|
System.out.println("Hiiiiii");
|
|
yield "We got Hiiiii";
|
|
}
|
|
case "Bye" ->
|
|
{
|
|
System.out.println("Biiiii");
|
|
yield "We got Biiiii";
|
|
}
|
|
default ->
|
|
{
|
|
yield "Niks";
|
|
}
|
|
};
|
|
System.out.println("Result: '"+result+"'");
|
|
|
|
}
|
|
}
|