at-m42:lecture5
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
at-m42:lecture5 [2009/04/01 14:55] – eechris | at-m42:lecture5 [2011/01/14 12:45] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~SLIDESHOW~~ | ||
+ | ====== Methods and Flow of Control ====== | ||
+ | |||
+ | * [[# | ||
+ | * [[#Flow of Control]] | ||
+ | |||
+ | The slides and notes in this presentation are adapted from //Groovy Programming// | ||
+ | |||
+ | An index to the source code for all the examples in this lecture is [[/ | ||
+ | |||
+ | |||
+ | ===== Methods ===== | ||
+ | |||
+ | |||
+ | * [[#Method parameters]] | ||
+ | * [[#Default parameters]] | ||
+ | * [[#Method return values]] | ||
+ | * [[# | ||
+ | * [[#Variable scope]] | ||
+ | * [[# | ||
+ | |||
+ | ===== What is a method? ===== | ||
+ | |||
+ | A program structuring and task partitioning feature: | ||
+ | * A //method// us a segment of code that can be executed or //called// one or more times in a program. | ||
+ | * Methods may be given // | ||
+ | * Each method may use different //actual parameters// | ||
+ | * Other names for method: // | ||
+ | |||
+ | ---- | ||
+ | |||
+ | Methods in Groovy and other programming languages partition large programs into smaller manageable units, thus simplifying the programming task. Each method is responsible for a particular functionality required in the application. One method can call or execute any other method. thus a task represented by one method can be partitioned into subtasks realized by other sub-methods. Further, methods developed in one program can be reused in other programs. | ||
+ | |||
+ | ===== Defining a method ===== | ||
+ | |||
+ | <code groovy> | ||
+ | def methodName() { | ||
+ | // Method code goes here | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | The simplest form of a method definition is one with no parameters as shown on this slide. Method names are presented as program identifiers. when the method is called, the parentheses '' | ||
+ | |||
+ | ===== A simple method | ||
+ | |||
+ | <code groovy 1 | Example 1: A simple method definition (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | Here the method is named '' | ||
+ | |||
+ | Hello AT-M42 class of 2009 | ||
+ | |||
+ | ===== Using variables in a method ===== | ||
+ | |||
+ | <code groovy 1 | Example 2: A method with variables. (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | This program reads in two numbers from the console and then reverses the values. To acheive this effect, the method needs to declare and use variables in the method body. Running the script might produces the following | ||
+ | < | ||
+ | <pre> | ||
+ | < | ||
+ | Enter two integer values: < | ||
+ | < | ||
+ | Reversed values: 13 and 12 | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== Converting clock time ===== | ||
+ | |||
+ | <code groovy 1 | Example 3: Converting clock time (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | This example is similar to the last. It reads some data, processes it, and displays the results. The processing involves some arithmetic operations. The program reads three integer values representing a 24-hour clock time expressed as hours, minutes and seconds. The time is converted to the number of seconds. | ||
+ | |||
+ | Running the program might produce: | ||
+ | < | ||
+ | <pre> | ||
+ | < | ||
+ | Enter the time to be converted [hours minutes seconds]: < | ||
+ | The original time of: 22 hours, 34 minutes and 56 seconds | ||
+ | Converts to: 81296 seconds | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== Method parameters ===== | ||
+ | |||
+ | <code groovy> | ||
+ | def methodName(para1, | ||
+ | // method code goes here | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | A method will be more generally useful if its behaviour is determined by the value of one or more parameters. We can transfer values to the called method using //method parameters// | ||
+ | |||
+ | The method parameters appear as a list of //formal parameter// names enclosed in parentheses following the method name. The parameter names must differ from each other. | ||
+ | |||
+ | ===== Method parameters (example) ===== | ||
+ | |||
+ | <code groovy 1 | Example 4: Method parameters (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | This new version of the '' | ||
+ | |||
+ | Hello Chris, welcome to AT-M42 class of 2009 | ||
+ | |||
+ | ===== Default Parameters ===== | ||
+ | |||
+ | <code groovy> | ||
+ | def methodName(para1, | ||
+ | // method code goes here | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | The formal parameters in a method definition can specify //default values//. Where default values are given, these values are used if the caller does not pass them explicitly. Default parameter values are shown as assignments. Where default parameters are introduced in a method definition, they must be listed after the non-default parameters. That is default parameters, if present, must be given last in the formal parameter list. In the slide, the second and third parameters have been given default values. | ||
+ | |||
+ | The '' | ||
+ | |||
+ | ===== Default parameters (Example) ===== | ||
+ | |||
+ | <code groovy 1 | Example 5: Default parameters (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | An illustration of default parameters is shown in this slide. When we execute this script, we see the second call to method greetings assumes that the name has the default value '' | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== Method return values ===== | ||
+ | |||
+ | <code groovy 1 | Example 6: Method return values (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | A method can also return a value to its caller. This is achieved with the //return statement// of the form: | ||
+ | <code groovy> | ||
+ | return expression | ||
+ | </ | ||
+ | |||
+ | The statement indicates that control is to return immediately from the method to the caller, and that the expression is to be made available to the caller. The value may be captured with an appropriate assignment. | ||
+ | |||
+ | The '' | ||
+ | |||
+ | The script behaves identically to [[# | ||
+ | |||
+ | ===== Implicit returns ===== | ||
+ | |||
+ | <code groovy 1 | Example 7: Implicit returns (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | The '' | ||
+ | |||
+ | ===== Parameter Passing ===== | ||
+ | |||
+ | * //Pass by value// | ||
+ | * Parameter value is copied from actual parameter to formal parameter | ||
+ | * All values in Groovy are //object references// | ||
+ | * That is, changes to the state of the formal parameter will be reflected in the actual parameter. | ||
+ | |||
+ | ---- | ||
+ | |||
+ | See [[lecture2# | ||
+ | |||
+ | ===== Parameter aliasing ===== | ||
+ | |||
+ | <code groovy 1 | Example 8: Parameter aliasing (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | An implication of //pass by value// in Groovy is that any assignment to a formal parameter within a method body establishes a new object for the formal parameters to reference. This is illustrated in this slide. | ||
+ | |||
+ | When this program is run, the result is: | ||
+ | Name (at entry): Chris | ||
+ | Name (after assignment): | ||
+ | Lecturer: Chris | ||
+ | |||
+ | The method '' | ||
+ | |||
+ | ===== Interchange method ===== | ||
+ | |||
+ | <code groovy 1 | Example 9: Interchange method (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | A consequence of the aliasing of formal and actual parameters is that the '' | ||
+ | |||
+ | This is confirmed by the actual run of the program: | ||
+ | < | ||
+ | <pre> | ||
+ | Enter the first value: < | ||
+ | Enter the second value: < | ||
+ | First: 12 | ||
+ | Second: 34 | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== Variable Scope ===== | ||
+ | |||
+ | * Variables defined inside the body of a method are called //local variables// | ||
+ | * Local variables are limited in //scope// to the body of the method. | ||
+ | * Variables can only be referenced when they are //in scope//. | ||
+ | * Variables have no existence when they are out of scope. | ||
+ | * Formal parameters also behave as local variables within the scope of the method for which they have been defined. | ||
+ | * Methods declared outside the body of a method are //not in scope// within the method. | ||
+ | |||
+ | ===== Illustration of variable scope ===== | ||
+ | |||
+ | <code groovy 1 | Example 1: Variable scope (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | This example includes the method '' | ||
+ | |||
+ | Exception thrown: No such property: lecturer for class: example10 | ||
+ | |||
+ | Similarly, the scope of the formal parameter '' | ||
+ | |||
+ | Exception thrown: No such property: name for class: example10 | ||
+ | |||
+ | ===== Variables and methods in same scope ===== | ||
+ | |||
+ | <code groovy 1 | Example 11: Variables and methods in same scope (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | The order of declaration is irrelevant to scoping rules. Even though '' | ||
+ | |||
+ | ===== Collections as method parameters and return values ===== | ||
+ | |||
+ | <code groovy 1 | Example 12: List parameter and return. (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | A Groovy method can accept a collection parameter, such as a '' | ||
+ | |||
+ | ===== Flow of Control ===== | ||
+ | |||
+ | Three types of //flow of control// structures: | ||
+ | * Sequence | ||
+ | * Selection | ||
+ | * Iteration | ||
+ | |||
+ | ---- | ||
+ | |||
+ | So far the programs and examples we have seen have been // | ||
+ | |||
+ | Additional statements are provided in Groovy that can alter the flow of control of a program. We explore these in this section((If you know at least one other programming language, these will be very familiar)). | ||
+ | |||
+ | ===== Classification of Program Statements ===== | ||
+ | |||
+ | * // | ||
+ | * [[#while statement]] | ||
+ | * [[#for statement]] | ||
+ | * // | ||
+ | * [[#if statement]] | ||
+ | * [[#switch statement]] | ||
+ | * //Selection within iterations// | ||
+ | * [[#break statement]] | ||
+ | * [[#continue statement]] | ||
+ | | ||
+ | |||
+ | ===== While Statement ===== | ||
+ | |||
+ | The fundamental iteration clause is the //while statement// | ||
+ | <code groovy> | ||
+ | while (condition) { | ||
+ | statement #1 | ||
+ | statement #2 | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | The '' | ||
+ | |||
+ | ===== While Statement (Example 1) ===== | ||
+ | |||
+ | |||
+ | <code groovy 1 | Example 13: while statement (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | Example 13 in the slide prints the values 1 to 10 inclusive. Each iteration through the loop prints the current value of the variable '' | ||
+ | |||
+ | The program' | ||
+ | Start | ||
+ | count: 1 | ||
+ | count: 2 | ||
+ | : | ||
+ | count: 9 | ||
+ | count: 10 | ||
+ | Done | ||
+ | |||
+ | Conventionally, | ||
+ | |||
+ | ===== A More Interesting Example ===== | ||
+ | |||
+ | <code groovy 1 | Example 14: Sum of a series of positive integers (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | A typical use for a '' | ||
+ | |||
+ | A sample session with this program is: | ||
+ | < | ||
+ | <pre> | ||
+ | Enter first value: < | ||
+ | Enter next value [-ve to quit]: < | ||
+ | Enter next value [-ve to quit]: < | ||
+ | Enter next value [-ve to quit]: < | ||
+ | Enter next value [-ve to quit]: < | ||
+ | The sum is: 10 | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Note that in this example, if the first input is negative, the body of the '' | ||
+ | |||
+ | ===== For Statement ===== | ||
+ | |||
+ | Can be used to iterate over a '' | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | for (variable in < | ||
+ | statement #1 | ||
+ | statement #2 | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | for (variable in < | ||
+ | statement #1 | ||
+ | statement #2 | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | for (variable in < | ||
+ | statement #1 | ||
+ | statement #2 | ||
+ | ... | ||
+ | }</ | ||
+ | |||
+ | ===== for statement with Range ===== | ||
+ | |||
+ | <code groovy 1 | Example 15: for statement (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | Example 15 repeats the first '' | ||
+ | |||
+ | ===== Looping through a List ===== | ||
+ | |||
+ | <code groovy 1 | Example 16: Looping through a List. (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | |||
+ | The output from this program is: | ||
+ | Start | ||
+ | count: 11 | ||
+ | count: 12 | ||
+ | count: 13 | ||
+ | count: 14 | ||
+ | Done | ||
+ | |||
+ | |||
+ | |||
+ | Notes ... | ||
+ | |||
+ | ===== Iterating through a Map ===== | ||
+ | |||
+ | <code groovy 1 | Example 17: Looping through a Map. (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | We can use a '' | ||
+ | |||
+ | The output produced is: | ||
+ | Total staff age: 150 | ||
+ | |||
+ | |||
+ | |||
+ | ===== Looping through a String ===== | ||
+ | |||
+ | <code groovy 1 | Example 18: Looping through a String (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | This example shows that we can also iterate through the characters of a '' | ||
+ | |||
+ | The output is: | ||
+ | listOfCharacters: | ||
+ | |||
+ | |||
+ | |||
+ | ===== if Statement ===== | ||
+ | |||
+ | |||
+ | The general form is: | ||
+ | <code groovy> | ||
+ | if (condition) { | ||
+ | statement #1a | ||
+ | statement #1b | ||
+ | ... | ||
+ | } else { | ||
+ | statement #2a | ||
+ | statement #2b | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Note: '' | ||
+ | |||
+ | An '' | ||
+ | <code groovy> | ||
+ | if (condition) { | ||
+ | statement #1 | ||
+ | statement #2 | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | If the condition evaluates to '' | ||
+ | |||
+ | ===== A simple if statement ===== | ||
+ | |||
+ | <code groovy 1 | Example 19: A simple if statement (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | In Example 19, the program reads two integers and prints them in ascending order. This is achieved with an '' | ||
+ | |||
+ | < | ||
+ | < | ||
+ | enter second value: < | ||
+ | 13 and 25 | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== Shortened if statement ===== | ||
+ | |||
+ | <code groovy 1 | Example 20: Interchange two values (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | Example 20 repeats this exercise. This time, the program employs the shortened version of the '' | ||
+ | |||
+ | ===== More complex decisions ===== | ||
+ | |||
+ | ^ Score ^ Grade ^ | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | |||
+ | ---- | ||
+ | |||
+ | Various combinations of '' | ||
+ | |||
+ | ===== Grading program ===== | ||
+ | |||
+ | <code groovy> | ||
+ | if (score >= 70) { | ||
+ | grade = ' | ||
+ | } else if (score >= 60) { | ||
+ | grade = ' | ||
+ | } else if (score >= 50) { | ||
+ | grade = ' | ||
+ | } else if (score >= 40) { | ||
+ | grade = ' | ||
+ | } else { | ||
+ | grade = ' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | This chain of '' | ||
+ | |||
+ | ===== Switch Statement ===== | ||
+ | |||
+ | <code groovy> | ||
+ | switch (expression) { | ||
+ | case expression #1: | ||
+ | statement #1a | ||
+ | statement #1b | ||
+ | ... | ||
+ | case expression #2: | ||
+ | statement #2a | ||
+ | statement #2b | ||
+ | ... | ||
+ | ... | ||
+ | case expression #N: | ||
+ | statement #Na | ||
+ | statement #Nb | ||
+ | ... | ||
+ | default: | ||
+ | statement #Da | ||
+ | statement #Db | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | The '' | ||
+ | |||
+ | In this statement '' | ||
+ | |||
+ | ===== Basic switch behaviour ===== | ||
+ | |||
+ | <code groovy 1 | Example 21: Unexpected switch behaviour (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | This slide illustrates the basic behaviour of a '' | ||
+ | |||
+ | The control expression is simply the value of the variable '' | ||
+ | Two | ||
+ | Three | ||
+ | Four | ||
+ | Default | ||
+ | End of switch | ||
+ | |||
+ | |||
+ | |||
+ | ===== switch and break statement ===== | ||
+ | |||
+ | <code groovy 1 | Example 22: switch and break statement (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | Nornally, the statements of a case label are intended to be mutually exclusive. Having selected the matching case expression, we normally wish for only the corresponding statements to be obeyed, and then control passed to the statement following the '' | ||
+ | |||
+ | Running this program produces: | ||
+ | Two | ||
+ | End of switch | ||
+ | |||
+ | |||
+ | |||
+ | ===== Grading with switch ===== | ||
+ | |||
+ | <code groovy 1 | Example 23: switch and a range (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | A '' | ||
+ | |||
+ | Running this example produces: | ||
+ | |||
+ | < | ||
+ | <pre> | ||
+ | Enter examination score: < | ||
+ | Score: 54; grade: C | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== Switching on a List ===== | ||
+ | |||
+ | <code groovy 1 | Example 24: List case expressions (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | In Groovy, the case expression can be a '' | ||
+ | |||
+ | The output is: | ||
+ | Number is thirty something | ||
+ | |||
+ | |||
+ | |||
+ | ===== Regular expressions for case labels ===== | ||
+ | |||
+ | <code groovy 1 | Example 25: Regular expressions for case labels (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | In this example, the '' | ||
+ | |||
+ | The output is: | ||
+ | number is a 4-digit sequence | ||
+ | |||
+ | |||
+ | ===== Break statement ===== | ||
+ | |||
+ | <code groovy 1 | Example 26: for looop and break statement (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | The //break statement// is used to alter the flow of control inside loops and '' | ||
+ | |||
+ | In this example, the program forms the sum of at most 100 positive integer values. The use provides these values as input. If, at any point, a negative value is entered, the '' | ||
+ | |||
+ | A typical run of the program produces: | ||
+ | < | ||
+ | <pre> | ||
+ | Enter next value: < | ||
+ | Enter next value: < | ||
+ | Enter next value: < | ||
+ | Enter next value: < | ||
+ | Enter next value: < | ||
+ | sum: 50 | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== Continue Statement ===== | ||
+ | |||
+ | <code groovy 1 | Example 27: for loop and continue statement (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | The //continue statement// complements the '' | ||
+ | |||
+ | In this final example, the program finds the sum of 10 integers input by the user. If any negative value is entered, it is ignored, but it is counted as one of the 10 values. | ||
+ | |||
+ | A typical run of this program is: | ||
+ | < | ||
+ | <pre> | ||
+ | Enter next value: < | ||
+ | Enter next value: < | ||
+ | Enter next value: < | ||
+ | Enter next value: < | ||
+ | Enter next value: < | ||
+ | Enter next value: < | ||
+ | Enter next value: < | ||
+ | Enter next value: < | ||
+ | Enter next value: < | ||
+ | Enter next value: < | ||
+ | sum: 37 | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== Summary of this Lecture ==== | ||
+ | |||
+ | |||
+ | * [[# | ||
+ | * [[#Flow of Control]] | ||
+ | |||
+ | ===== Lab Exercises ===== | ||
+ | |||
+ | |||
+ | * [[at-m42: | ||
+ | |||
+ | ---- | ||
+ | |||
+ | [[Home]] | [[lecture4|Previous Lecture]] | [[Lectures]] | [[lecture6|Next Lecture]] | ||
at-m42/lecture5.txt · Last modified: 2011/01/14 12:45 by 127.0.0.1