Sunday 13 January 2019

Groovy: Ternary operator (?:)


It is a short cut to if-else statement.

Syntax
value = (expression) ? statement1 : statement2

If the expression is evaluated to true, then statement1 is assigned to the value, else statement2 is assigned to the value.

HelloWorld.groovy
a = 10

message = (a == 10) ? "a is equal to 10" : "a is not equal to 10"

println message

Output
a is equal to 10

Previous                                                 Next                                                 Home

No comments:

Post a Comment