Java : Decision Making Statements

All the program statements in java executed sequentially in the order in which they appear. This order of execution can change when there is used jumping statements or the repetition of certain statements or any decision making statements.

These are some decision making statements:

Below is the syntax for each decision making statements. For more in-depth knowledge follow the link corresponding to it.

See Also :

if statement

If the statement is used to control the program flow based on some conditional statements. If the execution result of the statement is true then only the block associated with if-statement will execute otherwise skipped. This if-statement is the simplest way to modify the control execution flow of the program.
Syntax:

if(test_expression)
{
    statement 1;
    statement 2;
    ...
}

See Also: if statement in detail with example

if-else statement

If this conditional expression execution result is true then only the block associated with if statement will execute otherwise control will reach to else block.

Syntax:

if(test_expression)
{
   //execute your code
}
else
{
   //execute your code
}

See Also: if-else statement in detail with example

else-if Statement

If the if-statement conditional statement execution result is false then control will reach to check conditions for an else-if statement. If the result is true, block associated with if-else statement will execute otherwise control will reach to else block.

Syntax:

if(test_expression)
{
   //execute your code
}
else if(test_expression n)
{
   //execute your code
}
else
{
   //execute your code
}

See Also: else-if statement in detail with example

Switch Statement

The switch statement is used when there are multiple possibilities of if blocks. When there is a chance to implement multiple if-else-if statements better go with a switch statement.

Syntax:

switch(variable)
{
case 1:
   //execute your code
break;

case n:
   //execute your code
break;

default:
   //execute your code
break;
}

See Also: switch statement in detail with example

Note:

  • After the end of each block for the case it is necessary to insert a break statement because if you do not use the break statement, all consecutive blocks of codes will get executed after matching the case block.
  • The switch statement is fast when compared with the case of multiple else if because it directly jumps to match condition.