Java Decision making statements

1) if Statement

2) if…else Statement

3) if…else…if…else Statement

4) Nested if statement

5) switch statement

 

if Statement:

  • if statement consists of a Boolean expression followed by one or more statements.
  • If the Boolean expression evaluates to true, then the block of code inside the if statement will be executed. If not, control jumps to next line outside if loop.
  • Using if decision making Statement we verify condition is True or False to Execute code block.

Example:

Output:

 

if… else Statement: 

  • if statement can be followed by an optional else statement, which executes when the condition mentioned is false.
  • Using if else decision making statement definitely execute any code block depends on condition.

Example: 

Output:

 

if… else…if… else Statement:

  • if statement can be followed by an optional else if…else statement, which is very useful to test various conditions using single if…else if statement.
  • Use to verify large conditions with multiple else block’s.

Example: 

Output:

 

Nested if statement:

  • When there is an if statement inside another if statement then it is called the nested if decision making statement.
  • Use to verify multi condition’s at once.

Example:

Output:

 

switch statement: 

  • A switch decision making statement allows a variable to be tested against a list of values.
  • Each value is called a case and the variable is being checked for each case.The body of a switch statement is referred to as a switch block.
  • A statement in the switch block can be categorized with one or greater case or default labels.
  • The switch statement evaluates its expression, then executes all statements that follow the matching case label.

Example:

Output: