Decision Making in Groovy

In groovy, decision-making is used to check the condition and execute the statements. if the condition is true then the true block statement is executed and if the condition is false then the false block is executed.

If statement

In groovy, if statement is used when we have only one condition. In if statement there is only true block. If the condition is true then the true block is executed. There is no false or else block in this statement.

Syntax:

Flow chart:

Decision Making in Groovy

Example 1:

Output:

Decision Making in Groovy

Example 2:

Output:

Decision Making in Groovy

If else statement

In groovy, if else statement is used when only one condition. In if else statement we have a true as well as false or we can say else block. If the condition is true then the true block is executed otherwise else block is executed.

Syntax:

Flow chart:

Decision Making in Groovy

Example 3:

Output:

Decision Making in Groovy

Example 4:

Output:

Decision Making in Groovy

Nested If Statement

In groovy, Nested if statement is used when we have more than one condition.

Syntax:

Flow chart:

Decision Making in Groovy

Example 5:

Output:

Decision Making in Groovy

Switch Statement

In groovy, a switch is a multiway branch statement. It provides an easy way to execute different parts of the code which are based on the value of the expression.

Syntax:

Flow chart:

Decision Making in Groovy

Example 6:

Output:

Decision Making in Groovy