Programming for Geographical Information Analysis: Core Skills - PowerPoint PPT Presentation

1 / 67
About This Presentation
Title:

Programming for Geographical Information Analysis: Core Skills

Description:

Programming for Geographical Information Analysis: Core Skills Lecture 3: Program Flow I Loops and Branches – PowerPoint PPT presentation

Number of Views:85
Avg rating:3.0/5.0
Slides: 68
Provided by: Andrew1392
Category:

less

Transcript and Presenter's Notes

Title: Programming for Geographical Information Analysis: Core Skills


1
Programming for Geographical Information
AnalysisCore Skills
  • Lecture 3 Program Flow I
  • Loops and Branches

2
Review
  • Last lecture looked at classes, blocks, variable
    and objects.
  • public class Numbers
  • public static void main (String args)
  • int number 10
  • System.out.println(number)
  • int numbers new int2
  • numbers0 123
  • numbers1 321
  • System.out.println(numbers0)

3
Review
  • public class GIS
  • public static void main (String args) Point
    point1 new Point()
  • System.out.println(point1.x)
  • public class Point
  • double x 100.0

4
Code structure
  • We have seen that you can structure code through
    classes, linking them by object instantiation.
  • However, the code still reads like a shopping
    list of commands to do one after another.
  • This is inflexible, and also results in very long
    code.
  • We need some way of
  • Repeating useful code without copying it out
    again.
  • Branching our code based on processing and user
    decisions.
  • Separating off useful code into the classes that
    contain the data the code acts on.
  • Responding to user interactions.

5
Flow control
  • This lecture
  • Looping
  • Looping with arrays
  • Branching
  • Coding style

6
Flow control
  • Future parts will look at
  • Separating code into procedures.
  • And then, a bit later in the course
  • Responding to user events.

7
Loops
  • Lets imagine we wanted to run a piece of code
    until some condition was true. To do this, we can
    use the while construction. For example,
  • int i 0
  • while (i lt 3)
  • i
  • Remember i increments i by one, and is the
    same as i i 1
  • How does i change as this code runs?

8
Loops
  • Lets throw in some printlns to see how i changes
    (always useful)
  • int i 0
  • while (i lt 3)
  • System.out.println(i)
  • i
  • System.out.println("Finished")
  • The code in the block is repeated all the time a
    is less than 3, so prints 0, 1, 2.
  • The code then continues with the next line and
    prints finished.
  • Note that at this point, i is 3.

9
Conditions and comparisons
  • The (i lt 3) bit is known as the condition.
  • The loop runs all the time it is true.
  • Along with the mathematical operators, there are
    also comparison operators
  • gt greater than
  • gt greater than or equal to
  • lt less than
  • lt less than or equal to
  • equal to (used because is for
    assigning variables)
  • ! not equal to

10
Comparing Strings
  • Because Strings are objects, you cant do this
  • String a hi
  • String b hi
  • while (a b)
  • Because the answer is, no, theyre not the same
    object. Theres a special way of comparing
    Strings, which is
  • while (a.equals(b))
  • while (a.equals(hi))
  • this compares the value of the text in the
    String.

11
Boolean operators
  • There are also Boolean operators, used for
    putting several conditions together
  • Boolean AND
  • Shortcut Boolean AND
  • Boolean OR
  • Shortcut Boolean OR
  • ! Not
  • Note that if you are going to use multiple
    conditions, each condition, and the overall
    condition need to be in parentheses
  • while ( (a ! b) (b gt 10) )

12
Examples
  • while ((a ! b) (b gt 10))
  • Means do this while it is true that a is not
    equal to b, AND b is greater than 10.
  • while ((a ! b) (b gt 10))
  • Means do this while it is true that a is not
    equal to b, OR b is greater than 10.
  • while (booleanVariable true)
  • while (booleanVariable)
  • Means do it while a boolean variable is true.
  • while (booleanVariable false)
  • while (!booleanVariable)
  • Means do it while it is true that a boolean
    variable is false.

13
Shortcuts
  • The checking of conditions is quite complicated,
    and takes the computer a relatively long time, so
    we want to minimise checking. The shortcut
    operators dont bother to assess the second
    condition, if the first is unfavourable, for
    example, in
  • while ((a ! b) (b gt 10))
  • the (b gt 10) isnt assessed if (a ! b) is
    untrue, because there isnt any point the
    overall condition is false.
  • Equally, in
  • while ((a ! b) (b gt 10))
  • the (b gt 10) isnt assessed if (a ! b) is
    true, because there isnt any point the overall
    condition is true.

14
While-loops continued
  • With
  • while (i lt 3)
  • i
  • The i only happens if the condition is true.
    If you want the while loop to run once whether it
    is true or not, use
  • do
  • i
  • while (i lt 3)

15
Counting loops
  • This code is works ok
  • int i 0
  • while (i lt 3)
  • i
  • but as a coder it is a bit issue-prone. The
    variable declaration for i, the condition, and
    the incrementing of i are all in different
    places.
  • There are various problems with this, not least
    that the scope of i is larger than it needs to
    be, and that, with cutting and pasting, all kinds
    of problems could creep in.
  • While-loops are good for some things (like
    looping until you find a file) but bad for
    counting.

16
For-loops
  • There is a special construction for counting,
    which is much cleaner
  • for (int i 0 i lt 3 i)
  • System.out.println(i)
  • System.out.println("Finished")
  • Here we see all the familiar parts the variable
    declaration, the condition, and the increment,
    but all are in the same place, and the scope of i
    is just the loop block.
  • Again, this prints 0, 1, 2, but at the end,
    i is destroyed, rather than hanging around
    causing potential problems.

17
Variations
  • There is nothing to stop you dropping, one, two,
    or all of the parts of the for-loop, as long as
    you have the semi-colons so this loops forever
  • for ( )
  • System.out.println
  • (All work and no play makes Jack a dull boy.)
  • Hint to get out of infinitely looping or
    unresponsive (hung) programs at the command
    prompt, push the CTRL and C key together.

18
Variations
  • Note also, that the increment can be replaced
    with expanded maths, so
  • for (int i 0 i lt 3 i i 1)
  • Or any other kind of maths to do each loop.
  • The increment doesnt have to be plus one.
  • for (int i 0 i lt 3 i i 2)
  • for (int i 3 i gt 0 i--)
  • What do you think i-- does?

19
For-each
  • Introduced in Java 1.5.
  • Allows iteration through variable collections.
  • int arr 10,50,30
  • for (int a arr)
  • System.out.println(a)
  • Will print 10, 50, 30.

20
Assignment to variables
  • Remember, however, that primitives are copied,
    while objects are linked to, so
  • int arr 10,50,30
  • for (int a arr)
  • a 2
  • wont change the data in the array, whereas
  • Point arr
  • new Point(),new Point(),new Point()
  • for (Point a arr)
  • a.x 2
  • will change the Points, as a is a link to them.

21
More about loops
  • With all loops, if there is just one statement to
    do, you can forget the brackets, e.g.
  • for (int i 0 i lt intArray.length i)
  • System.out.println(i)
  • do System.out.println(i) while (i lt 10)
  • Dont. Always put in brackets. That way, if you
    decide to put more in the loops, the brackets are
    there already.
  • Infact, generally, with all blocks, put in both
    the brackets before you write any code in them.

22
Evil control statements
  • goto
  • Darkness encoded, goto is Satans only friend in
    this realm of joy and light.
  • break (end loop) and continue (go to beginning
    line).
  • Used to end blocks and temporarily skip loops
    respectively.
  • Not as bad as goto was, but still to be used very
    sparingly.

23
When to use break and continue
  • Use only when you need to escape a loop in the
    middle of something that would be very complex to
    write avoiding it.
  • while (file wrongFile)
  • // code to get a file
  • // if we find the right file, continue
  • // code to destroy file for space

24
Review
  • for (int i 0 i lt 3 i)
  • System.out.println(i)
  • while (i lt 3)
  • i

25
Flow control
  • Looping
  • Looping with arrays
  • Branching
  • Coding style

26
Using arrays
  • When we looked at arrays, we saw that you need to
    assign them a value, like other variables
  • array1D0 10.0
  • And to use them you need to get the data from
    them, thus
  • System.out.println(array1D0)
  • Ok, so having a single name saves us from
    thinking up new names, but this still seem pretty
    intensive work if we have to type a line for each
    array position. What if we have 10000?
  • One key idea will make our lives easier
  • Instead of hardwiring in our position number, why
    not use the index from a loop to go through all
    the positions?

27
Looping through arrays
  • Heres the trick with a simple assignment and
    request from the array
  • int array1D new int100
  • for (int i 0 i lt array1D.length i)
  • array1Di 10
  • System.out.println(array1Di)
  • Note the clever use of array1D.length, which is
    never reached (and quite right too remember
    that numbering of positions starts at zero).

28
Assignment
  • Obviously we might want more complex assignments,
    including things like
  • array1Di 10 i
  • array1Di // some code to read data
  • I cannot express how wonderful this simple slight
    of hand is. Youll soon take it so for granted
    that youll barely notice it, so take some time
    now to appreciate how clever this really is.

29
Enlarging arrays
  • Arrays are fixed in size. To enlarge or shrink
    them, copy them into a larger or smaller array,
    adding/removing a space where needed.
  • // using some pre-existing array arrayA.
  • int arrayB new intarrayA.length 1
  • for (int i 0 i lt arrayA.length i)
  • arrayBi arrayAi
  • arrayA null
  • arrayBarrayB.length 1 10
  • Note that we use the same index value to loop
    through both arrays.
  • Removing a space is more complex. Can you work
    out how to do it?

30
Looping
  • But what about multi-dimensional arrays?
  • In geography, where data is often in 2D arrays,
    this is common. Wed like some way of going to
    each row and then travelling across it to each
    space in a row.
  • Well, this is even cleverer. For 2D arrays, we
    nest two for-loops.
  • Allows us to run across each cell in a series of
    rows.
  • First, lets look at it without arrays.

31
Nesting loops
  • for (int i 0 i lt 2 i)
  • for (int j 0 j lt 3 j)
  • System.out.println (i " " j)
  • Remember, that variables are destroyed as the
    processing leaves their scope, and re-made if the
    code runs again.
  • The outer loop starts, then the inner loop
    starts.
  • When the inner loop has run once, it returns to
    the start of the inner loop, not the outer loop.
  • It keeps doing this until run to completion (j
    3 i still zero).
  • What do you think happens to j then, and where
    does the code go next?

32
Nesting loops
  • for (int i 0 i lt 2 i)
  • for (int j 0 j lt 3 j)
  • System.out.println (i " " j)
  • 4) j is destroyed, and the outer loop increments
    to i 1.
  • 5) The inner loop runs again, j recreated as j
    0.
  • 6) The inner loop runs to completion.
  • Thus, each time the outer loop runs once, the
    inner loop runs to completion.
  • 7) This is repeated until the outer loop
    completes.
  • 8) The code then moves on.

33
Nested loops
  • Lets look at i and j
  • i j
  • 0 0
  • 0 1
  • 0 2
  • 1 0
  • 1 1
  • 1 2
  • This is exactly what we need for moving down a
    row at a time in our array (i) and then running
    across each row a space at a time (j).

34
2D arrays
  • int array2D new int23
  • for (int i 0 i lt array2D.length i)
  • for (int j 0 j lt array2Di.length j)
  • array2Dij 10
  • System.out.println (array2Dij)
  • Note that i is in scope in the inner block, so we
    can use array2Di.length to cope with irregular
    arrays.

35
2D issues
  • This is surely one of the neatest algorithms
    ever!
  • However, it is easy to make mistakes. There are
    three problems with the below. Can you spot them?
  • int array2D new int23
  • for (int i 0 i lt array2D.length i)
  • for (int j 0 j lt array2Dj.length i)
  • array2Dij 10
  • System.out.println (array2Dji)

36
2D issues
  • The three mistakes are classics that everyone
    makes, even experienced coders
  • int array2D new int23
  • for (int i 0 i lt array2D.length i)
  • for (int j 0 j lt array2Dj.length i)
  • array2Dij 10
  • System.out.println (array2Dji)
  • array2Dj.length Looping through to the wrong
    dimension length. This is very common if the
    lengths are hard-wired in, so avoid that.

37
2D issues
  • int array2D new int23
  • for (int i 0 i lt array2D.length i)
  • for (int j 0 j lt array2Dj.length i)
  • array2Dij 10
  • System.out.println (array2Dji)
  • i Cutting and pasting your outer loop to make
    your inner loop, and forgetting to change part of
    the variable use here, the inner increment
    should be to j.

38
2D issues
  • int array2D new int23
  • for (int i 0 i lt array2D.length i)
  • for (int j 0 j lt array2Dj.length i)
  • array2Dij 10
  • System.out.println (array2Dji)
  • System.out.println (array2Dji) Switching the
    indices the wrong way round. This should be
    array2Dij. With an non-square array, this
    will result in trying to read off one side of the
    array and the program will break. Worse, with a
    square array, your data will silently be
    transposed.
  • If you get confused, run through your algorithm
    by hand on paper, using a 2 by 3 non-square
    array.

39
Looping through 2D arrays
  • So, heres our standard code for looping through
    a raster image or other dataset
  • for (int i 0 i lt array2D.length i)
  • for (int j 0 j lt array2Di.length j)
  • System.out.println(array2Dij)

40
Variations
  • Looping through the same positions in two arrays
  • for (int i 0 i lt arrayA.length i)
  • for (int j 0 j lt arrayAi.length j)
  • arrayBij arrayAij

41
Variations
  • Looping through two arrays at positions relative
    to one array (note boundary problem)
  • for (int i 1 i lt arrayA.length - 1 i)
  • for (int j 1 j lt arrayAi.length - 1 j)
  • arrayBij arrayAi-1j-1

arrayA
arrayB
42
Boundary problems
  • Various solutions.
  • Depends on problem context.

Wrap boundaries Suitable for modelling
abstract landscapes
Only process as many cells as you can Suitable
for modelling non- abstract landscapes
Only process cells that can have complete
processing Suitable for image processing
43
Review
  • Loops allow us to repeat the same code, with two
    important results
  • we make less mistakes in our code
  • we can get arduous jobs, like processing arrays,
    done easily.

44
Flow control
  • Looping
  • Looping with arrays
  • Branching
  • Coding style

45
Branching
  • Processing can rarely be done by a single list of
    instructions.
  • We usually need to have different sets of code
    running, depending on conditions or user
    interaction.
  • For this, we need to branch our code down one or
    more execution paths.

46
Branching if
  • The simplest form is the if-statement
  • if (some condition)
  • do something
  • For example
  • if (earthRadius 6378)
  • System.out.print("radius correct")
  • Block only done if the whole condition is true.
  • Note that if theres only one line, it can be
    written without brackets, but avoid this.

47
if else
  • The if-else statement lets us branch down two
    mutually exclusive routes
  • if (condition) do something
  • else do something else
  • if (earthRadius 6378)
  • System.out.println("radius correct")
  • else
  • earthRadius 6378
  • System.out.println("radius corrected")

48
Ternary kung fu
  • The ? Ternary operator
  • A replacement for the if / else statement in
    assignment.
  • The most nifty move you can pull. Use it and
    other coders will give you a knowing nod in the
    street. You will be the Jackie Chan Kung Fu
    King/Queen of Code.
  • How do I pull this amazing move?
  • variable
  • condition?expression1expression2
  •   If the condition is true, expression1 is used,
    otherwise expression2 is used.

49
Ternary example
  • name (fileFoundtrue)?(filename)(defaultName)
  •  
  • is the same as
  • if (fileFoundtrue)
  • name filename
  • else
  • name defaultName
  • Why use it?
  • Because we can just because we can.

50
The if / else / if ladder
  • For more complex statements with multiple
    choices.
  • if (condition)
  • statement or statements
  • else if (condition)
  • statement or statements
  • else if (condition)
  • statement or statements
  • else
  • statement or statements
  • You need to put the elements with the most chance
    of being true at the top, otherwise it is very
    inefficient.

51
The switch statement
  • switch (variable)
  • case value1
  • statements1
  • break
  • case value2
  • statements2
  • break
  • case value3
  • statements3
  • break
  • default
  • statements4
  • Slightly slower, but not as slow as a bad
    if-else-if ladder.
  • The break's are optional, if you leave one out,
    the computer will just run on into the next
    values case statements.

52
  • int day 2
  • switch (day)
  • case 1
  • System.out.println("Saturday")
  • break
  • case 2
  • System.out.println("Sunday")
  • break
  • default
  • System.out.println("Meh, whatever")
  • Note that unlike the if / else if / else ladder
    the conditions generally have to be fixed they
    cant be variables (this changed with Java 7, but
    many people have JRE 6).

53
Review
  • if (some condition)
  • do something
  • else
  • do something else

54
Flow control
  • Looping
  • Looping with arrays
  • Branching
  • Coding style

55
Why
  • We think of coding as all about the computer, but
    it is really all about the people users (first)
    but also developers.
  • For the users, we want code that runs fast and in
    an obvious manner (well come back to the latter
    when we look at user interfaces).
  • For developers, we want code that is easy to
    understand and reuse, otherwise our code will die
    off.

56
For Users Efficiency
  • Trying to speed up the program.
  • Knowing how the JVM and compiler work and making
    sure you program in the way that will make it
    fastest.
  • For example, using switch rather than if / else /
    if ladders, as switches only need one decision to
    be made.
  • Hard to learn because it relies on understanding
    how computers work.
  • Well point out examples, and the book list has
    some good sources.

57
For developers
  • We want code that other people can pick up and
    understand, just incase we spontaneously combust.
  • As it happens, this is also good for us. Your own
    code can seem like a strangers work in two
    weeks time.
  • This comes down to clarity and comments.
  • Other coders also have an appreciation of the art
    involved in programming, so you should aspire to
    elegance in your code it will also usually help
    with clarity and efficiency.

58
Clarity
  • You could write all your code on one line
  • public class HelloWorldpublic static void
    main(String args)char HHchar ee char
    ll char ooSystem.out.println(Hello
    World)
  • It would run, but would make very little sense to
    you or anyone else.

59
  • Instead, because spaces dont matter, we can make
    it look clearer, and we try to simplify the how
    it does stuff...
  • public class HelloWorld
  • public static void main (String args)
  • System.out.println(Hello World)
  • Note the amount of space and the tabbing as we
    enter blocks.

60
Comments
  • We also put in comments in our code like this
  • // This is a comment.
  • Using // at the start of the line. Such comments
    help when we come back to the code later.
  • Dont leave it until the end to tab and comment
    it is useful to do it as you go along, trust me
    even if it takes a bit of time.

61
Conventions
  • Coders also stick to certain conventions, like
    capitals for classes, and lower class for
    variables/objects.
  • This means youre always thinking about what you
    are using, and others can understand the code.
  • Most coders work in teams, but even if you dont,
    you need to get used to pretending you do it
    will help your code.
  • Java code conventions online at
  • http//www.oracle.com/technetwork/java/codeconvtoc
    -136057.html

62
Elegance
  • Its a piece of pi to write code that works, but
    runs badly and is impossible to understand.
  • Programmers therefore have a notion of elegance
    doing something in the simplest, clearest
    manner.

63
  • O Poesy! for thee I hold my pen//That am not yet
    a glorious denizen//Of thy wide heavenShould I
    rather kneel//Upon some mountain-top until I
    feel//A glowing splendour round about me
    hung,//And echo back the voice of thine own
    tongue?//O Poesy! for thee I grasp my pen//That
    am not yet a glorious denizen//Of thy wide
    heaven yet, to my ardent prayer,//Yield from thy
    sanctuary some clear air,//Smoothed for
    intoxication by the breath//Of flowering bays,
    that I may die a death//Of luxury, and my young
    spirit follow//The morning sun-beams to the great
    Apollo//Like a fresh sacrifice or, if I can
    bear//The oerwhelming sweets, twill bring me to
    the fair//Visions of all places a bowery nook
  • John Keats (1795-1821)

64
  • Mad with poetry
  • I stride like Chikusai
  • into the wind.
  • Matsuo Basho (1644 1694)

65
Course text
Elegance is having the right solution for the
job.
66
Review
  • Always tab appropriately and break up your code
    with blank lines.
  • Comment your code extensively.
  • Always stick to coding conventions and name your
    variables sensibly.
  • Keep your eyes open for efficiency hints.
  • Keep reading other peoples code you never stop
    learning to program, and you can learn a lot from
    seeing how other people tackle problems.

67
Practical
  • Filling our array with loops.
  • Flow control II Methods

Next lecture
Write a Comment
User Comments (0)
About PowerShow.com