Workflow

if ... else ...

if-else example
/* 
if(<condition>) <statement1>

if(<condition>) <statement1> else <statement2>

if(<condition>) {
  <statement1>
}else {
  <statement2>
}*/

The if statement provides code execution by condition. If condition true, then statement1 will be executed. Otherwise statement2 will be executed from else block. The else block can be omitted.

statement1 and statement2 can be represented as group of statements in curly brackets.

switch

switch example
/*
switch (<expression>) {
  case <value1>:
    <sequence_of_statements1>
    break;
  ...
  case valueN:
       <sequence_of_statementsN>
    break;

  default:
  <sequence_of_statementsN>
}*/

The switch statement is used to perform different actions based on different conditions.

The switch expression is evaluated one time. So it is more efficiently than repeated if statements.

The switch expression type can be byte/Byte, short/Short, char/Character, int/Integer, String, any enumerated type.

The break keyword ends the switch block. If it is omitted, next case will be executed.

If no cases match, the default code block is executed. If default is omitted, then nothing executed. Usually the default block is placed at the end of switch.

for

for example
/*
for (<init_statement> ; <cond_statement> ; <it_statement>) {
  // code block to be executed
}
*/

init_statement is executed one time before the execution of the code block.

cond_statement defines the condition for executing the code block.

it_statement is executed every time after the code block has been executed.

init_statement, cond_statement, it_statement could be omitted.

for each

for-each example
/* 
for(<type-name> <var-name> : <iterable>){
// code block to be executed
}
*/

The for each loop allows iterate over arrays and any iterable object such maps, sets and etc.

The stream package has methods for handling collections, such as forEach.

while

Code example
/* 
while(<condition>){
// code block to be executed
}
*/

The while loop executes code block while condition is true.

do ... while

/* 
do {
// code block to be executed
}while(<condition>)
*/

The do ... while loop executes code block while condition is true. Unlike while loop code is executed at least once whether condition is true or false.