From the course: Learning Groovy

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Conditional structures and loops

Conditional structures and loops - Groovy Tutorial

From the course: Learning Groovy

Start my 1-month free trial

Conditional structures and loops

- [Instructor] Now, let's explore how to define conditional logic and iteration code in Groovy. I don't know about you, but most people don't like to label themselves as middle-aged. I certainly don't, but sadly, the "Oxford English Dictionary" proves me wrong by defining the age range between 45 and 65. A good way to express this logic is by writing an if else block. Let's put this code into practice. Here we're defining a if condition, where the lower bound for the age should be 45. Then we are adding a Boolean and, and then we'll say the upper bound is 65. If that's the case, then simply print out the full name of the person, and say the person is middle-aged. Now, we also want to handle the case where that person doesn't fit the range. In those cases, we'll simply print out the age. Great, rarely will our program only ever handle a single person. Next, we'll define a list of persons and iterate over its…

Contents