G 54 PRG Programming Lecture 6 Decision Making































- Slides: 31

G 54 PRG Programming Lecture 6 Decision Making, Control Structures & Error Handling Amadeo Ascó Adam Moore 1

Previously • Variables: hold information • Primitive Data Types – – – – boolean true and false byte 1 byte, -128 to 127 char 2 bytes, from 0 to 65536 (short 2 bytes number) int 4 bytes, from Integer. MIN_VALUE to Integer. MAX_VALUE long 8 bytes, from Long. MIN_VALUE to Long. MAX_VALUE float 4 bytes , from Float. MIN_VALUE to Float. MAX_VALUE double 8 bytes , from Double. MIN_VALUE to Double. MAX_VALUE • Other Data Types: Objects Amadeo Ascó , Adam Moore 2

Overview • Decision Making • Control Structures • Error Handling Amadeo Ascó , Adam Moore 3

Decision Making The Traffic light an example Amadeo Ascó , Adam Moore 4

Decision Making • Traffic lights – Red or amber then stop – Green and stopped then start – Green and car running continue • Some decisions are required • Pseudocode if red or amber then else stop if stopped then start else continue Amadeo Ascó , Adam Moore 5

Decision Making • if Statement – if-else branching if red or amber then stop else if stopped then start else continue if (b. Red || b. Amber) { … } else { if (b. Stopped) { … } else { … } } Examples: Count and re-set to zero when counter is 100 Amadeo Ascó , Adam Moore 6

Decision Making int i. Counter = 0; // initialise counter. . . if (i. Counter == 100) { // reset counter i. Counter = 0; }. . . Amadeo Ascó , Adam Moore 7

Decision Making • Format if (condition) { statements } if (condition) { statements 1 } else { statements 2 } if (condition 1) { statements 1 } else if (condition 2) { statements 2 } else { statements 3 } Amadeo Ascó , Adam Moore 8

Decision Making • Conditions result in boolean values • Condition can be – A boolean – Expressions that result in a boolean – Combination of Rational Operators and Method that return boolean Amadeo Ascó , Adam Moore 9

Decision Making • Rational Operators counter. A == counter. B == is equal to counter. A != counter. B != is not equal to counter. A > counter. B > is bigger than counter. A >= counter. B >= is bigger or equal to counter. A < counter. B < is smaller than counter. A <= counter. B <= is smaller or equal to (counter. A > counter. B) || (counter. A < counter. B) || logical OR (counter. A > counter. B) && (counter. A < counter. B) && logical AND Amadeo Ascó , Adam Moore 10

Decision Making • switch Statement – A way to simulate multiple if statements int i. Counter = 0; // initialisation int i. Value; . . . if (i. Counter == 0) { i. Value = -1; } else if (i. Counter == 1) { i. Value = -2; } else if (i. Counter == 2) { i. Value = -3; } else { i. Value = -4; } switch (i. Counter) { case 0: i. Value = -1; break; case 1: i. Value = -2; break; case 2: i. Value = -3; break; default: i. Value = -4; } // end switch Amadeo Ascó , Adam Moore 11

Decision Making – Format int i. Counter = 0; // initialisation String str. Msg; . . . switch (expression) { switch (i. Counter) { case constant 1: case 0: str. Msg = "1 st "; statements 1 break; case constant 2: case 1: str. Msg = "2 nd"; statements 1 break; case 2: … str. Msg = "3 rd"; default: break; default: statements str. Msg = "4 th"; } } // end switch – The expression must be a char, byte, short, int or enum Amadeo Ascó , Adam Moore 12

Control Structures • Loops – while – do – for • Loop Control – break – continue Amadeo Ascó , Adam Moore 13

Control Structures • while loop while (condition) { … statements … } // end while – The condition must result in a boolean – It may not pass through the statements even once Amadeo Ascó , Adam Moore 14

Control Structures int i. Index = 0; // initialise while (i. Index < 4) { System. out. println(i. Index); i. Index += 2; // add two } // end while System. out. println(i. Index); . . . Amadeo Ascó , Adam Moore index 4 2 0 0 2 4 15

Control Structures • do loop do { … statements … } while (condition); – The condition must result in a boolean – At least it will pass through the statements once Amadeo Ascó , Adam Moore 16

Control Structures int i. Index = 0; // initialise do { System. out. println(i. Index); i. Index += 2; // add two } while (index < 4); System. out. println(i. Index); . . . Amadeo Ascó , Adam Moore index 4 2 0 0 2 4 17

Control Structures int i. Index = 4; // initialise do { while (i. Index < 4) { System. out. println(i. Index); i. Index += 2; // add two } while (index < 4); } // end while System. out. println(i. Index); 4 4 6 Amadeo Ascó , Adam Moore 18

Control Structures int i. Index = 0; // initialise do { System. out. println(i. Index); i. Index += 2; // add two } while (index < 4); System. out. println(i. Index); . . . while (i. Index < 4) { System. out. println(i. Index); i. Index += 2; // add two } // end while System. out. println(i. Index); . . . Amadeo Ascó , Adam Moore 19

Control Structures • for loop for (from; condition; change) { … statements … } // end for – The from is a definition or initialisation (optional) – The condition must result in a boolean – The change must be an statement(s) – It may not pass through the statements even once Amadeo Ascó , Adam Moore 20

Control Structures for (int i. Index = 0; i. Index < 4; i. Index += 2) { System. out. println(i. Index); } // end for System. out. println(i. Index); Amadeo Ascó , Adam Moore index 4 2 0 0 2 4 21

Control Structures Initialisation -Increment Condition - Increment Condition int i. Index = 0; // initialise for (int i. Index = 0; i. Index < 4; do { while (i. Index < 4) { i. Index += 2) { System. out. println(i. Index); i. Index += 2; } // end for } while (i. Index < 4); } // end while System. out. println(i. Index); . . Amadeo Ascó , Adam Moore 22

Control Structures • break int counter = 0; // initialise – Stop the loop and leave it • continue – Go to the starting of the loop and continue the execution Amadeo Ascó , Adam Moore do { if (counter == 2) { counter = 4; continue; } else if (counter == 5) { break; } ++counter; } while (counter < 10); . . . 23

Control Structures int i. Counter = 0; // initialise do { if (i. Counter == 1) { i. Counter = 4; continue; } else if (i. Counter == 5) { break; } ++i. Counter; } while (i. Counter < 10); . . . Amadeo Ascó , Adam Moore counter 1 5 04 24

Error Handling • Help to increase robustness • Elegant way to handle errors • Allows to detect errors easily • Keep handling code separate from generating error Amadeo Ascó , Adam Moore 25

Error Handling • Format – Generating error • It is thrown where the error is found throw new Exception. Type(…); • The method where it is thrown from must identify the thrown exception with the keyword throws public static void main(String[] astr. Args) throws Exception. Type • Runtime. Exceptions don’t need to be declared in the signature of the method that throws them Amadeo Ascó , Adam Moore 26

Error Handling /** * Implements the division of the passed value by the passed divisor. * * @param value the divided element on the division. * @param divisor the element to divide by. * @return the result of dividing the value by the. * @throws Arithmetic. Exception when the divisor is zero. */ public int divide(int value, int divisor) throws Arithmetic. Exception { if (divisor == 0) { throw new Arithmetic. Exception("Divide by zero"); } return (value / divisor); } // divide() Amadeo Ascó , Adam Moore 27

Error Handling – Throwable handling code try { … statements … } catch (Exception. Type excp) { … error notification and recovery statements … } finally { … last statements run … } // end try Amadeo Ascó , Adam Moore 28

int result; Error Handling try { // Dividing result = divide(value, divisor); System. out. println("Result: " + result); // result of the division } catch (Arithmetic. Exception excp) { System. err. println(excp. get. Message()); result = -1; // assignment System. out. println("Result: " + result); // result of assignment } finally { // Resetting result = 0; } // end try System. out. println("Result: " + result); // result of resetting Amadeo Ascó , Adam Moore 29

Error Handling int result; value try { // Dividing result = divide(value, divisor); System. out. println("Result: " + result); } catch (Arithmetic. Exception excp) { System. err. println(excp. get. Message()); result = -1; // assignment System. out. println("Result: " + result); } finally { // Resetting result = 0; } // end try System. out. println("Result: " + result); divisor 3 Amadeo Ascó , Adam Moore 12 Result: 4 Result: 0 value 12 divisor 0 Divide by zero Result: -1 Result: 0 30

Error Handling java. lang. Throwable java. lang. Exception java. lang. Runtime. Exception java. lang. Arithmetic. Exception java. lang. Null. Pointer. Exception java. lang. Index. Out. Of. Bounds. Exception Own. Exception Amadeo Ascó , Adam Moore 31