Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 G54PRG Programming Lecture 1 Amadeo Ascó Adam Moore 6 Decision Making, Control Structures & Error Handling.

Similar presentations


Presentation on theme: "1 G54PRG Programming Lecture 1 Amadeo Ascó Adam Moore 6 Decision Making, Control Structures & Error Handling."— Presentation transcript:

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

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

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

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

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

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

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

8 8 Amadeo Ascó, Adam Moore Format Decision Making if (condition) { statements } if (condition) { statements } if (condition) { statements1 } else { statements2 } if (condition) { statements1 } else { statements2 } if (condition1) { statements1 } else if (condition2) { statements2 } else { statements3 } if (condition1) { statements1 } else if (condition2) { statements2 } else { statements3 }

9 9 Amadeo Ascó, Adam Moore 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 Decision Making

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

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

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

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

14 14 Amadeo Ascó, Adam Moore 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

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

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

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

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

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

20 20 Amadeo Ascó, Adam Moore 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

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

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

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

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

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

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

27 27 Amadeo Ascó, Adam Moore 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 ArithmeticException when the divisor is zero. */ public int divide(int value, int divisor ) throws ArithmeticException { if (divisor == 0) { throw new ArithmeticException("Divide by zero"); } return (value / divisor); } // divide() /** * 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 ArithmeticException when the divisor is zero. */ public int divide(int value, int divisor ) throws ArithmeticException { if (divisor == 0) { throw new ArithmeticException("Divide by zero"); } return (value / divisor); } // divide()

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

29 29 Amadeo Ascó, Adam Moore Error Handling int result; try { // Dividing result = divide(value, divisor); System.out.println("Result: " + result); // result of the division } catch (ArithmeticException excp) { System.err.println(excp.getMessage()); 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 int result; try { // Dividing result = divide(value, divisor); System.out.println("Result: " + result); // result of the division } catch (ArithmeticException excp) { System.err.println(excp.getMessage()); 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

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

31 31 Amadeo Ascó, Adam Moore Error Handling java.lang.Throwable java.lang.Exception java.lang.RuntimeException OwnException java.lang.ArithmeticException java.lang.NullPointerException java.lang.IndexOutOfBoundsException


Download ppt "1 G54PRG Programming Lecture 1 Amadeo Ascó Adam Moore 6 Decision Making, Control Structures & Error Handling."

Similar presentations


Ads by Google