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

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

Dec 17, 2015

Download

Documents

Adrian Howard
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 G54PRG Programming Lecture 1 Amadeo Ascó Adam Moore 6 Decision Making, Control Structures & Error Handling.

1

G54PRG ProgrammingLecture

1

Amadeo Ascó Adam Moore

6

Decision Making, Control Structures & Error Handling

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

2Amadeo 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

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

3Amadeo Ascó , Adam Moore

Overview

• Decision Making

• Control Structures

• Error Handling

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

4Amadeo Ascó , Adam Moore

Decision Making

The Traffic lightan example

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

5Amadeo Ascó , Adam Moore

if red or amber thenif red or amber thenif red or amber thenstop

if red or amber thenstop

if red or amber thenstop

elseif stopped then

startelse

if red or amber thenstop

elseif stopped then

startelse

if red or amber thenstop

else

if red or amber thenstop

else

if red or amber thenstop

elseif stopped then

startelse

continue

if red or amber thenstop

elseif stopped then

startelse

continue

if red or amber thenstop

elseif stopped then

startelse

continue

if red or amber thenstop

elseif stopped then

startelse

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

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

6Amadeo 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 thenstop

elseif stopped then

startelse

continue

if red or amber thenstop

elseif stopped then

startelse

continue

if (bRed || bAmber) { …} else { if (bStopped) { … } else { … }}

if (bRed || bAmber) { …} else { if (bStopped) { … } else { … }}

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

7Amadeo Ascó , Adam Moore

Decision Making

int iCounter = 0; // initialise counter...

if (iCounter == 100) {// reset counteriCounter = 0;

}...

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

8Amadeo 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}

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

9Amadeo 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

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

10Amadeo 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)

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

11Amadeo Ascó , Adam Moore

• switch Statement– A way to simulate multiple if statements

int iCounter = 0; // initialisationint 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; // initialisationint iValue;...

switch (iCounter) { case 0: iValue = -1; break; case 1: iValue = -2; break; case 2: iValue = -3; break; default: iValue = -4;} // end switch

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

12Amadeo Ascó , Adam Moore

– Formatswitch (expression) {

case constant1:statements1

case constant2:statements1

…default:

statements}

– The expression must be a char, byte, short, int or enum

Decision Makingint iCounter = 0; // initialisationString 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; // initialisationString strMsg;...switch (iCounter) { case 0: strMsg = "1st "; break; case 1: strMsg = "2nd"; break; case 2: strMsg = "3rd"; break; default: strMsg = "4th";} // end switch

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

13Amadeo Ascó , Adam Moore

Control Structures

• Loops– while

– do

– for

• Loop Control– break

– continue

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

14Amadeo 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

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

15Amadeo Ascó , Adam Moore

024

Control Structures

int iIndex = 0; // initialise

while (iIndex < 4) { System.out.println(iIndex); iIndex += 2; // add two} // end whileSystem.out.println(iIndex);...

int iIndex = 0; // initialise

while (iIndex < 4) { System.out.println(iIndex); iIndex += 2; // add two} // end whileSystem.out.println(iIndex);...

indexindex

024

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

16Amadeo 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

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

17Amadeo Ascó , Adam Moore

int iIndex = 0; // initialise

do { System.out.println(iIndex); iIndex += 2; // add two} while (index < 4);System.out.println(iIndex);...

024

Control Structures

indexindex

024

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

18Amadeo Ascó , Adam Moore

int iIndex = 4; // initialise

do { System.out.println(iIndex); iIndex += 2; // add two} while (index < 4);System.out.println(iIndex);

Control Structuresint iIndex = 4; // initialise

while (iIndex < 4) {

System.out.println(iIndex);

iIndex += 2; // add two

} // end whileSystem.out.println(iIndex);4

64

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

19Amadeo 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 whileSystem.out.println(iIndex);...

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

20Amadeo 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

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

21Amadeo Ascó , Adam Moore

for (int iIndex = 0; iIndex < 4; iIndex += 2) { System.out.println(iIndex);} // end forSystem.out.println(iIndex);

024

Control Structures

indexindex

024

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

22Amadeo Ascó , Adam Moore

for (int iIndex = 0; iIndex < 4; iIndex += 2) { System.out.println(iIndex);} // end forSystem.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 whileSystem.out.println(iIndex);...

InitialisationConditionIncrementInitialisation - Condition - Increment

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

23Amadeo 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);...

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

24Amadeo Ascó , Adam Moore

0int iCounter = 0; // initialise do { if (iCounter == 1) { iCounter = 4; continue; } else if (iCounter == 5) { break; } ++iCounter;} while (iCounter < 10);...

514

Control Structures

countercounter

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

25Amadeo 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

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

26Amadeo 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

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

27Amadeo 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()

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

28Amadeo Ascó , Adam Moore

– Throwable handling code

try { … statements …

} catch (ExceptionType excp) { … error notification and recovery statements … } finally {

… last statements run … } // end try

Error Handling

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

29Amadeo Ascó , Adam Moore

Error Handlingint 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 trySystem.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 trySystem.out.println("Result: " + result); // result of resetting

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

30Amadeo Ascó , Adam Moore

Error Handlingint 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 trySystem.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 trySystem.out.println("Result: " + result);

valuedivisor

12 3

Result: 4Result: 0

valuedivisor

12 0

Divide by zeroResult: -1Result: 0

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

31Amadeo Ascó , Adam Moore

Error Handlingjava.lang.Throwable

java.lang.Exception

java.lang.RuntimeException

OwnException

java.lang.ArithmeticExceptionjava.lang.NullPointerException

java.lang.IndexOutOfBoundsException