Groovy web console

subscribe to the feed Subscribe
to this
site

03 Operators and Decisions

Published 2 months ago by ST with tags groovy
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
// for a full overview see
// http://groovy-lang.org/operators.html

// Arithmetic operators

def a = 5
def b = 10
println """a: $a
b:$b"""

def sum = a + b
println "Sum: $sum"

println "Difference: ${a - b}"

println "Product: ${a * b}"

println "Fraction: ${a / b}"

// Relational operators

def same = a == b
def less = a < b
def greater = a > b

// Important note for those familiar with Java:
// The operator == uses the equals method on objects

// Decisions

if (less) {
  println 'a is less than b'
}
else {
  println 'a is greater than or equals b'
}

// Logical operators

def notSame = !same // NOT

if (same || less) { // logical OR
  println 'a is less than or equals b'
}

if (a > 0 && a < 10) { //logical AND
  println 'a is between zero and ten'
}

// Groovy truth: non-boolean values can be interpreted as booleans

def false1 = 0
def false2 = ''
def false3 = null

assert !false1
assert !false2
assert !false3

def true1 = 12
def true2 = 'Hello'
def true3 = new Date()

assert true1
assert true2
assert true3