Decision Making in Java

0
129
Decision-Making-In-Java

 

Decision Making statements will have one or more conditions to validate and check whether conditions are satisified. In case the conditions satisfies Boolean value as “True”, a set of statements are executed within the block and a set of remaining statements will be executed if the condition satisifies as “false”

The Decision Making statements in Java are as below :

  1. If Statement
  2. Switch Statement

If Statement in Java : The if statement is a control flow based statement.  Where in if statement will test the condition and executes the if block once the value returns as true.

Syntax :

Example on IF statement:

OUTPUT :

Java If statement output

In the above example we have seen how the if statement condition checks if the value is equal to 4 and since it is true, the if block is executed.

if else statement in Java :if else statements are those which validates the test condition. If the condition is true then if block is executed otherwise else block is executed.

Syntax of if else:

Example on if else Statement :

OUTPUT :

Java If Else statement

In the above example we have seen how the if statement condition checks if the value is equal to 4 and since it is false, the else block is executed.

If -else-if statement in Java :

syntax of If -else-if statement in Java :

In the above example we have seen how the if statement condition checks if the value is equal to 4, else another if condition checks whether value is equal to 6. since both the conditions fails, the else block is executed.

Switch Statement in Java : Switch statement executes single statement from multiple conditions based on our choice.

Syntax of Switch statement  :

 

Example :

Output :

Java Switch Statement

LEAVE A REPLY

Please enter your comment!
Please enter your name here