A real decision is measured by the fact that you've taken a new action. If there's no action, you haven't truly decided. [Tony Robbins]
Boolean expressions are statements that can one of two values: true or false.
Example:
100 > 50 -> evaluates to true
if statements are used to allow your code to follow different paths depending on a condition.
The concept: if the statement is true, do the following bit of code.
The basic pattern:
if ( boolean expression ) { code to be executed }
If the expression we check is not true, a different set of instructions can be used.
A concrete example:
We have a few more forms:
This shows an else if. It requires a second condition to be checked before moving on. In fact, you can chain else if's together to check a series of different condition making it work just like a switch (with more typing).
Now just to give you more control, you can put an if any place where you can put statements.
switch is a shortcut to using a long series of if-else statements
You can only switch on ints, bytes, shorts, chars. You cannot use strings.
To use switch you define a number of cases, and a default case. Once the case has been completed you break out of the block.