Control Flow
if-then
while
You can implement an infinite loop using the while statement as follows:
The Java programming language also provides a do-while statement, which can be expressed as follows:
for
When using this version of the for statement, keep in mind that:
- The initialization expression initializes the loop; it's executed once, as the loop begins.
- When the termination expression evaluates to
false, the loop terminates. - The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.
Branching
break
The break statement has two forms: labeled and unlabeled.
- An unlabeled
breakstatement terminates the innermostswitch,for,while, ordo-whilestatement, - but a labeled
breakterminates an outer statement.
continue
The continue statement skips the current iteration of a for, while , or do-while loop.
- The unlabeled form skips to the end of the innermost loop's body and evaluates the
booleanexpression that controls the loop. - A labeled
continuestatement skips the current iteration of an outer loop marked with the given label.
return
The return statement exits from the current method, and control flow returns to where the method was invoked.
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html
switch
A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types, the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer.
The body of a switch statement is known as a switch block. A statement in the switch block can be labeled with one or more case or default labels.
danger
Each break statement terminates the enclosing switch statement. Control flow continues with the first statement following the switch block. The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.