Skip to main content
Repeated action syntax (loops or recursion)

While loops

While loops in Groovy are executed in the manner they are in Java. The condition in the while loop is a boolean value that if true, makes the functions inside the while loop to execute. If the boolean is changed to false then the while loop is then terminated. 

For Loops
Similar to the syntax Java uses for for loops, groovy starts the for loop statement with a variable declaration, an expression and then incrementation. 

For In Loops
This is not a normal for loop. Instead, a for in loop takes a variable and then checks to see if the variable is in a certain range. For example if there was an array of numbers such as int [] arrary =[0,1,2,3,4,5,6,7,8,9], a for in loop could take a value, and check to iterate all of the given values that are inside the array, the loop would be set up in the way shown below.

for ( int i in array)
{
       println "${i}"
}

So then the output for these lines of code would be : 0,1,2,3,4,5,6,7,8,9. Along with the for in loop iterating through arrays, the for in loop can also iterate through a range if needed. An example of this is shown below.

for (int i in 1..9)
{
       println"${i}"
}
The output would then be an iteration of the range of the given values.







Comments

  1. In the For In range example that you provided:

    for (int i in 1..9)
    {
    println"${i}"
    }

    Is there somewhere in this loop that the array being looped through goes? It looks like the program should know to iterate through the range 1-9 but how does it know which array it is iterating through?

    ReplyDelete

Post a Comment

Popular posts from this blog

Description of interesting language facet # 2

Description of interesting language facet #2         In Groovy, objects are used for almost everything. Due to the fact that Groovy uses objects, Groovy automatically will wrap the types of variables in the code. Something that is worth mentioning is boxing and unboxing. When the coder is converting a primitive data type to an object it is called unboxing. Likewise, when the coder is converting an object to a primitive data type. For clarification purposes, there are eight primitive types which are  boolean , byte , char , short , int , long , float and double. In Java, widening is done before boxing is done. Widening is defined as taking a small primitive type value and is automatically accommodated in a bigger primitive data type.  The first method is the method that Java prioritizes because it does widening rather than boxing. However, for Groovy, the second method would be the method that would be called because no matter what c...

Implementation of binary search in the language

Implementation of binary search in the language                         Searing through data can be a very tedious task for the coder, so binary search was invented to search through data in an efficient manner. A key detail is that for the coder to use binary search to search through a set of data, that given data must already have been sorted in order for it to work.                         Binary search allows the coder to search through a sorted array or list of data in an efficient manner. In groovy, binary search is implemented by allowing the coder to divide the data in half and then check for the desired value. Then if the value is not in that given data set, the other half of the data set is checked. The data set is then divided into a new interval for the code This cycle is then repeated until the value is determined or the interva...

Methods, procedures or encapsulation

Methods, procedures or encapsulation When creating a method, the word def is used in front of the method or the method gives a return type to clarify that is a method. There are three modifiers that can be used for a method: public, private, or protected.  The default modifier in Groovy is public, so if no modifier is given, then it will automatically be public. Parameters for methods in Groovy are handled the same way as they are in Java, the parameter names mus differ from each other. In Groovy, if no parameters are given inside the method, Groovy has default parameters that are passed in.  An example is def method(parameterA = 0, parameterB =0, parameterC=0) { } A main method in Groovy is as seen below. static void main(String[] args) { }  Groovy also has instance methods. These methods can be accessed by instantiating an object to call upon the object. There are optional modifiers that can be used on methods. These include static, final, or s...