Workflow

if ... else ...

/* 
if(<condition>) <statement1>

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

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

if(x==y) x++;
if(x==y) x++; else x--;
if(x==y) x++; else if(x!=y) x--;

if(x==y){
  x++;
}else {
  x--;
}

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 (<expression>) {
  case <value1>:
    <sequence_of_statements1>
    break;
  ...
  case valueN:
       <sequence_of_statementsN>
    break;

  default:
  <sequence_of_statementsN>
}*/
String todayIs="";

switch (new Date().getDay()) 
   {
    case 0:
        todayIs = "non working day";
        break;
    
    case 1: case 2:
    case 3: case 4: case 5:
        todayIs = "working day";    
        break;

    case 6:
        todayIs = "non working day";   
        x = 23;
        break;

    default:
        todayIs="such day not exist";    
        break;
}

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 (<init_statement> ; <cond_statement> ; <it_statement>) {
  // code block to be executed
}
*/

int ind;
for(ind=5; ind<10; ind++) console.log("ind="+ind);

for(int i=5; i<10; i++){
     console.log("ind="+i);
     ...
}

// infinite loop, use break in the executed code block for exit from loop
for(;;){
...
}

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(<type-name> <var-name> : <iterable>){
// code block to be executed
}
*/

char [] digits = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};

StringBuilder sb = new StringBuilder();

for (char ch : digits) {
  sb.append(ch).append(' ');
}

//---------------------------
int [] digits = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
StringBuilder sb = new StringBuilder();
Arrays.stream(digits).forEach( i -> sb.append(i+1) );

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

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

int ind=0;
while(ind<10) ind++;

while(ind<20){
...
ind++;
}

// infinite loop, use break in the executed code block for exit from loop
while(true) {

}

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.