Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide Second Year Edition Name: Tutorial Group: Groovy can be obtained freely by going to http://groovy-lang.org/download Page 1 of 8
Variables Variables are characters or words which hold a data value. For example, Int X = 12; defines an integer value of 12 which is assigned to the value of x. You must declare the type of variable before initialization which can be done before putting the data type before the variable. There are different data types that you could use; Data type Usage Int For numbers where there are no decimal points e.g. 12, 13 or 14 Double Also known as real numbers which can include negatives. For numbers where there is a decimal point e.g. 12.5, 13.5 Float For numbers where there is a decimal point e.g. 12.5, 13.5 Char For the use of single characters String For multiple character use as long as the data supplied is in quotation marks. Boolean Returns as a true or false depending on data supplied Using Equals When using variables, it is important to understand the use of the equals sign as it has two main functions. A singular equals sign is used to assign a variable to data whereas a pair of equals signs are used to say if a variable is equal to another. e.g. x= 4 Used for assigning variables When x is used with the print command, only the number 4 will be printed. x==4 Is X equal to 4? Printing To print data out in Groovy, we use println() or the print() statements. The difference is println() prints the contents and then starts a new line where as print() prints out the content on the same line as the previous statement. e.g. int x=4; int y=5; Type print() statement println() statement Code Print(4); Print(5); Println(4); Println(5); Output 4 5 4 5 As much as it is subtle, it can make a big difference in your desired output of a system. Page 2 of 8
Expressions and Operators (Part 1) Operator Meaning Example Result + Addition 3+5 8 - Subtraction 10-5 5 * Multiplication 5*5 25 / Division 25/3 8 % Modulo (Remainder) 14%4 2 - or + Unary Unary means it just works with one operand -y Y=5-5 You can also increment a variable by using two plus signs; e.g. int x=5; x++ You can also do the same for decrementing a variable by using two minus signs; e.g. int x=5; x-- Flow of Control A program that does the same thing as previously is a waste of time. Programs have to do different things based on the data they have to work with. What you cannot see when you program is that one instruction is a plus and it runs a machine instruction with decisions being made behind the scenes. These are commonly known as if statements and we are checking if something is true about the data we have on hand. The conditional expression is a test of data if for example, we are checking that x>5. We are not looking at input statement as we have not looked at the input statements. This is because the incantation is more complicated then what we want to look at. Page 3 of 8
If Statements An if statement is a conditional statement which only prints if it is true or false. We use assigned variables to do comparisons. e.g. Int x = 10; If( x <11){ Println( X is smaller than 11 ); This code makes an integer variable called x and assigns the value of 10. Then we use an if statement to say that if x is smaller than 11 then we print out X is smaller than 11. It is important that when using if statements that you use the curly brackets for declaring the block contents. The contents of this if statement is println( X is smaller than 11 ); Expressions and Operators (Part 2) Equality and Relational operators; Operator Meaning < Less than > More than <= Less than or equal to >= More than or equal to!= Not equal to Conditional operators; Operator Meaning Or && and e.g. Int x = 24; Int y = 20; If ((x >= 20) && (y!= 19)){ print( Statement ); This code has 2 conditions with an and statement. It says if X is greater than or equal to 20 and Y is not equal to 19 Then print out statement. If the if statement had instead of && then it would say if X is greater than or equal to 20 or Y is not equal to 19 Then print out statement. Page 4 of 8
While Loops With while statements they loop a block of code until a condition is no longer true. For this type of loop we have a condition and then we have to increment a variable so that the condition will eventually become false. e.g. Int x = 0; While (x < 10){ Println( X is still smaller than 10 ); X++; If x<10 evaluates to true, then the statement is executed. Then, the x<10 is evaluated again. If it evaluates to true, statement is executed again. This repetition continues until the x<10 evaluates to false. We declare a variable called X which has the starting value of 0. Then we say while x is smaller than 10 we want to print of X is still smaller than 10. Then we use the increment operator to add 1 to x after every loop. So this loop will run until x is greater than 10 in which the condition (X<10) is no longer true. Every time the loop runs it adds 1 to x. (x++ increments x by 1). For Loops: For loops are very similar to while loops however they use 3 conditions and are all within the statement line. e.g. For (int i = 0; i < 10; i++){ Println( X is still smaller than 10 ); This code is doing the exact same thing as the while statement except it uses 2 lines of code rather than 4. To begin with, it starts by making a variable i in the first condition (int i = 0). The second condition is whilst i is smaller than 10. And then the final condition is saying to increment i by 1 every time. So to clarify, you make a variable (named i) with a value of 0. Then whilst i < 10 the condition is true. And what we do after each time of running the code is we increment i by 1 (i++). So whilst i<10 we print out x is still smaller than 10. This will run 10 times. If you still don t understand it then here is a groovy screen shot showing what it is doing every time. Page 5 of 8
(Image 1: For loop in Groovy) Arrays Arrays are essentially lists which store set amounts of data. They look like this: int [] array = [1,2,3,4,5]; Arrays are declared like this; int [] array = new int[6]; type Creates array Number of elements in array Page 6 of 8
Array Traversal Here is an array which has been declared in a program called int_array1.groovy. It creates an array of ten integers with each value (including zero but excluding ten) assigned to an integer. The for loop at the bottom of the program allows us to traverse the array and print each value with text. e.g. int [] marks = new int[10]; marks [0] = 23; marks [1] = 5; marks [2] = 17; marks [3] = 0; marks [4] = 2; marks [5] = 9; marks [6] = 10; marks [7] = -4; marks [8] = 3; marks [9] = -8; for (int i = 0; i < marks.length; i++){ println("marks "+ i + " value is: "+ marks[i]); Output marks 0 value is: 23 marks 1 value is: 5 marks 2 value is: 17 marks 3 value is: 0 marks 4 value is: 2 marks 5 value is: 9 marks 6 value is: 10 marks 7 value is: -4 marks 8 value is: 3 marks 9 value is: -8 We start by saying what data type is going into our array (in this case the data type int is going into our array). Then, we declare that it is an array using the square brackets [] and finally we name the array marks. Then, assign the number of values we want to hold (in this case 10) so it will be =new int[10]. Each number uses up a data slot so therefore his array has 10 data slots containing integers which will print values from index 0 to index 9 (including 0, excluding 10) To call a data value you call the name of the array, followed by the data slot number. You should remember that a computers index starts from zero. Let s see how this works for the above shown on the previous page; e.g. int [] marks = new int[10]; marks [0] = 23; marks [1] = 5; marks [2] = 17; Page 7 of 8
marks [3] = 0; marks [4] = 2; marks [5] = 9; marks [6] = 10; marks [7] = -4; marks [8] = 3; marks [9] = -8; School of Computer Science println(marks[5]) Output 9 If we wanted to, could fill an array with integers and request it to print the index of the array. This could be done like so; e.g. int [] marks = new int[10]; for (int i = 0; i < marks.length; i++){ marks[i] = i; println(marks); Output [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Be careful when traversing arrays however as languages like Java checks if the program will cause an out of bounds error when you attempt to access an array element that does not exist. Remember that you can declare, create, and initialize an array with a list of literals. e.g. int[] counts = {12, 22, 17, 16, 4, 11; You can use an initializer list only when the array is first declared. Each value must match the type of the array. The values go into the array in the order given and determine the length of the array. References: Max Chapman Notes on Groovy Dr. Margaret Reid-Miller, Carnegie Mellon University - https://www.cs.cmu.edu/~mrmiller/15-110/ Page 8 of 8