# Java Review: Control Flow

- Java uses the dominant imperative control flow paradigm. Other paradigms are declarative programming and data flow.
- Structured Programming control flow primitives within a method:
  - Sequence - execute one statment after another.
  - Choice - execute only one several statements depending on a condition (if and switch statements).
  - Repetition - execute some statements repetitively (while, for, do...while).
- Actual programming languages typically add more.
  - Exceptions (`throw`, `try...catch`).
  - Ways to exit or continue early (`break`, `return`, `continue`).
  - The dreaded `goto` statement is not popular.

# if Statement with a single true clause

       if (expression) {
           statements // do these if expression is true
       }

- The expression must be boolean.
- The braces are not necessary if there is only one statement, but it's a common style to always use them.
- You must (for readability) indent the inner part of an if statement.

# if Statement with true and false clauses

       if (expression) {
           statements // do these if expression is true
       } else {
           statements // do these if expression is false
       }

# if Statement with parallel tests

       if (expression1) {
           statements // do these if expression1 is true
       } else if (expression2) {
           statements // do these if expression2 is true
       } else if (expression3) {
           statements // do these if expression3 is true
       . . .
       } else {
           statements // do these no expression was true
       }

- Indenting the nested if statements would be technically correct, but the convention in parallel tests is to write it in this style. Many programming languages have an `elseif` statement for this purpose.
- If the tests are all on a single integer value, a `switch` statement is better.

# switch Statement

       switch (expr) {
          case c1:
                statements // do these if expr == c1
                break;
          case c2:
                statements // do these if expr == c2
                break;
          case c2:
          case c3:
          case c4:  //  Cases can simply fall thru.
                statements // do these if expr ==  any of c's
                break;
          . . .
          default:
                statements // do these if expr != any above
       }

- The switch expression must be an integer value, altho C# also allows strings.
- Requiring the `break` is compatible with C and C++, but is considered a mistake by many. C# made the fall-thru case explicit with `continue`.

# End